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
The NewbieThe Newbie 

Autopopulation of lookup fields

Hello Community,

I need help with autopopulation of lookup fields

Have to auto populate Asignment and Account  based on Part number and Case Orgin

Asignment__c (Lookup)
Account__c (Lookup)
If Case Orgin is mechanical and Part Number is available then should autopopulate the above two fields
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

How you would know, which account & assignment should auto populate?

Thanks!!
The NewbieThe Newbie
Account and Assignment both have the store the same serial number
AnkaiahAnkaiah (Salesforce Developers) 
That means,

Case object have part number and same part number available in Account and assignment object ?

Thanks!!
The NewbieThe Newbie
Yes sir
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

try with below code and change the field API Names as per your org.
Trigger Accountandassignmentupdate on Case(before insert,before update){

set<string> partnumber = new set<string>();

for(case cs:trigger.new){

if(cs.Part_Number__c != Null && cs.Orgin=='mechanical'){
partnumber.add(Part_Number__c);
}
}

map<string,id> partnumberwithaccountid = new map<string,id>();
map<string,id> partnumberwithassignmentid = new map<string,id>();

for(Account acc:[select id,Part_Number__c from account where Part_Number__c:=partnumber]){

partnumberwithaccountid.put(acc.Part_Number__c,acc.id);
}

for(assignment__c ass:[select id,Part_Number__c from assignment__c where Part_Number__c:=partnumber]){

partnumberwithaccountid.put(ass.Part_Number__c,ass.id);
}

for(case cs:trigger.new){

if(partnumberwithaccountid.containskey(cs.Part_Number__c) && partnumberwithassignmentid.containskey(cs.Part_Number__c) ){
cs.accountid = partnumberwithaccountid.get(cs.Part_Number__c);
cs.assignment__c = partnumberwithassignmentid.get(cs.Part_Number__c);
}
}

}

If this helps, Please mark it as best answer.

Thanks!!
The NewbieThe Newbie
Can it done with a flow?