Menu Close

RealTime Scenario

1. > Create 4 contact record. (A,B,C,D)
2. > 3(B,C,D) Contact record will be child of 1(A-Parent)
3. > When ever A-Parent information will be changed should be refelected to their respective child also. -plugin/Jscript
4. > Selection of parent lookup — Data wil be fetch through Jscript OData.
Description
———–
Optionset value -> Parent/Child

[ If parent is selected from parent/child field then parent field and parent section fields parentemail and parentphonenumber will be disabled]
[ if child is selected from parent/child field then parent field should be required field and the parent section fields parentemail and parentphonenumber should be
auto populated from parent record].

Create new entity Donation
create fields
————-
parent/child = optionset = parent, child(business required field)
parent = lookup to itself
emailaddress = text field
phonenumber = phonenumber
gender = optionset= male/female
companyname = text field
designation = text field
city = text field
country = lookup to country entity//(in the country entity only add country field)

CREATE NEW SECTION CALLED PARENT SECTION LABEL(TWO COLUMN)
Fields
—–
parentemail = text field
parentphonenumber = text field

======================================================================
Javascript  is as follows:

function FormOnLoad() {
    /*For Type
    0 -Undefined
    c  1- Create
    u 2-Update
    r 3-ReadOnly
    d 4 -Disabled
    */
    var type = Xrm.Page.ui.getFormType();
    Xrm.Page.getControl(“new_parentemail”).setDisabled(true);
    Xrm.Page.getControl(“new_parentphonenumber”).setDisabled(true);
    if (type == 1) {//Create Mode
        //TODO : by default optionset value will be parent and parent lookup should be disabled.
        var optionSet = Xrm.Page.data.entity.attributes.get(“new_parentchildoptionset”);
        optionSet.setValue(100000000);
        Xrm.Page.getControl(“new_parent”).setDisabled(true);

    } else {
        var parentChildOptionSet = Xrm.Page.data.entity.attributes.get(“new_parentchildoptionset”);
        if (parentChildOptionSet != null) {
            var optionsetvalue = parentChildOptionSet.getValue();
            // this for parent
            if (optionsetvalue == 100000000) {
                Xrm.Page.getAttribute(“new_parent”).setRequiredLevel(“none”);
                Xrm.Page.getControl(“new_parent”).setDisabled(true);
             
            } else if (optionsetvalue == 100000001) {
                Xrm.Page.getControl(“new_parent”).setDisabled(false);
                //Setting the required filed
                Xrm.Page.getAttribute(“new_parent”).setRequiredLevel(“required”);
            }
        }
    }

}
function OnChange_ParentChildOptionSet() {
   
    var optionsetvalues = Xrm.Page.data.entity.attributes.get(“new_parentchildoptionset”);
    var optionsetvalue = optionsetvalues.getValue();
    // this for parent
    if (optionsetvalue == 100000000) {
        Xrm.Page.getAttribute(“new_parent”).setRequiredLevel(“none”);

        Xrm.Page.getControl(“new_parent”).setDisabled(true);

    } else if (optionsetvalue == 100000001) {//Child
        Xrm.Page.getControl(“new_parent”).setDisabled(false);
        Xrm.Page.getAttribute(“new_parent”).setRequiredLevel(“required”);
    }

}
function OnChange_ParentLookup() {
    var parentlookup = Xrm.Page.getAttribute(“new_parent”).getValue();
    if (parentlookup != null) {
        var lookupId = parentlookup[0].id;
        var CrmServerUrl;
        if (typeof GetGlobalContext != “undefined”) {
            CrmServerUrl = GetGlobalContext().getClientUrl();
        } else {
            if (typeof Xrm != “undefined”) {
                CrmServerUrl = Xrm.Page.context.getClientUrl();
            } else {
                alert(“Context is not available.”);
            }
        }
        if (CrmServerUrl.match(//$/)) {
            CrmServerUrl = CrmServerUrl.substring(0, Crm.length – 1);
        }

        //REST SERVICE WITH ODATA.
        var oDataPath = CrmServerUrl + “/XRMServices/2011/OrganizationData.svc”;
        var fetchData = new XMLHttpRequest();
        var Odata = oDataPath + “/new_donationSet?$select=new_phonenumber,EmailAddress&$filter=new_donationId eq (guid'” + lookupId + “‘)”;
        fetchData.open(“GET”, Odata, false);
        fetchData.setRequestHeader(“Accept”, “application/json”);
        fetchData.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
        fetchData.onreadystatechange = function () { retrieveParentDetails(this); };
        fetchData.send();

    }
}
retrieveParentDetails = function (fetchData) {
    if (fetchData.readyState == 4) {
     
        var fetchD = this.parent.JSON.parse(fetchData.responseText).d;
       
        if (fetchD.results.length > 0) {
            var phoneNumber = fetchD.results[0].new_phonenumber;
            var email = fetchD.results[0].EmailAddress;
            Xrm.Page.getAttribute(“new_parentphonenumber”).setValue(phoneNumber);
            Xrm.Page.getAttribute(“new_parentemail”).setValue(email);
        }
    }
}

Share this:
Posted in Blog

Related Posts