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
SFDC@ErrorSFDC@Error 

Update & Create child based on parent update

Hi All
How can I update and create child record based on the parent? Like in the parent, I have a picklist field with value 1,2,3,4 and Budget Amount is 2000.and in the child there is no record. If I select picklist value 2 and save it will create 2 child record with budget amount 1000 and 1000.but if i select picklist value 4 then it is creating 4 records with budget amount 500,500,500,500 like. Problem is here record is creating new 4 child record .but i want to update existing record and create new 2 records
trigger Pay on Parent__c (before update) {
    Set<Id> ids = new Set<Id>();
     list<child__c> ids1= new list<child__c >();
      for(Opportunity rr : Trigger.new) {
        if (Trigger.oldmap.get(rr.id).Number__c!= Trigger.newmap.get(rr.id).Number__c  ) {
     
            for(integer i = 0; i< integer.valueOf(rr.Number__c); i++){
                child__c c= new child__c();
               
                c.name = rr.Name ;
                c.Parent__c= rr.id;
                c.Pay=rr.Pay/rr.Number__c
                
               
                ids1.add(c);
            }
     }
     }
    
    
    if(!ids1.isEmpty()){
    upsert ids1;
    }       
}

.
Maharajan CMaharajan C
Hi SFDC,

Please use the below code for you refernce:

I am using the Program__C object instead of child__c Object. And Opportunity as Parent.

Please gothrough the code entierly and understand clearly.

I tested it's working fine to me.

===============

trigger PayTrigger on Opportunity (before update) {
    
    Set<Id> ids = new Set<Id>();
    list<Project__c> ids1= new list<Project__c >();
    list<Project__c> ids2= new list<Project__c >();
    Map<Id,Integer> oppOldMap = new Map<Id,Integer>();
    Map<Id,Integer> childsizeMap = new Map<Id,Integer>();
    Map<Id,List<Project__c>> childMap = new Map<Id,List<Project__c>>();
    
    for(Opportunity rr : Trigger.new) {
        if (Trigger.oldmap.get(rr.id).Program__c!= Trigger.newmap.get(rr.id).Program__c  ) {
            ids.add(rr.Id);
            
            if(Trigger.oldmap.get(rr.id).Program__c != null)
                oppOldMap.put(rr.Id,integer.valueOf(Trigger.oldmap.get(rr.id).Program__c));
            
        }
    }
    
    List<opportunity> oppLst = new List<Opportunity>();
    if(ids.size() > 0)
    {
        oppLst = [Select Id,Name,Amount,Program__c,(Select Id,Name,Opportunty_Name__c,Pay__c from Project__r) from Opportunity where Id IN: ids];   /// Use you child Object relationshipname
    }
    
    if(oppLst.size() > 0)
    {
        for(Opportunity o : oppLst)
        {
            childsizeMap.put(o.Id,o.Project__r.size());
            for(Project__c poj : o.Project__r)
            {
                if(childMap.containsKey(poj.Opportunty_Name__c))
                    childMap.get(poj.Opportunty_Name__c).add(poj);
                
                else
                    childMap.put(poj.Opportunty_Name__c , new List<Project__c>{poj});
                
            }
        }
    }
    
    
    for(Opportunity opp : Trigger.new)
    {
        Integer childs = 0;
               
        if(childsizeMap.containsKey(opp.Id))
        {
             childs = childsizeMap.get(opp.Id);
        }
           
        Integer newProgram = 0;
        
        if(opp.Program__c != null)
        newProgram = integer.valueOf(opp.Program__c);
            
        if(childs > 0 &&  oppOldMap.containsKey(opp.Id) && (newProgram > oppOldMap.get(opp.Id)) )
        {
            Integer isize = integer.valueOf(opp.Program__c) - childs;
            
            if(isize > 0 )
            {
                for(integer i = 0; i<isize ; i++){
                    Project__c c= new Project__c();
                    c.name = opp.Name ;
                    c.Opportunty_Name__c= opp.id;
                    c.Pay__c = opp.Amount / Integer.valueOf(opp.Program__c);
                    ids1.add(c);
                }
                
                for(Project__c p : childMap.get(opp.Id))
                {
                    system.debug('@@@ Update Project ');
                    Project__c pro = new Project__c(id = p.Id);
                    pro.Pay__c = opp.Amount / Integer.valueOf(opp.Program__c);
                    ids2.add(pro);
                }
            }
        }
        
        else
        {

            Integer oldValue = 0;
            if(oppOldMap.get(opp.Id) != null)
            {
               oldValue =  oppOldMap.get(opp.Id);
            }
            
            if(opp.Program__c != null && (newProgram > oldValue))
            {
                for(integer i = 0; i< integer.valueOf(opp.Program__c) ; i++){
                    Project__c c= new Project__c();
                    c.name = opp.Name ;
                    c.Opportunty_Name__c= opp.id;
                    c.Pay__c = opp.Amount / Integer.valueOf(opp.Program__c);
                    ids1.add(c);
                } }
        }
    }
    
    if(ids2.size() > 0)
        update ids2;
    
    if(ids1.size() > 0)
        insert ids1;        
    
}

===================


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
SFDC@ErrorSFDC@Error
Hi Maharajan, I have tried your code.but it is not working. For example, picklist value is 1,2,3,4 and amount is 1000. if i select picklist value 1 then child record is Name Amount ABC 1000 if i select picklist value 2 then child record is Name Amount ABC 500 Name Amount ABC 500 if i select picklist value 3 then child record is Name Amount ABC 333 Name Amount ABC 333 Name Amount ABC 333 if i select picklist value 2 then child record is Name Amount ABC 500 Name Amount ABC 500 and so on like this
SFDC@ErrorSFDC@Error
Yes Maharajan, But my requirement is different. So it is not working for me .