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
Tony WileyTony Wiley 

Auto-populate Account lookup field based on another field

On the Case object we have the standard lookup field to the standard Account object. I also added a field called "Login_ID__c". We have this same field (Login_ID__c) in the Account object as well. Now when the user enters a Login ID in the case object, based on the value in the Login ID field it should auto populate the Account lookup field because each Account record has a unique Login ID. I have tried using a workflow and process builder but to no avail. From what I’ve researched, I think it’s possible to do this with an apex trigger but I am not familiar with apex at all. Can anybody help me out or offer any insight? Thank you.
 
Best Answer chosen by Tony Wiley
v varaprasadv varaprasad
Hi Tony,

Please check once below sample code : 
 
trigger updateAccount on Case(before insert,before update){
   
   set<string> logids = new set<string>();
   for(case cs : trigger.new){
       if(cs.Login_ID__c != null)
	   logids.add(cs.Login_ID__c);   
   }
   
  account acc = [select id,Login_ID__c from account where Login_ID__c in : logids limit 1];
 
  if(acc != null){
    for(case cs : trigger.new){
	    cs.accountid = acc.id;
	}
  
  
  }
  


}



Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/


Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1



 

All Answers

v varaprasadv varaprasad
Hi Tony,

Please check once below sample code : 
 
trigger updateAccount on Case(before insert,before update){
   
   set<string> logids = new set<string>();
   for(case cs : trigger.new){
       if(cs.Login_ID__c != null)
	   logids.add(cs.Login_ID__c);   
   }
   
  account acc = [select id,Login_ID__c from account where Login_ID__c in : logids limit 1];
 
  if(acc != null){
    for(case cs : trigger.new){
	    cs.accountid = acc.id;
	}
  
  
  }
  


}



Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/


Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1



 
This was selected as the best answer
Tony WileyTony Wiley
Hello,

Thanks for the quick reply, the code worked perfectly as intended. I have one more question though, every once in a while we have someone call into our support team who is a client of a client (so not actually our client but will be using our software to interact with our client) and therefore will not have an account with us but will have a Login ID to login to our system. Is it too much trouble to modify the code a bit to have it check to see first if there is an Account record with the given Login ID that’s typed into the case and if not, have it NOT run the trigger? What’s happening currently is when you try to create a case with a Login ID that is not associated with an Account record, the case creation errors out and doesn’t allow you to create the case. Otherwise it works perfectly.

Thanks a bunch,
Tony
 
v varaprasadv varaprasad
Hi Tony,

If Login_ID__c is there in account it will update on the case. Otherwise, it will be null.
it will not through any error.


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 
Tony WileyTony Wiley
I figured out that the error I was getting was from another trigger that was in the case object. Everything works as expected now thanks to your code. 

Thanks for your help,
Tony
 
Rod M. TylerRod M. Tyler
So I am trying to do something similar. I have a user lookup field on the Account object that holds a Service Manager name. The Service Manager is assigned by the ShippingPostalCode of the account. I was going to make an object to hold the zip code to service manager assignments and then update the account based on the matching zip.