Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 9244

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

$
0
0

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
     $.ajax({
  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");
  }
     });
}


Viewing all articles
Browse latest Browse all 9244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>