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
Raju Chi 5Raju Chi 5 

Need help on trigger logic

Hi All,

I have a requirement like child Part number update to Parent object field with comma separeted.

But issue is when status is approved then i have populated Parent id to child data with some creteria.when updating part number using before update part number is not populated in parent object  because child data is not adding before commit.

When using after update getting record is read only.

Here is code.

public  void updatePartNumber1(List<Account> rwlist){
        if(isCheck == true){
        String concatenateString = '';
        
         List<Contact> erplst = [SELECT Name,Part_Number__c,Device__r.Type__c,Device__r.Name   FROM Contact where account__c =:rwlist];
        String partnumber;
        for(Account  rw : rwlist){
            
           partnumber ='';
            system.debug('erplst'+erplst.size());
            if(rw.status__c == 'Approved' ){
            for(Contact erp : erplst){ 
               
                If(rw.Apply_RWA_All_Base_Device__c == 'Yes' && rw.Expose_RWA_to_Customer__c == 'Yes' ){
                    concatenateString += String.isBlank(concatenateString) ? erp.Part_Number__c : ',' + erp.Part_Number__c;
                    
                    if(erp.Device__r.Tapeout_Type__c  == 'Single Value'){
                        
                        partnumber = erp.Device__r.Name;
                        rw.Single_Product__c = true;
                    } 
                }else{
                    If(rw.Apply_RWA_All_Product_family__c == 'Yes' && rw.Expose_RWA_to_Customer__c == 'Yes' ){
                        concatenateString += String.isBlank(concatenateString) ? erp.Part_Number__c : ',' + erp.Part_Number__c; 
                        system.debug('concatenateString'+concatenateString);
                        
                    }
                }
                
            }
            rw.Initial_Part_Number_s_Covered__c = concatenateString;
            
            if( rw.Description_and_Part_Number__c != null && (rw.Description_and_Part_Number__c.contains(partnumber))){
                rw.Description_and_Part_Number__c = rw.Description_and_Part_Number__c;
            }else if(rw.Description_and_Part_Number__c == '' || rw.Description_and_Part_Number__c == null ){                
                rw.Description_and_Part_Number__c = partnumber;
            }else{
                 rw.Description_and_Part_Number__c = partnumber+ rw.Description_and_Part_Number__c;
            }
            
            rw.Customer_Product_Family_Name__c = true;
            rwlist.add(rw);
            }   
        }
        try{
            update rwlist;
        }catch(exception e){
            system.debug(e);
        }
        }
    }
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Raju,

I have some changes in your code. You can take reference from this below code.

Trigger:-
trigger AccountTrigger on Account(before Update){
  if(trigger.isBefore && trigger.isUpdate){
     ClassName.updatePartNumber1(trigger.new);
}
}

public class ClassName{
public static  void updatePartNumber1(List<Account> rwlist){
        if(isCheck == true){
        String concatenateString = '';
        
         List<Contact> erplst = [SELECT Name,Part_Number__c,Device__r.Type__c,Device__r.Name,Device__r.Tapeout_Type__c   FROM Contact where account__c IN:rwlist];
        String partnumber;
        for(Account  rw : rwlist){
            
           partnumber ='';
            system.debug('erplst'+erplst.size());
            if(rw.status__c == 'Approved' ){
            for(Contact erp : erplst){ 
               
                If(rw.Apply_RWA_All_Base_Device__c == 'Yes' && rw.Expose_RWA_to_Customer__c == 'Yes' ){
                    concatenateString += String.isBlank(concatenateString) ? erp.Part_Number__c : ',' + erp.Part_Number__c;
                    
                    if(erp.Device__r.Tapeout_Type__c  == 'Single Value'){
                        
                        partnumber = erp.Device__r.Name;
                        rw.Single_Product__c = true;
                    } 
                }else{
                    If(rw.Apply_RWA_All_Product_family__c == 'Yes' && rw.Expose_RWA_to_Customer__c == 'Yes' ){
                        concatenateString += String.isBlank(concatenateString) ? erp.Part_Number__c : ',' + erp.Part_Number__c; 
                        system.debug('concatenateString'+concatenateString);
                        
                    }
                }
                
            }
            rw.Initial_Part_Number_s_Covered__c = concatenateString;
            
            if( rw.Description_and_Part_Number__c != null && (rw.Description_and_Part_Number__c.contains(partnumber))){
                rw.Description_and_Part_Number__c = rw.Description_and_Part_Number__c;
            }else if(rw.Description_and_Part_Number__c == '' || rw.Description_and_Part_Number__c == null ){                
                rw.Description_and_Part_Number__c = partnumber;
            }else{
                 rw.Description_and_Part_Number__c = partnumber+ rw.Description_and_Part_Number__c;
            }
            
            rw.Customer_Product_Family_Name__c = true;
           
            }   
        }
        
      
        }
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
Raju Chi 5Raju Chi 5
HI Suraj,
Thanks for response
I have used already same one but not working. Here problem is child data adding to parent is after update. 

when parent status change to Approved then we are populating parent id to all child records based on creteria.

Regards,
Raju