Ribbon Button: Disable the Button in HomePageGrid depending on the value of the selected record CRM 2011

Microsoft Dynamics CRM is a Customer Relationship Management software package developed by Microsoft

Ribbon Button: Disable the Button in HomePageGrid depending on the value of the selected record CRM 2011

Postby gellee » Wed Jun 27, 2012 6:36 pm

1. Create a new solution OR use default solution
>>Settings > Customization > Solutions > New >
2. Open NewSolution
3. Add the your Entity and you need to create a custom button
4. Export the solution then extract
5. Open the customization.xml from the file extracted earlier
6. Find the <RibbonDiffXml> and under <RuleDefinitions> add new <EnableRule>

<EnableRules>
<EnableRule Id="DeleteEnableId">

<CustomRule FunctionName="myfunction" Library="$webresource:new_myLibrary.js">
<CrmParameter Value="SelectedControlSelectedItemIds" />
</CustomRule>
</EnableRule>

</EnableRules>


7. Add new <CommandDefinition> and add the EnableRule Id

<CommandDefinition Id="Mscrm.DeleteSelectedRecord">
<EnableRules>
<EnableRule Id="Mscrm.SelectionCountAtLeastOne" />
<EnableRule Id="Mscrm.VisualizationPaneNotMaximized" />
<EnableRule Id="DeleteEnableId" />
</EnableRules>
<DisplayRules>
<DisplayRule Id="Mscrm.NotShowForManyToManyGrids" />
<DisplayRule Id="Mscrm.DeleteSelectedEntityPermission" />
</DisplayRules>
<Actions>
<JavaScriptFunction FunctionName="Mscrm.GridRibbonActions.deleteRecords" Library="/_static/_common/scripts/RibbonActions.js">
<CrmParameter Value="SelectedControl" />
<CrmParameter Value="SelectedControlSelectedItemReferences" />
<CrmParameter Value="SelectedEntityTypeCode" />
</JavaScriptFunction>
</Actions>
</CommandDefinition>

8. Under the <CommandUIDefinition> Add a new attribute to your button. Set/update the Command of your button to Mscrm.DeleteSelectedRecord

<CommandUIDefinition>
<Button
Alt="$Resources:Ribbon.HomepageGrid.MainTab.Management.Delete"
Command="Mscrm.DeleteSelectedRecord"
Description=""
Id="prm.prm_patientanniversary.Button1.Button"
Image32by32="/_imgs/Workplace/remove_32.png"
Image16by16="/_imgs/ribbon/delete16.png"
LabelText="$LocLabels:prm.prm_patientanniversary.Button1.Button.LabelText"
Sequence="45"
TemplateAlias="o2"
ToolTipTitle="$LocLabels:prm.prm_patientanniversary.Button1.Button.ToolTipTitle"
ToolTipDescription="$LocLabels:prm.prm_patientanniversary.Button1.Button.ToolTipDescription" />
</CommandUIDefinition>


9. >>You must create a javascript and add it to your web resource(Please see the below code), for this, the name of my library is new_myLibrary.js and the function inside is myfunction.
>>myfunction accepts one parameter, as you can see in the code below we passed one parameter value-SelectedControlSelectedItemIds

>>SelectedControlSelectedItemIds-A string array of GUID Id values for all selected items in a grid.


>>to know more about the valid parameters to be passed, visit http://msdn.microsoft.com/en-us/library/gg309332.aspx

// JScript source code

function myfunction(selectedIds) {
debugger;
var myid ;

if (selectedIds != null && selectedIds != "") {
for (var i = 0; i < selectedIds.length; i++) {
var ValueExist = CheckValue(selectedIds[i]);
if (ValueExist == false) {
return ValueExist;
}

}

}
else {
myid = Xrm.Page.data.entity.getId();
}


}

function CheckValue(myid) {
debugger;
// Prepare variables for a contact to retrieve.
var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.
var xml = "<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
authenticationHeader +
"<soap:Body>" +
"<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<entityName>contact</entityName>" +
"<id>" + myid + "</id>" +
"<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
"<q1:Attributes>" +
"<q1:Attribute>fullname</q1:Attribute>" +
"</q1:Attributes>" +
"</columnSet>" +
"</Retrieve>" +
"</soap:Body>" +
"</soap:Envelope>";
// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
// Capture the result.
var resultXml = xHReq.responseXML;

// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
//alert(msg);
}
// Display the retrieved value.
else {
var fullname = resultXml.selectSingleNode("//q1:fullname").nodeTypedValue;
if (fullname != "try") { // if type = Date of Birth
return true;
}
else {
alert("Disabled");
return false;
}
}
}


10. Save.
11. Re-zip
12. Import
13. Publish
14. Refresh your browser
User avatar
gellee
 
Posts: 46
Joined: Fri Jan 20, 2012 3:04 pm

Return to Microsoft Dynamics CRM

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Registered users: Baidu [Spider], Bing [Bot], Exabot [Bot], Google [Bot], Google Feedfetcher, Yahoo [Bot]