Controlling Fields and Tabs using java script

function controllingFieldsAndTabs()
{
    debugger;
    Xrm.Page.ui.controls.get("parentaccountid").setVisible(false);
    //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("parentaccountid").setDisabled(false);
    Xrm.Page.getControl("parentaccountid").setDisabled(true);

    ///Hide CRM field
    alert("Hai1");
    if (Xrm.Page.ui.getFormType() == 1)
    {
        alert("hai");
        Xrm.Page.ui.controls.get("primarycontactid").setVisible(false);
    }

    //Hide a Tab in CRM
    Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(false);

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

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

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

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

    //Check IsDirty in CRM field
    var isDirty = Xrm.Page.getAttribute(“parentaccountid”).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(“parentaccountid”).setSubmitMode(“always”);

    //Preventing an attribute to be saved in CRM form
    Xrm.Page.getAttribute(“parentaccountid”).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();

}

1. Xrm.Page.context.client.getClient();

this code snippet will returns the results about client details. whether Web, Outlook or Mobile.

2. Xrm.Page.context.client.getClientState();

this code will return the client state. possible return values are; For web client  it will returns always "Online".  For outlook and mobile it can be either "Online" or "Offline"

3. Xrm.Page.context.client.getFormFactor();

this getFormFactor() will returns integer value which will indicate what kind of devices user is using.

0  - Unknown
1 - Desktop
2 - Tablet
3 - Phone

4. Xrm.Page.context.getClientUrl();

this method will returns the base url of current application.

5. Xrm.Page.context.getCurrentTheme();

this method will returns the current Microsoft Office Outlook theme using by the user.this function mostly userful for client type Outlook. for web application this function will returns "default".

6. Xrm.Page.context.getIsAutoSaveEnabled();

this function will return boolean variable based on Auto Save function is enabled or not.

7. Xrm.Page.context.getOrgLcid();

this will returns the base language of the organization. For english its values is 1033.

8. Xrm.Page.context.getOrgUniqueName();

this function will returns the current organization name.

9. Xrm.Page.context.getQueryStringParameters();

this function will returns an object which contains all the query string parameters of the form contains.

10. Xrm.Page.context.getTimeZoneOffsetMinutes();

this function will returns the minutes difference between local time to UTC time.

11. Xrm.Page.context.getUserId();

this function will returns the guid of an user who logged into the application.

12. Xrm.Page.context.getUserLcid();

this function will returns the base language user has been selected.

13. Xrm.Page.context.getUserName();

this function will returns the name of the user who logged into the application.

14. Xrm.Page.context.getUserRoles();

this function will returns the array of roles assigned to the current user.

Comments

Popular posts from this blog

Meta Data Using WebApiRequest

Sample CRUD Operations Using C# in MSCRM

Basic Plugin Code in D365 using C#