+ Start a Discussion
sfdclearnsfdclearn 

Updating a look up field with the help of a trigger

Hi

 

   I am trying to copy the value of a text field(Contact__C)in a custom object to a look up field(TPF_Primary_Contact__c) in a standard object with the following trigger.

 

trigger ContactUpdateonAccount on Customcontact__c(after insert,after update)
{
Map<Id, Customcontact__c> mapcustomcontactByAccountId = new Map<Id, Customcontact__c>();
for (Customcontact__c objcustomcontact: Trigger.new)
{
mapcustomcontactByAccountId.put(objcustomcontact.Account__c,objcustomcontact);
}

if( mapcustomcontactByAccountId.size() > 0)
{
List<Account> lstAccounts = [Select TPF_Primary_Contact__c from Account where ID IN : mapcustomcontactByAccountId.keySet()];
if(!lstAccounts.isEmpty())
{
for(Account objAccount : lstAccounts)
{
Customcontact__c objcustomcontact = mapcustomcontactByAccountId.get(objAccount.Id);
objAccount.TPF_Primary_Contact__c=objcustomcontact.Contact__C;

update objAccount;

}
}
}
}

 

When I use the trigger to update a text field in the standard object it works. But when i try to update look up field with the same trigger it doesn't allow me to save the record and throws the following error when the record in the custom object is saved.

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ContactUpdateonAccount caused an unexpected exception, contact your administrator: ContactUpdateonAccount: execution of AfterInsert caused by: System.StringException: Invalid id: testc: Trigger.ContactUpdateonAccount: line 17, column 1

 

Please advise.

 

Thanks!

pujapuja

You need to make the Contact__C field as a lookup ,because the lookup field needs  Id not text.

 

 

RoyGiladRoyGilad

Adding to what puja wrote earlier, the lookup field is expecting record ID but that's not what you have in your test field , in your example you have the string 'testc' in this field and it causes an error.

 

Plus, In a more general aspect regarding your code,

The DML (the update query) should be outside the loop, otherwise you will hit governor limit when you work with batch

sfdclearnsfdclearn

The code works when the contact__c (source field in the  custom object ) is changed to a look up field. But in the original scenario the text field in the custom object is copied to the look up field in the standard object. Is that possible in any ways to copy the custom field ( in a custom object) which is a text field to a look up field.

 

please advise.

 

Thanks!!!

RoyGiladRoyGilad

When writing the name in a lookup field SF converts the value to a record SF ID (you can inspect the element using firebug of Chrome developer, and see the value in the '_lnk' value).

When you enter the same value in a text area you need to convert the value to SF id explicitly , by for example qurying the db for records with the name inserted and then pass the value to the lookup field.

 

In any case, in Apex the lookup field is expecting the record SF ID, and not the name of the record.

 

Roy

sfdclearnsfdclearn

Could you please explain in detail how i can do this?

 

Thanks in advance for your help!