Skip to main content
Robert Goldberg preguntó en #Apex
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);}

      }
5 respuestas
  1. 19 dic 2017, 04:29
    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
0/9000