Show/Hide Ribbon Button using Enable Rule

If you want to show/hide a ribbon button, but you are having problems because of the load/save events that sometimes makes the show/hide not work correctly, this is the best way I found to get the job done.

Just need to add this bit of code in the RibbonDiffXml in the extracted solution of the entity you want to add (or already have) the button, in the customization.xml file.

<CommandDefinitions>
 <CommandDefinition Id="Mscrm.Form.entity.name"> <!--button id-->
 <EnableRule Id="entity.functionName.EnableRule" />
 <DisplayRules />
 <Actions>
 <JavaScriptFunction FunctionName="markopportunityasgo" Library="$Webresource:cst_Opportunity2013" />
 </Actions>
 </CommandDefinition>
 </CommandDefinitions>

<EnableRules>
 <EnableRule Id="entity.functionName.EnableRule">
 <CustomRule FunctionName="javascriptFunction" Library="$webresource:javascriptFileName">
 <CrmParameter Value="PrimaryItemIds" /> <!--get entity id-->
 </CustomRule>
 </EnableRule>
 </EnableRules>
//Javascript function that get the entity id. If gives error, you can remove it from the funtion
function javascriptFunction(id)
{
 //Logic
 return true; //shows button
}
Advertisements

Create/Update Entity records using XrmServiceToolKit.Soap

If you want to create or update a record using the XrmServiceToolKit Soap and don’t know how to do it for all the field types, here is some code that might help you.

//Create Entity
var entity = new XrmServiceToolkit.Soap.BusinessEntity("EntityName");
//String
entity.attributes["fieldName"] = Xrm.Page.getAttribute("fieldName").getValue();

//Lookup
entity.attributes['fieldName'] = { id : Xrm.Page.getAttribute('fieldName').getValue()[0].id, logicalName : Xrm.Page.getAttribute('fieldName').getValue()[0].entityType, type : 'EntityReference' };

//Bool
entity.attributes["fieldName"] = Xrm.Page.getAttribute("fieldName").getValue();

//OptionSetValue
entity.attributes["fieldName"] = { value: Xrm.Page.getAttribute("fieldName").getValue(), type: "OptionSetValue" };

//Currency
entity.attributes["fieldName"] = { Value: Xrm.Page.data.entity.attributes.get("fieldName").getValue() };

//Decimal
entity.attributes["fieldName"] = { Value: parseFloat(eval(Xrm.Page.data.entity.attributes.get("fieldName").getValue())) };

entity.attributes["fieldName"] = { Value: 2, type: "int"}; 

entity.attributes["fieldName"] = { Value: 1.5617, type: "double" }; 

var quoteId;
quoteId = XrmServiceToolkit.Soap.Create(entity);

//Update Entity
var updateEntity = new XrmServiceToolkit.Soap.BusinessEntity("EntityName", entityGuid);
//Lookup to update
updateEntity.attributes["fieldName"] = { id : entityGuid, logicalName : 'entityName', type : 'EntityReference' };
//OptionSet
updateEntity.attributes["fieldName"] = { value : Value, type : 'OptionSetValue' };
//Currency
updateEntity.attributes["fieldName"] = { value : Value, type : 'Money' };
//Boolean
updateEntity.attributes["fieldName"] = { value : true/false, type: 'boolean' };
//String
updateEntity.attributes["fieldName"] = "Value";

updateEntity.attributes["fieldName"] = { Value: 2, type: "int"}; 

updateEntity.attributes["fieldName"] = { Value: 1.5617, type: "double" }; 

</pre>
<pre>var updateResponse = XrmServiceToolkit.Soap.Update(updateEntity );