Use jquery/json to create contact record and create account lookup RRS feed

  • Question

  • I have a custom entity, Machine, that has lookup field to select a contact associated with the machine record.  If the contact does not exist the user has a couple of fields to enter First Name, Last Name & Phone then they click a button that creates a new contact in the background and automatically associates the new contact to the machine record.  This all works fine.

    The machine record also has an account lookup field that will have the account filled in when the machine record was created.  I would like to dynamically "pass" this account to the contact record that gets created, but I am having a problem with it.

    This works:

    CRMObject.ParentCustomerId = { Id: '{9E54CFAF-6C63-E211-BCE1-782BCB48F57D}', LogicalName: "account", Name: '1 pat mann' };

    This does not:

    CRMObject.ParentCustomerId = { Id: custid, LogicalName: “account”, Name: custname };

    How can I dynamically pass a currently filled in lookup on one entity to lookup on a new record?

    function createContact() {
     var custlookup = [];   // get account info from current machine record
     custlookup = Xrm.Page.getAttribute("ph_customerid").getValue();  
     if (custlookup != null)   
     var custname = custlookup[0].name;   
     var custid = custlookup[0].id;    
         // Get the CRM URL
         var serverUrl = Xrm.Page.context.getServerUrl();
      // Cater for URL differences between on premise and online
         if (serverUrl.match(/\/$/)) {
      serverUrl = serverUrl.substring(0, serverUrl.length - 1);
      // Specify the ODATA end point (this is the same for all CRM 2011 implementations)
         var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
      // Specify the ODATA entity collection
         var ODATA_EntityCollection = "/ContactSet";
      // Define an object for the CRM record you want created
         var CRMObject = new Object();
      // Define attribute values for the CRM object
    CRMObject.ParentCustomerId = { Id: custid, LogicalName: “account”, Name: custname }; // this errors
    // CRMObject.ParentCustomerId = { Id: '{9E54CFAF-6C63-E211-BCE1-782BCB48F57D}', LogicalName: "account", Name: '1 pat mann' }; // this works
         CRMObject.FirstName = "John";
         CRMObject.LastName = "Doe";
         CRMObject.FullName = "John Doe";
      //Parse the entity object into JSON
         var jsonEntity = window.JSON.stringify(CRMObject);
      //Asynchronous AJAX function to Create a CRM record using OData
      type: "POST",
      contentType: "application/json; charset=utf-8",
      datatype: "json",
      url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection,
      data: jsonEntity,
      beforeSend: function (XMLHttpRequest) {
      //Specifying this header ensures that the results will be returned as JSON.
      XMLHttpRequest.setRequestHeader("Accept", "application/json");
      success: function (data, textStatus, XmlHttpRequest) {
      //This function will trigger asynchronously if the Retrieve was successful
    // alert("ajax call successful");
      var NewCRMRecordCreated = data["d"];
    //  var FullName = Xrm.Page.getAttribute("new_firstname").getValue() + " " + Xrm.Page.getAttribute("new_lastname").getValue();             
                          var FullName = "Zig Zigler";
                               // set Service Contact field
                               SetLookupValue("new_servicecontact", NewCRMRecordCreated.ContactId, FullName, "contact");
           // set Parent Customer field on new contact record
    //       updateContact(NewCRMRecordCreated.ContactId, "contact", "ContactSet");
      error: function (XmlHttpRequest, textStatus, errorThrown) {
      //This function will trigger asynchronously if the Retrieve returned an error
      alert("ajax call failed");

    Thursday, September 26, 2013 3:34 AM

All replies

  • Hi,

    What if you enclose your variable value custid with string quote ' like:

    var newCustId = ''+custid+'';

    CRMObject.ParentCustomerId = { Id: newCustId , LogicalName: account”, Name: custname };


    CRMObject.ParentCustomerId = { Id: ''+custid +'', LogicalName: account”, Name: custname };

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" Thanks, Imran Abbasi

    Thursday, September 26, 2013 4:16 AM
  • Imran,

    I just tried that it and it did not work.  I tried both single and double quotes. 

    For some reason the lookup logic does not like to use variables for the parameters, which makes it almost useless. 

    I have searched endlessly but could not find any examples where someone dynamically set a lookup field without hard coding the ID in.

    Thursday, September 26, 2013 4:55 AM
  • Code from the SDK provides some hint to this

    var retrievedRecord = JSON.parse(oReq.responseText).d.results[0];

       var olookup = {};
       olookup.id = "{" + retrievedRecord[idAttribute] + "}";
       olookup.entityType = entityName;
       olookup.name = retrievedRecord[nameAttribute];
       var olookupValue = [];
       olookupValue[0] = olookup;

    so enclose the value in "{" and that should work.



    Dynamics CRM MVP | Inogic | http://inogic.blogspot.com| news at inogic dot com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Proposed as answer by Sam - Inogic Thursday, September 26, 2013 5:17 AM
    Thursday, September 26, 2013 5:16 AM
  • Sam,

    Thanks but this did not work either.  I believe this is doing the opposite of what I need.  This is setting a lookup field in an open record from a record that has been retrieved in the background.  I need to set a lookup in a record I have retrieved, or created, in the background from a lookup field in an open record.

    I hope that makes sense.

    Thursday, September 26, 2013 12:39 PM