locked
Mapping ID from parent record RRS feed

  • Question

  • I have astructure in my CRM with client to account, account to collection.  When adding a collection record I would like the client ID that sits within it to auto popualate.  I am quite new to CRM so not up to speed with some of the basics just. yet.  Is it a case of going into customization of the collection entity and creting a n-1 relation back to the client entity or can I map it to the client ID field that sits in the account record instead?

    thanks

    Wednesday, September 24, 2014 11:42 AM

All replies

  • Hi Darren,

    Please explain your entity structures. Client to account relation and account to collection relation?

    you can write a javascript function to populate client on collection record.

    Regards,

    Saad

    Wednesday, September 24, 2014 11:52 AM
  • thanks Saad, one to many clitn to account and one many from account to collection...?

    Wednesday, September 24, 2014 12:48 PM
  • Hi Darren,

    Please create a lookup of client on your collection entity.

    Use the javascript on Collection to get client value from account entity using Odata and populate it on Collection Entity.

    Use the code below:

    function setClientLookup() {
        var serverUrl = "http://" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
        var Account = Xrm.Page.getAttribute("WriteAccountLookupOnCollection").getValue();
    
    
        if (Account != null) {
            var Accountid = Account[0].id;
    
            // Creating the Odata Endpoint
            var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
            var retrieveReq = new XMLHttpRequest();
            var Odata = oDataPath + "/AccountSet?$select=WriteYourClientFieldOnAccount&$filter=AccountId eq guid'" + Accountid + "'";
            retrieveReq.open("GET", Odata, false);
            retrieveReq.setRequestHeader("Accept", "application/json");
            retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            retrieveReq.send();
    
    
            if (retrieveReq.readyState == 4 /* complete */) {
                var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;
                var Client = retrieved.results[0].WriteYourClientFieldOnAccount;
                
    			if (Client.getValue()!= null)
    			{
    			var lookup = new Array();
    
                lookup[0] = new Object();
    
                lookup[0].id = Client.getValue()[0].id;
    
                lookup[0].name = Client.getValue()[0].name;
    
                lookup[0].entityType = Client.getValue()[0].entityType;
    			
    			 Xrm.Page.getAttribute("WriteClientLookupOnCollection").setValue(lookup);
    			
    			
    			}
    			
            }
    
        } 
    }

    Regards,

    Saad

    Thursday, September 25, 2014 5:08 AM