I am hoping someone can guide me. I have created this trigger on a custom object called Trident Contract
. It pulled the agent name from the custom object Agent
using the Agent Id. Now I need to add the Buyer Agent Office
to the trigger. The Buyer Agent
Lookup Field was working until I tried to also add the office. Here is my first trigger. Please advise how to add another field lookup. Thank you
trigger BuyAgent on Trident_Contract__c (before insert, before update) {
Set <String> AgentIDs = new Set <String>();
for(Trident_Contract__c tc:trigger.new) {
if(tc.Buyer_Agent_ID__c !=NULL){
AgentIDs.add(tc.Buyer_Agent_ID__c);
}
}
//Match Agent ID to Buyer Agent ID
//Map ID
Map<String, Agent__c> Agents = new map <String, Agent__C>();
for (Agent__c obj: [
SELECT ID, Agent_ID__c
FROM Agent__c
WHERE Agent_ID__c IN :AgentIDs]) {
Agents.put(obj.Agent_ID__c,obj);
}
for(Trident_Contract__c tc : trigger.new) {
if (Agents.containsKey(tc.Buyer_Agent_ID__c))
tc.Buyer_Agent_Lookup__c = Agents.get(tc.Buyer_Agent_ID__c).ID;
}
}
I updated the trigger to the following
trigger BuyAgent on Trident_Contract__c (before insert) {
Set<String> lagid=new set<string>();
for(Trident_Contract__c tc :Trigger.new){
lagid.add(tc.buyer_agent_Id__c);
}
Map<String, String>matchid=new Map<String, String>();
for(Agent__c ag: [
SELECT Agent_ID__c, Agent_Office__c
FROM Agent__c
WHERE Agent_ID__c IN :lagid]) {
matchid.put(ag.Agent_ID__c, ag.Agent_Office__c);
}
for(Trident_Contract__c tc : Trigger.new) {
if(matchid.containskey(tc.buyer_agent_ID__c))
tc.Buyer_Agent_lookup__c = matchid.get(tc.Buyer_Agent_Lookup__c);
tc.Buyer_Agent_Office__c = matchid.get(tc.Buyer_Agent_Office__c);
}
}
But it does not update the Buyer Office Field