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 somebody explain the meaning of each line of this code?

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;



}
}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Enea,

Please find the below explanation to the code.
The overall code is assing the Job salespartner to the Account salespartner field and wrote teh line by line explanation .
//Below line is trigger on Job object and it executes after insert and after update of record
Trigger SalespartnerUpdate on Job__c(after insert, After Update){
//Below line is initializing the map with key as id and value as id
    Map<id, id> accwithspmap = new map<id,id>();
    //for each job record 
    for(job__c j:trigger.New){
        // if Job record has aaccount and job salespartener is not null
    if(j.Account__c!=Null && j.SalesPartner__c!=Null){
        // Adding the account id and salespartner record ids to the map
    accwithspmap.put(j.Account__c,j.SalesPartner__c);
    }
    }
    //Initialising the Account list
    List<Account> acclistupdate = new List<Account>();
    // in below line we are query all the account which were added as key to the map defined above and iterating through for loop
    for(Account acclist: [Select id,SalesPartner__c from account where id=:accwithspmap.keyset()]){
    // we are checking map accountid and the account id in the iteration are mapping
    If(accwithspmap.containskey(acclist.id)){
        // assiging the salespartner from job to account
    acclist.SalesPartner__c = accwithspmap.get(acclist.id);
    // adding the account to the list
    acclistupdate.add(acclist);
    }
    
    }
    //updating the account list
    Update acclistupdate;
    
    
    
    }
Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
 
mukesh guptamukesh gupta
Hi Enea,

Just Simple:-
 
// this trigger will execute after any new job entry insert or old job entry update

Trigger SalespartnerUpdate on Job__c(after insert, After Update){

Map<id, id> accwithspmap = new map<id,id>(); // this map hold the list of sales partners list with each account

for(job__c j:trigger.New){ // this is iterating by upcomming entry
if(j.Account__c!=Null && j.SalesPartner__c!=Null){// here we are checking Account and SalesPartner should be is exist
accwithspmap.put(j.Account__c,j.SalesPartner__c); // storing salespartnerlist with Account
}
}
List<Account> acclistupdate = new List<Account>(); // new list for account
for(Account acclist: [Select id,SalesPartner__c from account where id=:accwithspmap.keyset()]){ // fetch the account list that's have salespartner

If(accwithspmap.containskey(acclist.id)){// salespartner is exist or not
acclist.SalesPartner__c = accwithspmap.get(acclist.id); // assign replace old SalesPartner by new salespartner
acclistupdate.add(acclist); // updated account list hold here
}

}

Update acclistupdate; // update account list



}
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh