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
BigPBigP 

Can someone please help me creating this apex trigger?

We must have a Job object (Job__c) created that will have a Status picklist field with values Budget, Brief, First presentation, Backlog, Closed, Lost. If we have the object Job that is linked to an account, and this account does not have a Sales Partner (it is empty) but the Job has a Sales Partner field , then when the Job goes to Backlog status, the linked Account that had a sales partner, will receive as Sales Partner that Job has.
Best Answer chosen by BigP
AnkaiahAnkaiah (Salesforce Developers) 
Hi ,

Try with below trigger code. 

Replace the object API names and field API names as per your org.
 
Trigger SalespartnerUpdate on Job__c(after insert, After Update){

Map<id, id> accwithspmap = new map<id,id>();

for(job__c j:trigger.New){
if(j.Account__c!=Null && j.Sales_Partner__c!=Null){
accwithspmap.put(j.Account__c,j.Sales_Partner__c);
}
}
List<Account> acclistupdate = new List<Account>();
for(Account acclist: [Select id,Sales_Partner__c from account where id=:accwithspmap.keyset()]){

If(accwithspmap.containskey(acclist.id)){
acclist.Sales_Partner__c = accwithspmap.get(acclist.id);
acclistupdate.add(acclist);
}

}

Update acclistupdate;



}

Let me know if any issues.

If this helps, Please mark it as best answer.

Thanks!!
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi,

What is the data type of Sales Partner field in Account & job__c object?

as per my understanding, when job record created and its linked to account, if account dont have the value for Sales Partner field then it should autopopulated from job record to account record? Am i correct.


Thanks!!
BigPBigP
Hi
Sales Partner should be lookup user data type.
Yes it should autopopulate from job record to account record.
AnkaiahAnkaiah (Salesforce Developers) 
Hi ,

Try with below trigger code. 

Replace the object API names and field API names as per your org.
 
Trigger SalespartnerUpdate on Job__c(after insert, After Update){

Map<id, id> accwithspmap = new map<id,id>();

for(job__c j:trigger.New){
if(j.Account__c!=Null && j.Sales_Partner__c!=Null){
accwithspmap.put(j.Account__c,j.Sales_Partner__c);
}
}
List<Account> acclistupdate = new List<Account>();
for(Account acclist: [Select id,Sales_Partner__c from account where id=:accwithspmap.keyset()]){

If(accwithspmap.containskey(acclist.id)){
acclist.Sales_Partner__c = accwithspmap.get(acclist.id);
acclistupdate.add(acclist);
}

}

Update acclistupdate;



}

Let me know if any issues.

If this helps, Please mark it as best answer.

Thanks!!
 
This was selected as the best answer
BigPBigP
Can you explain your code please?