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
SambitNayakSambitNayak 

Trigger to fetch a field on Contact

Hi,
I have the contact object which has 2 fields-- job code and job grade.
I have another object job code where there are 2 fields job code and job grade.
When a user creates a contact and enters a job code, he should get the job grade populated automatically.
How can do this using a Map?
Thanks.
SFDC Ravi KumarSFDC Ravi Kumar
trigger UpdateJobGrade on Contact (before insert,before update) {
    set<ID> JobCodeIds = New Set<ID>();
    for(Contact c : Trigger.New){
        if(c.Job_Code__c != NULL || c.Job_Code__c != ''){
        JobCodeIds.add(c.Job_Code__c);
        }
    }

    Map<ID,Job_Code__c> JCIds = New MAP<ID,Job_Code__c>([select id,Name,Job_Grade__c From Job_Code__c where Id In: JobCodeIds]);

    for(Contact c : Trigger.New){
        c.Job_Grade__c = JCIds.get(c.Job_Code__c).Job_Grade__c;
    }
}
Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as the best answer!!!