Wednesday, 18 May 2016

Useful scripts for MS CRM

 Useful scripts for Dynamics CRM 


1. Get the Guid of logged in userid/username

Xrm.Page.context.getUserId()
Xrm.Page.context.getUserName()

2. Get the value from a CRM field:

var fieldvalue= Xrm.Page.getAttribute("fieldSchemaname").getValue() ;


3. Set the value of a CRM field:


Xrm.Page.getAttribute("fieldSchemaname").setValue(value);


4. Hide/Show a tab/section:


Xrm.Page.ui.tabs.get(5).setVisible(false);
Xrm.Page.ui.tabs.get(5).setVisible(true);


5.  Call the onchange event of a field:


Xrm.Page.getAttribute("fieldSchemaname").fireOnChange();

6. Get the selected value of picklist:


Xrm.Page.getAttribute("fieldSchemaname").getSelectedOption().text;


7. Set the requirement level:


Xrm.Page.getAttribute("fieldSchemaname").setRequiredLevel(“none”);
Xrm.Page.getAttribute("fieldSchemaname").setRequiredLevel(“required”);
Xrm.Page.getAttribute("fieldSchemaname").setRequiredLevel(“recommended”);

8. Set the focus to a field:


Xrm.Page.getControl(“CRMFieldSchemaName”).setFocus(true);


9. Return array of strings of users security role GUIDs:


Xrm.Page.context.getUserRoles()

10.  Get the form type


var formtype=Xrm.Page.ui.getFormType();

11.  Get entity id and entity Name

Var id =Xrm.Page.data.entity.getId();
var entityName = Xrm.Page.data.entity.getEntityName();

12. Get the lookup value

if (Xrm.Page.data.entity.attributes.get("fieldname").getValue() != null) 
{
         //Get guid of lookup record
          var CustomerId = Xrm.Page.data.entity.attributes.get("fieldname").getValue()[0].id;
         //Get name of lookup record
           var CustomerName = Xrm.Page.data.entity.attributes.get("fieldname").getValue()[0].name;
         //Get entity type of lookup record
           var CustomerType = Xrm.Page.data.entity.attributes.get("feildname ").getValue()[0].entityType;


       }

13. Set the lookup field

Xrm.Page.getAttribute("customerid").setValue([{ id: "475B158C-541C-E511-80D3-3863BB347BA8", name: "A. Datum", entityType: "Account"}]);

or 

function SetLookUp(fieldName, fieldType, fieldId, value)
 {
try {
debugger;
var object = new Array();
object[0] = new Object();
object[0].id = fieldId;
object[0].name = value;
object[0].entityType = fieldType;
Xrm.Page.getAttribute(fieldName).setValue(object);
}
catch (e) {
alert("Error in SetLookUp: fieldName = " + fieldName + " fieldType = " + fieldType + " fieldId = " + fieldId + " value = " + value + " error = " + e);
}
}






No comments:

Post a Comment