0

I am trying to do a 1-1 look up relationship of the same object. This is the situation:

I have a look up in my account called partner (Partner__c) and it is a look up to the same object (account). What I need is once I select a partner for an account for instance I am in John Doe account and want to select Mary Doe as partner once I saved John Doe, Mary Doe Partner field needs to be updated to John Doe (also a checkbox (is_partner__c) need to be true).

My first question: is there a way of doing this without code? as I couldnt find anything in google.

I developed my trigger however I am running into an infinite loop, I guess once Updated 1 account will be stuck updating the original account and so on. SO i am not sure if thi is the right way, or how can I say the trigger to do it once.

     Map<Id,Id> partners = new Map<Id,Id>();
     Set<Id> partnersSet = new Set<Id>();
     for(Account a : Trigger.new) 
     {
        if(a.Partner__c != null){
           partners.put(a.Partner__c, a.Id);
           partnersSet.add(a.Partner__c);
        }
     }
     system.debug('!!! partner map (partner id, current account id): ' + partners);
     List<Account> partnersToUpdate = new List<Account>();
     partnersToUpdate = [SELECT Id FROM Account WHERE Id IN : partnersSet ];
     String accountID;
     if( partnersSet.size()>0 ){
        for(Account aPartner:partnersToUpdate){
           accountID = partners.get(aPartner.Id);
           aPartner.Partner__c = accountID;
           system.debug('!!! Partner: ' + aPartner.Partner__c);
        }
        //update partnersToUpdate;
     }  
  • is_partner__c needs to be true for both account? – snehakem May 29 '15 at 5:54
  • Hi no, in the example it will be only for Mary – manza May 29 '15 at 6:32
  • Did you try my code? – snehakem May 29 '15 at 6:33
  • snehakem: Thank you so much, it half works, it updated one account but not the other one – manza Jun 1 '15 at 1:17
1

snehakem is almost right but need small modification please try this code Mr.manza

Trigger myTrigger on Account (after insert, after update){
     Map<Id,Account> mapPartners = new Map<Id,Account>();
     Set<Id> partnersSet = new Set<Id>();
     map<id,Account> partnersToUpdate = new map<id,Account>();
     List<Account> partnersToProcess = new List<Account>();

     for(Account a : Trigger.new) 
     {
        if((Trigger.isInsert && a.Partner__c != null) || (Trigger.isUpdate && a.Partner__c!=Trigger.oldMap.get(a.id).Partner__c)){
           partnersToProcess.add(a);
           partnersSet.add(a.Partner__c);
        }
     }     

     mapPartners=new Map<Id,Account>( [SELECT Id,Partner__c,is_partner__c FROM Account WHERE Id IN : partnersSet ]);

     for(Account aa : partnersToProcess ){
         if(mapPartners.keyset().contains(aa.Partner__c)){

              mapPartners.get(aa.Partner__c).is_partner__c =true;
              mapPartners.get(aa.Partner__c).Partner__c=aa.id;

         }
    }

    if(mapPartners.size()>0){
         update mapPartners.values();
    }

}
  • I just realized something, this version it updates both accounts fields is_partner__c to true, however I need the second account to be only set as true, not the original one (maybe the name is not the most adequate as it should be something like is partner of someone) – manza Jun 10 '15 at 1:23
  • Then we have to use recursive function – ncmouli Jun 10 '15 at 5:45
1

The updated trigger wont go into an infinite loop,

Trigger myTrigger on Account (after insert, after update){
     Map<Id,Account> mapPartners = new Map<Id,Account>();
     Set<Id> partnersSet = new Set<Id>();
     map<id,Account> partnersToUpdate = new map<id,Account>();
     List<Account> partnersToProcess = new List<Account>();

     for(Account a : Trigger.new) 
     {
        if((Trigger.isInsert && a.Partner__c != null) || (Trigger.isUpdate && a.Partner__c!=Trigger.oldMap.get(a.id).Partner__c)){
           partnersToProcess.add(a);
           partnersSet.add(a.Partner__c);
        }
     }     

     mapPartners=new Map<Id,Account>( [SELECT Id,Partner__c,is_partner__c FROM Account WHERE Id IN : partnersSet ]);

     for(Account a : partnersToProcess ){
         if(mapPartners.keyset().contains(a.Partner__c)){

              mapPartners.get(a.Partner__c).is_partner__c =true;
              mapPartners.get(a.Partner__c).Partner__c=a.id;
              //partnersToUpdate.put(a.id,a);
              partnersToUpdate.put(a.Partner__c,mapPartners.get(a.Partner__c));
         }
    }

    if(partnersToUpdate.size()>0){
         update partnersToUpdate.values();
    }
}
  • Thank you for the quick response, I am researching as I am getting a weird error: Error:Apex trigger Account_AIAU caused an unexpected exception, contact your administrator: Account_AIAU: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.Account_AIAU: line 81, column 1 – manza May 29 '15 at 6:45
  • Trigger Account_AIAU is this the same trigger which i wrote? or is that the different one? if yes then please deactivate that one and keep only the current trigger active & test. Let me know if it works – snehakem May 29 '15 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.