+ Start a Discussion
Shweta SoparkarShweta Soparkar 

Trigger for updating the details object in a master details relationship

I have a master detail relationship wherein I have a master custom object named Contact and a detail relationship object Associated Account.
Now , in associated Account there is a field named primary account. One contact can be associated with many accounts but a contact can have only one primary account. So, here I want to write a trigger where whenever a user tries to create a record in associated account the trigger should fire and set all the primary account except the latest one to false(primary account is a checkbox). and the same should be reflected in contact.
Best Answer chosen by Shweta Soparkar
Shweta SoparkarShweta Soparkar
I am able to solve this, Below is my new code:

List<Associated_Account__c> vwo = new List<Associated_Account__c >();
   
   Set<Id> associatedAccountIds = new Set<Id>();
   Set<Id> contactIds = new Set<Id>();
   
   for(Associated_Account__c d : trigger.new){
       if(d.Primary_Account__c== true)
       {
            contactIds.add(d.Contact__c);
            associatedAccountIds.add(d.id);
        }
    }

    vwo = [SELECT id,Primary_Account__c FROM Associated_Account__c 
          WHERE Primary_Account__c=true AND Contact__c IN :contactIds AND Id NOT IN :associatedAccountIds];
    for(Associated_Account__c d:vwo)
        d.Primary_Account__c=false;
    update vwo;

    // Updating the Master Record- Contact Object
    
    List<Id> accName= new List<Id>();
     
    List<Associated_Account__c> aa= new List<Associated_Account__c>();
    aa =[select id, Associated_Account__c from Associated_Account__c WHERE Primary_Account__c=true AND Contact__c IN :contactIds];
    
    for(Associated_Account__c  d: aa)
        accName.add(d.Associated_Account__c);
        
     List<Contact> con= new List<Contact>();
     con= [select AccountId from Contact where Id IN :contactIds];
    
    for(Contact c : con)
        c.AccountId= accName.get(0);
        
    update con;
   

All Answers

Shweta SoparkarShweta Soparkar
This is how far I have reached :

trigger updateAssociatedAccount on Associated_Account__c (after insert, after update) {

    Associated_Account__c[] acc= Trigger.new;
    
    //Associated_Account__c [] AA =[SELECT Id FROM Associated_Account__c where a IN :Trigger.new];
    
    List<Associated_Account__c> toUpdate= new List<Associated_Account__c>();
  
    if(Trigger.isInsert)
    {
       for(Associated_Account__c  a: acc)
       {
            if(a.Primary_Account__c==true)
            {
               a.Primary_Account__c=false;
               toUpdate.add(a);
            }
       } 
    }
    else if(Trigger.isUpdate)
    {
        
        for(Associated_Account__c  a: acc)
        {
            if(Trigger.oldMap.get(a.Id).Contact__c==a.Contact__c)
            {
                if(a.Primary_Account__c==true)
                {
                     a.Primary_Account__c=false;
                     toUpdate.add(a);
                }
            }
        }
    }
    update toUpdate;
}



 
Shweta SoparkarShweta Soparkar
I am able to solve this, Below is my new code:

List<Associated_Account__c> vwo = new List<Associated_Account__c >();
   
   Set<Id> associatedAccountIds = new Set<Id>();
   Set<Id> contactIds = new Set<Id>();
   
   for(Associated_Account__c d : trigger.new){
       if(d.Primary_Account__c== true)
       {
            contactIds.add(d.Contact__c);
            associatedAccountIds.add(d.id);
        }
    }

    vwo = [SELECT id,Primary_Account__c FROM Associated_Account__c 
          WHERE Primary_Account__c=true AND Contact__c IN :contactIds AND Id NOT IN :associatedAccountIds];
    for(Associated_Account__c d:vwo)
        d.Primary_Account__c=false;
    update vwo;

    // Updating the Master Record- Contact Object
    
    List<Id> accName= new List<Id>();
     
    List<Associated_Account__c> aa= new List<Associated_Account__c>();
    aa =[select id, Associated_Account__c from Associated_Account__c WHERE Primary_Account__c=true AND Contact__c IN :contactIds];
    
    for(Associated_Account__c  d: aa)
        accName.add(d.Associated_Account__c);
        
     List<Contact> con= new List<Contact>();
     con= [select AccountId from Contact where Id IN :contactIds];
    
    for(Contact c : con)
        c.AccountId= accName.get(0);
        
    update con;
   
This was selected as the best answer