lookup field related data is set to lookup filed in same record



///////////lookup field related data is set in same record/////////////
function getCustomerLookupDetails() {
    var customerId = null;
    var billToId = null;
    var shipToId = null;
    var customerObject = null;
    var billToAddressObj = null;
    var shipToAddressObj = null;
    //var jobId = Xrm.Page.data.entity.getId();

    customerObject = Xrm.Page.data.entity.attributes.get("mtctb_customerid").getValue();
    billToAddressObj = Xrm.Page.getAttribute("mtctb_billtoaddress").getValue();
    shipToAddressObj = Xrm.Page.getAttribute("mtctb_shiptoaddress").getValue();

    if (customerObject != null) {
        if (customerObject.length == 0) {
            customerObject = null;
            return;
        }
    }

    if (billToAddressObj != null) {
        if (billToAddressObj.length == 0) {
            billToAddressObj = null;
            return;
        }
    }

    if (shipToAddressObj != null) {
        if (shipToAddressObj.length == 0) {
            shipToAddressObj = null;
            return;
        }
    }
/////////////giud id spliting process/////////////////
    if (customerObject != null)
        customerId = customerObject[0].id.replace("{", "").replace("}", "");

    if (billToAddressObj != null)
        billToId = billToAddressObj[0].id.replace("{", "").replace("}", "").toLowerCase();

    if (shipToAddressObj != null)
        shipToId = shipToAddressObj[0].id.replace("{", "").replace("}", "").toLowerCase();

        if (customerId != null) {
            XrmSvcToolkit.retrieve({
                entityName: "gr_arcustomer",
                id: customerId,
                select: ["gr_BillTo", "gr_ShipTo"],
                async: false,
                successCallback: function (result) {
////////////lookup value set to billToAddress and shipToAddress fields/////////////
                    if (billToId != result.gr_BillTo.Id.toLowerCase()) {
                        if (Xrm.Page.getAttribute("mtctb_billtoaddress") != null) {
                            var lookup = new Array();
                            lookup[0] = new Object();
                            lookup[0].id = result.gr_BillTo.Id;
                            lookup[0].name = result.gr_BillTo.Name;
                            lookup[0].entityType = result.gr_BillTo.LogicalName;
                            Xrm.Page.getAttribute("mtctb_billtoaddress").setValue(lookup);
                        }
                    }
                    if (shipToId != result.gr_ShipTo.Id.toLowerCase()) {
                        if (Xrm.Page.getAttribute("mtctb_shiptoaddress") != null) {
                            var lookup = new Array();
                            lookup[0] = new Object();
                            lookup[0].id = result.gr_ShipTo.Id;
                            lookup[0].name = result.gr_ShipTo.Name;
                            lookup[0].entityType = result.gr_ShipTo.LogicalName;
                            Xrm.Page.getAttribute("mtctb_shiptoaddress").setValue(lookup);
                        }
                    }
                },

                errorCallback: function (error) {
                    throw error;
                }
            });
        }
}

Comments

Popular posts from this blog

Basic Plugin Code in D365 using C#

CURD (Create, Update, Retrieve and Delete) Operation in D365 using Power Shell Script

Meta Data Using WebApiRequest