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
Stephanie AkinStephanie Akin 

Trigger to populate Lookup field on Contact from custom object

Hi.

I am new to apex and I need some help with a trigger. I've looked around online and can't even piece together some code to test it, so I am asking for your help.

I have a lookup field on the contact object named Sage Caller (which is a user object lookup field), I need this field to be updated when the field from my custom object Scorecard named Caller is filled out. (Caller is a lookup field to the user object).

Basically, when I create a Scorecard record and fill out the Caller field, whomever is in that field I need that same user to be updated into the Sage Caller field on the contact.

I can't use a workflow because it is just a lookup relationship and not master detail. 

Thanks in advance! 


Best Answer chosen by Stephanie Akin
Vatsal KothariVatsal Kothari
Hey Stephanie,

Just add below line after 2nd line :

Map<Id,Contact> conMap = new Map<Id,Contact>();

Thanks,
Vatsal

All Answers

Vatsal KothariVatsal Kothari
Hi Stephaine,

can you please tell what is the relation between Score Card & Contact Object? Because you are creating score card record and updating contact..So which contact do you want to update?

Thanks,
Vatsal
Stephanie AkinStephanie Akin
Hi Vatsal:

There is a field on the Scorecard object called Primary Training Contact and that is a Contact lookup field. That would be the contact record to update Sage Caller on. 

I hope that helps. 

Thank you!
Vatsal KothariVatsal Kothari
I tried for you, Here is the code:

trigger updateContact on Scorecard__c (before insert,before update){
	Set<Id> conId = new Set<Id>();
	
	for(Scorecard__c score : trigger.new){
		if(score.Primary_Training_Conatct__c != null){
			conId.add(score.Primary_Training_Conatct__c);
		}
	}
	for(Contact con : [Select Id,Sage_Caller__c from Contact where Id IN: conId]){
		conMap.put(con.Id,con);
	}
	
	for(Scorecard__c score : trigger.new){
		if(conMap.containsKey(score.Primary_Training_Conatct__c) && conMap.containsKey(score.Primary_Training_Conatct__c) != null){
			conMap.get(score.Primary_Training_Conatct__c).Sage_Caller__c = score.Caller__c;
		}
	}
	
	if(!conMap.isEmpty()){
		update conMap;
	}
}
If this solves your problem, kindly mark it as the best answer..:)

Thanks,
Vatsal

Stephanie AkinStephanie Akin
Thank you, Vatsal. I will test it out later today and if it works I will mark best answer. 
Stephanie AkinStephanie Akin
Vatsal:

It is giving me the error: Line 22 Variable does not exist: conMap
Stephanie AkinStephanie Akin
Sorry I meant Line 20 not 22. 

error: Line 20 Variable does not exist: conMap
Vatsal KothariVatsal Kothari
Hey Stephanie,

Just add below line after 2nd line :

Map<Id,Contact> conMap = new Map<Id,Contact>();

Thanks,
Vatsal
This was selected as the best answer