Link to home
Start Free TrialLog in
Avatar of IT_Techno
IT_TechnoFlag for South Africa

asked on

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;
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

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; 
        Xrm.Page.getAttribute(fieldName).setValue(lookupValue); 
    } 
} 

Open in new window


I have taken the reference from here:
http://gtcrm.wordpress.com/2011/08/23/defaulting-a-lookup-field-via-a-rest-query-at-form-load/

Regards,
Chinmay.
ASKER CERTIFIED SOLUTION
Avatar of IT_Techno
IT_Techno
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IT_Techno

ASKER

Discovered error myself.