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
Robert Goldberg 9Robert 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);}
      }
Best Answer chosen by Robert Goldberg 9
v varaprasadv varaprasad
Hi Robert,

Please check once below 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);
	  }
	  }
     system.debug('==MAKs=='+MAKs);                          
  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);
         }
		 system.debug('==AGMAP=='+AGMAP);
    for(Agent__c X: trigger.new){
		if(AGMAP.containskey(X.Modified_Anchor_Key__c))
			system.debug('==AGMAP.get(X.Agent_Id__c)=='+AGMAP.get(X.Modified_Anchor_Key__c));
			X.Active_Agent_ID__c=AGMAP.get(X.Modified_Anchor_Key__c);
	}
   }

Hope it helps you.

Thanks
Varaprasad

All Answers

v varaprasadv varaprasad
Hi Robert,
In your code everything is fine.
Please check once trigger is active or not.
Check once debug logs everything is working properly or not.
 
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);
	  }
	  }
     system.debug('==MAKs=='+MAKs);                          
  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);
         }
		 system.debug('==AGMAP=='+AGMAP);
    for(Agent__c X: trigger.new){
		if(AGMAP.containskey(X.Modified_Anchor_Key__c))
			ystem.debug('==AGMAP.get(X.Agent_Id__c)=='+AGMAP.get(X.Agent_Id__c));
			X.Active_Agent_ID__c=AGMAP.get(X.Agent_Id__c);
	}
   }

Thanks
Varaprasad


 
Paul S.Paul S.
I believe your last line:
X.Active_Agent_ID__c = AGMAP.get(X.Agent_Id__c);
Should be:
X.Active_Agent_ID__c = AGMAP.get(X.Modified_Anchor_Key__c);
You "get" the key from a map, which then returns the value.  In the case of your map, the Agent Id. 

However, is the Modified Anchor Key field on your records unique?  If not, you could run into issues with your map.
Robert Goldberg 9Robert Goldberg 9
Varaprasad -

I added the debug code in, and got this when I checked the log, this is what I got:

User-added image

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.
v varaprasadv varaprasad
Hi Robert,

Please check once below 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);
	  }
	  }
     system.debug('==MAKs=='+MAKs);                          
  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);
         }
		 system.debug('==AGMAP=='+AGMAP);
    for(Agent__c X: trigger.new){
		if(AGMAP.containskey(X.Modified_Anchor_Key__c))
			system.debug('==AGMAP.get(X.Agent_Id__c)=='+AGMAP.get(X.Modified_Anchor_Key__c));
			X.Active_Agent_ID__c=AGMAP.get(X.Modified_Anchor_Key__c);
	}
   }

Hope it helps you.

Thanks
Varaprasad
This was selected as the best answer
Robert Goldberg 9Robert Goldberg 9
Varaprasad - that works just as I wanted!  Thanks so much!