• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1894
  • Last Modified:

Using REST to create CRM record with Lookup Field

Hi, I managed to review a few blogs on using REST in Java to create records in CRM 2011. I can create the record and pass text fields along to be created with the new record by assigning values to the Object array, this works fine. Problem I have is that I need to populate a few lookup fields in the new record.

I keep getting an error when I try to create records with the lookup value, my code is below, I am running from a Custom form called "new_job" and I am creating a record called "new_tensiletest". I want the lookup field in "new_tensile" to populate with the "new_job". Examples I have seen all revolve around Account and Contact.

I am not sure I am passing the right parameters with the right names.
    var GUIDvalue = Xrm.Page.data.entity.getId();

    var CRMObject = new Object();
    var SampleJob = new Object();
    var SampleName = Xrm.Page.data.entity.attributes.get("new_name").getValue();

    SampleJob.Id = GUIDvalue;
    SampleJob.LogicalName = "new_job";
    SampleJob.Name = SampleName;

    CRMObject.new_tensileid = SampleJob;
  • 2
1 Solution
Chinmay PatelEnterprise ArchitectCommented:
Hi IT_Techno,

Can you describe the error you are getting?

Also try using this function to setup a lookup field

// Set lookup value of a field 
function SetLookupValue(fieldName, id, name, entityType) { 
    if (fieldName != null) { 
        var lookupValue = new Array(); 
        lookupValue[0] = new Object(); 
        lookupValue[0].id = id; 
        lookupValue[0].name = name; 
        lookupValue[0].entityType = entityType; 

Open in new window

I have taken the reference from here:

IT_TechnoAuthor Commented:
Hi Chinmay, thanks for your ref, but is more related to a field that is on the form loaded, I am creating a new record altogether which is not loaded and setting the lookup value of a field in the new record.

I managed to resolve my issue, it was case sensivity. I was referencing the Field Name and not the Schema Name of the field. I also found an easier way to pass values into the array object.

CRMObject.new_TensileId = { Id: GUIDvalue, LogicalName: "new_job" };

If I get the Schema Field name wrong, then it was giving me an error. resolve it, now working. I am obviosly new to this and feeling my way through.
IT_TechnoAuthor Commented:
Discovered error myself.

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now