Monday, February 21, 2011

CRM2011 java script samples

Here are few basic CRM2011 java script samples

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

Set the value of a CRM field
Xrm.Page.getAttribute(“po_CRMFieldSchemaName”).setValue(‘My New Value’);


// Hiding An Attribute
Xrm.Page.ui.controls.get("ownerid").setVisible(false);


Hide/Show a tab/section
Xrm.Page.ui.tabs.get(5).setVisible(false);
Xrm.Page.ui.tabs.get(5).setVisible(true);
 Xrm.Page.ui.tabs.get(tabNumber).sections.get(sectionNumber).setVisible(false);
 Xrm.Page.ui.tabs.get(tabNumber).sections.get(sectionNumber).setVisible(true);

Call the onchange event of a field
Xrm.Page.getAttribute(“CRMFieldSchemaName”).fireOnChange();

Get the selected value of picklist
Xrm.Page.getAttribute(“CRMFieldSchemaName”).getSelectedOption().text;

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

Set the focus to a field
Xrm.Page.getControl(“CRMFieldSchemaName”).setFocus(true);



// Hide Left Navigation link
Xrm.Page.ui.navigation.items.get("navContact").setVisible(false);


// Hide Left Navigation Pane
document.getElementById("crmNavBar").parentElement.style.display = "none";


Stop an on save event
event.returnValue = false;

Return array of strings of users security role GUIDs:
Xrm.Page.context.getUserRoles()


// Retrieving Object Type Code of Entity, etc = Entity Type Code
var etc = Xrm.Page.context.getQueryStringParameters().etc;



// Set lookup value to a field
function SetLookupValue(fieldName, id, name, entityType) {
if(fieldName != null) {
var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = id;
lookupValue[0].name = name;
lookupValue[0].entityType = entityType;

Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
}
}

// usage
SetLookupValue("new_field", "GUID", "Name", "new_entityname");

//Getting parent window context in child window
var parentcontext = window.top.opener.parent.Xrm.Page.context;


//Getting Parent window Object type code
var parobjcode = window.top.opener.parent.Xrm.Page.context.getQueryStringParameters().etc;

// get parent  Entity fields data
var parentFieldData = window.top.opener.parent.Xrm.Page.getAttribute("crmfield").getValue();

// Get the Server url with org Name
var serverAndOrgUrl = Xrm.Page.context.getServerUrl();
// Get the Current Organization Unique Name
var OrgName ="/"+Xrm.Page.context.getOrgUniqueName();

1 comment: