function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFNewSFNew 

Can Java code upate a lookup field

Hello,

        I would like to know if a WS client using API can update a lookup field in a custom object. So far I've searched the forum for a solution since I've been fighting with it for a while. I try to lookup Contacts from a Account object and assign it to a custom object that has a lookup field Contact__c if its already null,

 

This is the sequence I am doing,

 

1. I check if Contact__c is null in the object custom__c

2.  if null, I query the parent object Account for the Contacts

3. I get the Contacts in the Account and try to set the custom__c's lookup field as

     -  custom__cObj.setId(id)

     -  custom__cObj.Contact__r((Contact) qr.getRecords[i])

     -  binding.update(new SObject[] {custom__cObj})

 

where qr.getRecords[i] is a Contact.

Most people in the board talk about a trigger populating a lookup field, I am just wondering what I am doing wrong here. My code does not error out, this makes the situation worser. I am just not able to update the lookup Contact field. Could anyone please help me out on this. Thanks a lot.

   

 

SteveBowerSteveBower

Hi, some thoughts:

 

1. put your code in a try / catch block and see if you're throwing any exceptions.

 

2. capture the return value from binging.update() and see if you got success, or some error.

 

3. I've not tried assigning a lookup via the _r variable.  I'd try:

 

custom_cObj.Contact__c = qr.getRecords[i].id;  // Set the ID value for the lookup directly.

 

Best, Steve.

SuperfellSuperfell
set the contact__c field to the contact's 18 char Id value, no need to mess around with nested objects.