Wednesday, July 2, 2014

Microsoft Dynamic CRM 2011 useful XRM scripts


Get the value from a CRM field
var value = Xrm.Page.getAttribute(“CRMFieldSchemaName”).getValue();


Set the value of a CRM field
Xrm.Page.getAttribute(“CRMFieldSchemaName “).setValue(“New Value”);


Get the value from a CRM OptionSet field
var value = Xrm.Page.getAttribute(“CRMOptionSetSchemaName”).getValue();


Get the text from a CRM OptionSet field
var text = Xrm.Page.getAttribute(“CRMOptionSetSchemaName”).getText();


Set the value of a CRM OptionSet field
Xrm.Page.getAttribute(“CRMOptionSetSchemaName”).setValue(“1″); // OptionSet Value


Get the selected text of a CRM OptionSet field
Xrm.Page.getAttribute(“CRMOptionSetSchemaName”).getSelectedOption().text;


Get the selected value of a CRM OptionSet field
Xrm.Page.getAttribute(“CRMOptionSetSchemaName”).getSelectedOption().value;


Get the text and value of a CRM Lookup field
var lookupObject = Xrm.Page.getAttribute(“CRMLookupSchemaName”).getValue();
lookupObject[0].name; // text of lookup
lookupObject[0].id; // Guid of lookup


Set the value of a CRM Lookup field
var lookupData = new Array();
var lookupItem = new Object();
lookupItem.id = “4A2A54CB-349C-E111-8D26-1CC1DEE8DA78″; // Guid of record
lookupItem.name = “New Contact”; // Entity record name
lookupItem.entityType = “EntitySchemaName”;
lookupData[0] = lookupItem;
Xrm.Page.getAttribute(“CRMLookupSchemaName”).setValue(lookupData);


Disable CRM field
Xrm.Page.ui.controls.get(“CRMFieldSchemaName”).setDisabled(true);


Hide CRM field
Xrm.Page.ui.controls.get(“CRMFieldSchemaName”).setVisible(false);


Hide a Tab in CRM
Xrm.Page.ui.tabs.get(“tabName”).setVisible(false);


Hide a Section in CRM
var tab = Xrm.Page.ui.tabs.get(“tabName”);
tab.sections.get(“sectionName”).setVisible(false);


Set the Requirement level in CRM
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“required”);
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“none”);
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“recommended”);


Set Focus on a field in CRM
Xrm.Page.ui.controls.get(“CRMFieldSchemaName”).setFocus(true);


Cancelling Onsave Event in CRM
event.returnValue = false;
return false;


Note: Make sure to check the checkbox for Pass execution context as first parameter in event handlers of the form.

Check IsDirty in CRM field
var isDirty = Xrm.Page.getAttribute(“CRMFieldSchemaName”).getIsDirty();
alert(isDirty); // returns true if the field is dirty


Check IsDirty for all the fields in CRM
var isDirty = Xrm.Page.data.entity.getIsDirty();
alert(isDirty); // returns true if any of the field is dirty in the entire form.


Force Submit a read only field in CRM
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setSubmitMode(“always”);


Preventing an attribute to be saved in CRM form
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setSubmitMode(“never”);


Get Unique Organization Name in CRM
Xrm.Page.context.getOrgUniqueName();


Get Server url in CRM
Xrm.Page.context.getServerUrl();


Get the record Id in CRM
Xrm.Page.data.entity.getId();


Get the User Id in CRM
Xrm.Page.context.getUserId();


Get the Entity Schema Name in CRM
Xrm.Page.data.entity.getEntityName();


Get the UserRole Id’s in CRM
var userRoles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < userRoles.length; i++)
{
var userRole = userRoles[i]; // returns the Role Id
}


Get the Form Type in CRM
Xrm.Page.ui.getFormType();


Form Types in CRM
Is the user creating a new record?
Xrm.Page.ui.getFormType() == “1″


Is the user updating an existing record?
Xrm.Page.ui.getFormType() == “2″


Is the user unable to update this record?
Xrm.Page.ui.getFormType() == “3″


Is this record deactivated?
Xrm.Page.ui.getFormType() == “4″


Is the user using the Quick Create form?
Xrm.Page.ui.getFormType() == “5″


Is the user using the Bulk Edit form?
Xrm.Page.ui.getFormType() == “6″


Save a record in CRM
Xrm.Page.data.entity.save(); // for saving a record
Xrm.Page.data.entity.save(“saveandclose”); // for save and close
Xrm.Page.data.entity.save(“saveandnew”); // for save and new


Close the form in CRM
Xrm.Page.ui.close();



Useful functions

// Function to disable tabfunction tabdisable(tabname, disablestatus) {

var allSections = Xrm.Page.ui.tabs.get(tabname).sections.get(); //returns all the sections for the tab "tabname"

for (var i in allSections) {

var allCntrls = allSections[i].controls.get(); //get all the controls for this section.

for (var j in allCntrls) {

if (allCntrls[j].getName() != null && allCntrls[j].getName() != "asu_mdn") {

if (allCntrls[j].getControlType() == 'lookup' || allCntrls[j].getControlType() == 'standard' || allCntrls[j].getControlType() == 'optionset') {
allCntrls[j].setDisabled(disablestatus);
         }
      }
    }
  }
}
// Function to hide ribbon button
function HideRibbonButton(buttonID, action) {
var intervalId = self.setInterval(function () {

var button = window.top.document.getElementById(buttonID);

if (button != null) {

self.clearInterval(intervalId);

button.style.display = action;

}

}, 10);

}

// Function Disable Notes Contolfunction DisableNotesControl(val) {
var controlId = "notescontrol" // this is the default id of the notes control
   
 var iframe = document.getElementById(controlId);
if (iframe.readyState == "complete") {

DisableNotesControlContents(iframe, val); // this will be used once notes control is loaded and with complete state

}
// onreadystatechange works with IE but not other browsers

iframe.onreadystatechange =function () {
if (iframe.readyState == "complete") {
DisableNotesControlContents(iframe, val);

     }

   }

}

function DisableNotesControlContents(iframe, val) {
var controls = iframeDoc.getElementsByTagName("textarea");

 
if (controls != null) {

for (var i = 0; i < controls.length; i++) {

controls[i].disabled = val;

     }

   }

}


 
 

2 comments:

  1. I have tried all and they are working fine in MS CRM 2011 but I need to try these again in MS CRM 2013...
    Thanx for the help
    Looking forward for more Pradeep

    ReplyDelete