Thursday, October 15, 2015

An Complete Useful Xrm Reference Code(JavaScript)

var entityGuid=Xrm.Page.data.entity.getId();

Xrm.Page.getAttribute("AttributeSchemaName").getValue();

Xrm.Page.getAttribute("AttributeSchemaName").setValue("variable/field name");

Hide the fields
======================================================================
 Xrm.Page.getControl("mtctb_contacttnb").setVisible(false);
set requiredLevel on the behalf of field ======================================================================
Xrm.Page.getAttribute("fieldname").setRequiredLevel("none");
Xrm.Page.getAttribute("FIELD NAME").setRequiredLevel("required");
Xrm.Page.getAttribute("field name").setRequiredLevel("recommended");

set focus on the field
 ======================================================================
Xrm.Page.getControl("field name").setFocus(true);

force submit mode
 ======================================================================
Xrm.Page.getAttribute("field name").setSubmitMode("always");

Hide the tabs
 ======================================================================
Xrm.Page.ui.tabs.get("tab name").setVisible(true);
Xrm.Page.ui.tabs.get("tab name").setVisible(false);

hide the sections
 ======================================================================
Xrm.Page.ui.tabs.get("tag name").sections.get("section name").setVisible(true);
Xrm.Page.ui.tabs.get("tag name").sections.get("section name").setVisible(false);

changing the field display name
 ======================================================================
Xrm.Page.getControl("field name").setLabel("some text");

writing the innerText to a field:
 ======================================================================
document.getElementById("field name").innertText="some text";


Set focus:
 ======================================================================
Xrm.Page.getControl("attributename").setFocus(true);

Expand tab:
 ======================================================================
Xrm.Page.ui.tabs.get("tabname").setDisplayState('expanded');

Collapse tab:
 ======================================================================
Xrm.Page.ui.tabs.get("tabname").setDisplayState('collapsed');

Hiding Section:
======================================================================
Xrm.Page.ui.tabs.get(tabIndex).sections.get(sectionIndex).setVisible(false);
or
Xrm.Page.ui.tabs.get(tabIndex).sections.get("sectionName").setVisible(false);

Showing Section:
 ======================================================================
Xrm.Page.ui.tabs.get(tabIndex).sections.get(sectionIndex).setVisible(true);
or
Xrm.Page.ui.tabs.get(tabIndex).sections.get("sectionName").setVisible(true);
Hiding tab:
Xrm.Page.ui.tabs.get(tabindex).setVisible(false);
or
Xrm.Page.ui.tabs.get("tabname").setVisible(false);

Showing tab:
 ======================================================================
Xrm.Page.ui.tabs.get(tabindex).setVisible(true);
or
Xrm.Page.ui.tabs.get("tabname").setVisible(true);

Get changed data :
======================================================================
Xrm.Page.data.entity.getDataXml()

Current user roles:
 ======================================================================
var UserRoles = Xrm.Page.context.getUserRoles();

Retrieve all controls:
 ======================================================================
Xrm.Page.ui.controls.forEach( function (control, index) {
    var attribute = control.getAttribute();
    if (attribute != null) {
        var attributeName = attribute.getName();
    }
});

Refresh ribbon:
 ======================================================================
Xrm.Page.ui.refreshRibbon()

Attach event to attribute:
 ======================================================================
crmForm.all.new_isdue.attachEvent("onclick", Onisdueclick);
function  Onisdueclick () {
    alert('Hi');
}

Get Current user id:
 ======================================================================
var userID = Xrm.Page.context.getUserId();

Save :function:
 ======================================================================
Xrm.Page.data.entity.save();

Save and Close function:
 ======================================================================
Xrm.Page.data.entity.save("saveandclose");

Save and New function:
 ======================================================================
Xrm.Page.data.entity.save("saveandnew");

Close function:
 ======================================================================
Xrm.Page.ui.close();

Get from type:
 ======================================================================
 var type = Xrm.Page.ui.getFormType();

 getFromType() function returns integer value for different Form states
 0 - undefined
 1 - Create
 2 - Update
 3 - Read Only
 4 - Disabled
 5 - Quick Create (Deprecated)
 6 - Bulk Edit

Get disable control:
 ======================================================================
  var isdisable = Xrm.Page.ui.controls.get("attributename").getDisabled();
     True value shows disable field

Set control disable:
 -------------------------
  Xrm.Page.ui.controls.get("attributename").setDisabled(true);

Get current record id:
 ======================================================================
var Id = Xrm.Page.data.entity.getId();

Get server URL:
 ======================================================================
var serverURL = Xrm.Page.context.getServerUrl();

iframe access
 ======================================================================
var IFrame = Xrm.Page.ui.controls.get("IFRAME_test");
var Url = IFrame.getSrc();

Get current organization name:
 ======================================================================
var orgName = Xrm.Page.context.getOrgUniqueName();


Check modified Form using javascript in CRM 2011
In this article , I am going to explain how to check ,
form is modified or not using getIsDirty() function.
This function will return True in case of form is modified

Get modified form:
= =====================================================================
var ismodified = Xrm.Page.data.entity.getIsDirty();


Get current entity name:
 ======================================================================
var entity = Xrm.Page.data.entity.getEntityName();

OptionSet Retrieve
=======================================================================
var label = Xrm.Page.getControl("atrributename").getLabel()
var optionset = Xrm.Page.getAttribute("attributename").getValue();
var text = optionset.getText();
var value = optionset.getValue();

Requirement level setting:
 ======================================================================
Xrm.Page.getAttribute("attributename").setRequiredLevel("none");
Xrm.Page.getAttribute("attributename").setRequiredLevel("required");
Xrm.Page.getAttribute("attributename").setRequiredLevel("recommended");

Get a lookup value:
 ======================================================================
var lookup = new Array();
lookup = Xrm.Page.getAttribute("attributename").getValue();
if (lookup != null) {
var name = lookup[0].name;
var id = lookup[0].id;
var entityType = lookup[0].entityType;
}

Set a lookup value:
 ======================================================================
var lookup = new Array();
lookup[0] = new Object();
lookup[0].id = recorid;
lookup[0].name = recordname;
lookup[0].entityType = entityname;
Xrm.Page.getAttribute("attributename").setValue(lookup);

Alternate method to set lookup value:
 ======================================================================
Xrm.Page.getAttribute("attributename").setValue([{ id: recorid, name: recordname, entityType: entityname}]);

Get Attribute Value:
 ======================================================================
var name = Xrm.Page.getAttribute("attributename").getValue();

Set Attribute Value:
 ======================================================================
Xrm.Page.getAttribute("attributename").setValue('Navish');

Street1 = Xrm.Page.getAttribute("address1_line1").getValue();
                     
 ======================================================================
1.Save record embedded in iframe

Account form with iframe, that displays contact form. Requirement is that when user press “save” button on account form, contact in iframe should be also saved.

 var iframeXrmPage = Xrm.Page.getControl(“IFRAME_contact”).getObject().contentWindow.contentIFrame.Xrm.Page;

No comments:

Post a Comment