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
NANCY1NANCY1 

How to update all the Detail Object Records??

Hi,

 

Master Object > RMG_Employee_Master__c

Detail object > Proposal__c

Detail Object Field > Status__c

 

Under this master object, I have number of detail object records.

 

The functionality is something like when I create a new record in the Detail Object where Status__c is not equal to ‘Proposed’, than for all the other records [already created] Status__c should become ‘Closed’.

 

Please let me know is this can be achieved?? Thanks

Best Answer chosen by Admin (Salesforce Developers) 
hitzhitz

HI, Nancy

 

If i am not wrong then you just want to update all other Child records status value of particular Master Object right .... ?

if Yes then i have done some changes in previous Trigger code ..... Hopes this time it satisfiy your requirement......

New Trigger Code ..........

 

trigger roposalstatus on Proposal__c (after insert) {
   
    List < Id > empmasterIds = new List < Id >();
    List < Id > proposalIds = new List < Id >();

    for ( Proposal__c  c: Trigger.New ) {

        proposalIds.add( c.Id );
        empmasterIds.add( c.RMG_Employee_Code__c);

    }
    System.debug('----------> '+empmasterIds.size());
    List<RMG_Employee_Master__c> opps = [select id, Proposal_Status__c from RMG_Employee_Master__c where id in :empmasterIds];
    
    Map < Id ,RMG_Employee_Master__c > empmap = new Map < Id , RMG_Employee_Master__c >();

    for ( RMG_Employee_Master__c  a : opps   ) {
        empmap.put( a.Id, a);
    }

    List < RMG_Employee_Master__c > EmpToUpdate = new List < RMG_Employee_Master__c >();
    
    for(Proposal__c c: Trigger.New) {
        RMG_Employee_Master__c ac = empmap.get( c.RMG_Employee_Code__c );
            
        if ( ac == null ) {
            
            continue;
        }
        if(c.Status__c != 'Proposed'){
            System.debug('--------- REG EMP Master ID :- '+ ac.Id);
            System.debug('--------- New Proposal ID :- '+ c.Id);
            List<Proposal__c> proObj = [select Id,Status__c,RMG_Employee_Code__c from Proposal__c where Id !=:c.Id and RMG_Employee_Code__c =:ac.Id  ];
            System.debug('--------- Proposal size : '+ proObj.size());
           for(Proposal__c pc: proObj ){
               pc.Status__c = 'Closed';
               update pc;
           }
           
        }
        
        /*If (c.Status__c != 'Closed'){
            
             ac.Proposal_Status__c = c.Status__c;
             EmpToUpdate.add( ac );
            
        }else{
            ac.Proposal_Status__c = '';
            EmpToUpdate.add( ac );
        }*/
    }

   // upsert EmpToUpdate;       

}

All Answers

hitzhitz

HI, Nancy

 

If i am not wrong then you just want to update all other Child records status value of particular Master Object right .... ?

if Yes then i have done some changes in previous Trigger code ..... Hopes this time it satisfiy your requirement......

New Trigger Code ..........

 

trigger roposalstatus on Proposal__c (after insert) {
   
    List < Id > empmasterIds = new List < Id >();
    List < Id > proposalIds = new List < Id >();

    for ( Proposal__c  c: Trigger.New ) {

        proposalIds.add( c.Id );
        empmasterIds.add( c.RMG_Employee_Code__c);

    }
    System.debug('----------> '+empmasterIds.size());
    List<RMG_Employee_Master__c> opps = [select id, Proposal_Status__c from RMG_Employee_Master__c where id in :empmasterIds];
    
    Map < Id ,RMG_Employee_Master__c > empmap = new Map < Id , RMG_Employee_Master__c >();

    for ( RMG_Employee_Master__c  a : opps   ) {
        empmap.put( a.Id, a);
    }

    List < RMG_Employee_Master__c > EmpToUpdate = new List < RMG_Employee_Master__c >();
    
    for(Proposal__c c: Trigger.New) {
        RMG_Employee_Master__c ac = empmap.get( c.RMG_Employee_Code__c );
            
        if ( ac == null ) {
            
            continue;
        }
        if(c.Status__c != 'Proposed'){
            System.debug('--------- REG EMP Master ID :- '+ ac.Id);
            System.debug('--------- New Proposal ID :- '+ c.Id);
            List<Proposal__c> proObj = [select Id,Status__c,RMG_Employee_Code__c from Proposal__c where Id !=:c.Id and RMG_Employee_Code__c =:ac.Id  ];
            System.debug('--------- Proposal size : '+ proObj.size());
           for(Proposal__c pc: proObj ){
               pc.Status__c = 'Closed';
               update pc;
           }
           
        }
        
        /*If (c.Status__c != 'Closed'){
            
             ac.Proposal_Status__c = c.Status__c;
             EmpToUpdate.add( ac );
            
        }else{
            ac.Proposal_Status__c = '';
            EmpToUpdate.add( ac );
        }*/
    }

   // upsert EmpToUpdate;       

}

This was selected as the best answer
NANCY1NANCY1

Hi Hitesh,

 

based on your code.. i am using the below code to fulfill my requirements, but i just wanted to know if i try uploading the detail object records data using the Data Loader.. i use to get the following error:

 

proposalstatus: execution of AfterUpdate

 

caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements

 

Trigger.proposalstatus: line 53, column 2

 

 

 

trigger proposalstatus on Proposal__c (after insert,after update)

    List < Id > empmasterIds = new List < Id >();
    List < Id > proposalIds = new List < Id >();

    for ( Proposal__c  c: Trigger.New )
    {

        proposalIds.add( c.Id );
        empmasterIds.add( c.RMG_Employee_Code__c);

    }
   
    List<RMG_Employee_Master__c> opps = [select id, Proposal_Status__c from RMG_Employee_Master__c where id in :empmasterIds];
   
    Map < Id ,RMG_Employee_Master__c > empmap = new Map < Id , RMG_Employee_Master__c >();

    for ( RMG_Employee_Master__c  a : opps   )
    {
        empmap.put( a.Id, a);
    }

    List < RMG_Employee_Master__c > EmpToUpdate = new List < RMG_Employee_Master__c >();
   
    for(Proposal__c c: Trigger.New)
    {
        RMG_Employee_Master__c ac = empmap.get( c.RMG_Employee_Code__c );        
        if ( ac == null )
        {   
          continue;
        }
        If (c.Status__c != 'Closed')
        {
           ac.Proposal_Status__c = c.Status__c;   
           EmpToUpdate.add( ac );
        }
        else
        {
           ac.Proposal_Status__c = 'Unassigned';
           EmpToUpdate.add( ac );
        }
        if(c.Status__c == 'Selected' || c.Status__c == 'Blocked' || c.Status__c == 'Awaiting Background Check')
        {
            List<Proposal__c> proObj = [select Id,Status__c,RMG_Employee_Code__c from Proposal__c where Id !=:c.Id and RMG_Employee_Code__c =:ac.Id  ];
            for(Proposal__c pc: proObj )
             {
               pc.Status__c = 'Closed';
               update pc;
             }      
         }
  }
 
 upsert EmpToUpdate;      

}

hitzhitz

Hi, NANCY

 

u have written wrong code see...

 

// upsert EmpToUpdate;     

 

this code was in commented....

 

and now check it again....

NANCY1NANCY1

Hi Hitesh,

 

If i'll  comment out the 

 

// upsert EmpToUpdate;     

 

then how will i update my Proposal_Status__c field ??

hitzhitz

HI, NANCY

 

let me Know What Excetly you want to do ?? are u want to update other child records or only want to update parent records...

 

plz clear about ur Question...