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
John Neilan 2John Neilan 2 

Update Lookup Field with Text ID

Hello,

I have what I think is a very simple request, but I'm struggling with it.  I have a custom field on the Case object called Account ID, which is a text field.  I also have a field called Primary Account, which is a lookup field.  I have a Google form integration that creates a Case when a form is submitted and populates the Account ID with the 15-character Account ID, input by the form submitter.  I want to create a trigger to take that value and populate the Primary Account lookup field with the Account associated with the ID.  Can anyone point me to code that can accomplish this?  Thanks,
Best Answer chosen by John Neilan 2
JeffreyStevensJeffreyStevens
Okay - then I think you've got to do an AFTER INSERT trigger.  You'll have to re-soql the case - as you won't be able to update the instance of the record that fired the trigger.
 
trigger updateParentId on Case (after insert) [

  list<Case> casesToUpdate = new list<Case>([SELECT id,AccountId__c,PrimaryAccount__c FROM Case WHERE Id IN :trigger.newmap.keyset()]);

  for(Case c :casesToUpdate) {
    c.AccountId__c = c.PrimaryAccount__c;
  }

  if(casesToUpdate.size()>0) {
    update casesToUpdate;
  }

}

 

All Answers

JeffreyStevensJeffreyStevens
I think a workflow rule might do it better.  

If you do a trigger - then it'd have to be an after insert trigger.  And then you'd have to update the record again.
Vivek DVivek D
Hi John,
If the account id exist in SF then it is simple just pass that id to Parent AccountID in before insert. Use try catch in case there is error or that Id in not available in SF.
You can use process builder also but since you asked for trigger
// Before insert 
for(Case cse : Trigger.New){
   cse.ParentAccountID = cse.AccountId ; // From google
}

 
John Neilan 2John Neilan 2
@JeffreyStevens - I thought of that, but the Primary Account field is not available for a Field Update.

@Vivek - The ID does not exist on the Case until the Case is created, and it's created as a text value.  I did try a Process, but it was not updating the lookup field.
JeffreyStevensJeffreyStevens
Okay - then I think you've got to do an AFTER INSERT trigger.  You'll have to re-soql the case - as you won't be able to update the instance of the record that fired the trigger.
 
trigger updateParentId on Case (after insert) [

  list<Case> casesToUpdate = new list<Case>([SELECT id,AccountId__c,PrimaryAccount__c FROM Case WHERE Id IN :trigger.newmap.keyset()]);

  for(Case c :casesToUpdate) {
    c.AccountId__c = c.PrimaryAccount__c;
  }

  if(casesToUpdate.size()>0) {
    update casesToUpdate;
  }

}

 
This was selected as the best answer
John Neilan 2John Neilan 2
Thanks!  That helped a lot!