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 explain this code and what its purpose?

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.SalesPartner__c!=Null){
accwithspmap.put(j.Account__c,j.SalesPartner__c);
}
}
List<Account> acclistupdate = new List<Account>();
for(Account acclist: [Select id,SalesPartner__c from account where id=:accwithspmap.keyset()]){

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

}

Update acclistupdate;



}
Best Answer chosen by BigP
AnkaiahAnkaiah (Salesforce Developers) 
Hi Enea,
 
//This trigger will fire whenever account created or updated
Trigger SalespartnerUpdate on Job__c(after insert, After Update){

//accwithspmap variable is used to store the Account record id & SalesPartner__c record id
Map<id, id> accwithspmap = new map<id,id>();

//for loop is used to iterating Job__c records using trigger.New(job object fields will be available in this variable) context variable.
for(job__c j:trigger.New){
//checking current Job__c record having account & SalesPartner__c field values or not. 
//if the values are not emapty the below condition will execute
if(j.Account__c!=Null && j.SalesPartner__c!=Null){
//for each job record Account__c & SalesPartner__c field values are storing in map variable called accwithspmap by using Put method
accwithspmap.put(j.Account__c,j.SalesPartner__c);
}
}
//Initialized acclistupdate list variable to update the accounts with SalesPartner__c values
List<Account> acclistupdate = new List<Account>();
//getting the accounts which are linked to job__c records based on  accwithspmap.keyset()(this method have list of account keys) and iterating them using for loop
for(Account acclist: [Select id,SalesPartner__c from account where id=:accwithspmap.keyset()]){
//checking the accwithspmap variable contains the matching account or not. 
If(accwithspmap.containskey(acclist.id)){
//If its matching then we are assigning the job related SalesPartner__c field value to account SalesPartner__c field.
acclist.SalesPartner__c = accwithspmap.get(acclist.id);
// adding acclist variable values to acclistupdate list variable
acclistupdate.add(acclist);
}

}
//finally udating the account records with SalesPartner__c field valuue.
Update acclistupdate;



}

I hope, its clear.

Thanks!!

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Enea,

Whenever Job record created with Account and Sales partner then Account record is updating with sales partner field value from Job__c record.

Thanks!!
BigPBigP
Hi 
I want to know the meaning of each line of the code because i am new to apex.
AnkaiahAnkaiah (Salesforce Developers) 
Hi Enea,
 
//This trigger will fire whenever account created or updated
Trigger SalespartnerUpdate on Job__c(after insert, After Update){

//accwithspmap variable is used to store the Account record id & SalesPartner__c record id
Map<id, id> accwithspmap = new map<id,id>();

//for loop is used to iterating Job__c records using trigger.New(job object fields will be available in this variable) context variable.
for(job__c j:trigger.New){
//checking current Job__c record having account & SalesPartner__c field values or not. 
//if the values are not emapty the below condition will execute
if(j.Account__c!=Null && j.SalesPartner__c!=Null){
//for each job record Account__c & SalesPartner__c field values are storing in map variable called accwithspmap by using Put method
accwithspmap.put(j.Account__c,j.SalesPartner__c);
}
}
//Initialized acclistupdate list variable to update the accounts with SalesPartner__c values
List<Account> acclistupdate = new List<Account>();
//getting the accounts which are linked to job__c records based on  accwithspmap.keyset()(this method have list of account keys) and iterating them using for loop
for(Account acclist: [Select id,SalesPartner__c from account where id=:accwithspmap.keyset()]){
//checking the accwithspmap variable contains the matching account or not. 
If(accwithspmap.containskey(acclist.id)){
//If its matching then we are assigning the job related SalesPartner__c field value to account SalesPartner__c field.
acclist.SalesPartner__c = accwithspmap.get(acclist.id);
// adding acclist variable values to acclistupdate list variable
acclistupdate.add(acclist);
}

}
//finally udating the account records with SalesPartner__c field valuue.
Update acclistupdate;



}

I hope, its clear.

Thanks!!
This was selected as the best answer