You need to sign in to do that
Don't have an account?
Robert Goldberg 9
Trigger To Update Field From Map/Set On Same Object
I've written a trigger on a custom object (Agent__c), for when a value (is_active__c) changes to false, that it will look into that custom object, and then see if that agent is active somewhere else. If so, it should bring back a string and update a field on the original record.
However, I cannot get it to fire, no matter what I do.
Here is the code:
trigger Inactive_Active on Agent__c (before insert, before update) {
Set<String> MAKs = new Set<String>();
for(Agent__c X : trigger.new){
if(X.Is_Active__c!=true){MAKs.add(X.Modified_Anchor_Key__c);}}
Map <String, String> AGMAP = new Map <String, String>();
for (Agent__c AT: [SELECT Modified_Anchor_Key__c, Agent_ID__c
from Agent__c
Where Modified_Anchor_Key__c IN: MAKs and Is_Active__c=true]){AGMAP.put(AT.Modified_Anchor_Key__c, AT.Agent_ID__c);
}
for(Agent__c X: trigger.new){if(AGMAP.containskey(X.Modified_Anchor_Key__c)) X.Active_Agent_ID__c=AGMAP.get(X.Agent_Id__c);}
}
However, I cannot get it to fire, no matter what I do.
Here is the code:
trigger Inactive_Active on Agent__c (before insert, before update) {
Set<String> MAKs = new Set<String>();
for(Agent__c X : trigger.new){
if(X.Is_Active__c!=true){MAKs.add(X.Modified_Anchor_Key__c);}}
Map <String, String> AGMAP = new Map <String, String>();
for (Agent__c AT: [SELECT Modified_Anchor_Key__c, Agent_ID__c
from Agent__c
Where Modified_Anchor_Key__c IN: MAKs and Is_Active__c=true]){AGMAP.put(AT.Modified_Anchor_Key__c, AT.Agent_ID__c);
}
for(Agent__c X: trigger.new){if(AGMAP.containskey(X.Modified_Anchor_Key__c)) X.Active_Agent_ID__c=AGMAP.get(X.Agent_Id__c);}
}
Please check once below code :
Hope it helps you.
Thanks
Varaprasad
All Answers
In your code everything is fine.
Please check once trigger is active or not.
Check once debug logs everything is working properly or not.
Thanks
Varaprasad
However, is the Modified Anchor Key field on your records unique? If not, you could run into issues with your map.
I added the debug code in, and got this when I checked the log, this is what I got:
Could the fact that the Agent_ID__c and the Modified_Anchor_Key__c fields are external IDs have anything to do with this?
Paul - no, the Modified_Anchor_Key__c field is not unique. That's the issue - trying to find out when a new agent is inserted with the same Modified_Anchor_Key__c.
Please check once below code :
Hope it helps you.
Thanks
Varaprasad