CRM 2011 Useful JScripts

Posted: May 12, 2012 in CRM 2011, Jscripts

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;

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();

Advertisements
Comments
  1. patafb80 says:

    Hello. I’m using the following code to go through all the controls in the form and set the submit mode:

    for (var i in attributes) {
    attributes[i].setSubmitMode(“always”);
    }

    but I need to avoid setting the submit mode on some fields based on their name, how can I get the field name from the attribute?, thanks.

  2. […] CRM 2011 Useful JScripts (Nice Post from Lakshman Reddy) […]

  3. Casper says:

    great page, helped me heaps already – thanks!!

  4. Paddy Byrne says:

    Excellent post, I’ll be using this as a quick reference.

    Thanks for sharing.

  5. XT says:

    Hi Lakshman,

    I want to Save the Form while Clicking on the Save button irrespective of modification or dirty form

  6. karunakar says:

    thanks u
    I need if condition for lookupfield and optionset fields

  7. hayercrm says:

    Great, thanks for sharing. I have this bookmarked this blog and don’t know how many times I have visited this in last one year.

  8. Bharath says:

    hi sir…
    i tried the same coding what you written tin this blog.. But i am not get the output….
    i got confusion. please can you tel me how to write the coding

  9. Bharath says:

    Hi sir,

    i try to disable the field so i am using this code Xrm.Page.ui.controls.get(“new_registernumber”).setDisabled(true);
    in form properties added this coding
    in Event Handlers what i chose onload or onsave
    if i chose onload i can’t create new record
    if i chose on save on the time only it show disable
    after i can reopen and change the value
    but i need after reopen also i dont want to change the value
    so how to write the coding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s