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
Gwirizanani SinoiaGwirizanani Sinoia 

help! create a trigger that can create and update records on the child object

I am a new developer and trying to create a trigger that creates and updates child records based on  changes to the related object.
I have two objects in a master detail relationship
Testchild_c
Testparent_c
Testparent__c has P1 amount field , P2 amount field ,P3 amount field(currency) and Age Field(number field)
Testchild_c has result output fields based on amount  and Age on Testparent__c. Testchild_c fields are = P1result, P2result, P3result(text fields)
The table matrix table has all the variables
ie if  P1__c= $100000 and Age__c=17 output to P1 result field  'MER* Full GPR FBP' and if P2_c = $800000 output to P2result= 'Full GPR, MER*, FBP, Ex ECG**, FQ'
What the trigger need to do is either create or update the record. If two conditions are met to create and update the other etc.
I have matrix table to explain wha I am trying to say.
thanks in advance

User-added image



 
Best Answer chosen by Gwirizanani Sinoia
Waqar Hussain SFWaqar Hussain SF
try below code, I think this what you are looking for.
 
Here is a copy of my code.will need something to exempt the second insertion and update.please help!

    trigger TestPreapptrigger on Pre_Application__c(after insert, after update) {


        List < PA_Pre_App_Life_Assured__c > preAppCreateList = new List < PA_Pre_App_Life_Assured__c > ();

        for (Pre_Application__c preAppLoop: trigger.new) {

            if (preAppLoop.Age_at_next_birthday__c >= 17 && preAppLoop.Age_at_next_birthday__c <= 35 && preAppLoop.PA_Life_Sum_Assured__c >= 0 && preAppLoop.PA_Life_Sum_Assured__c <= 750000) {
                PA_Pre_App_Life_Assured__c insertRecords = new PA_Pre_App_Life_Assured__c(Pre_Application__c = preAppLoop.Id,
                    Life_Assured__c = 'NO NML EVIDENCE NEEDED');
                insertRecords.P1_result__c = 'MER* Full GPR FBP';
                preAppCreateList.add(insertRecords);

            }

            if (preAppLoop.Age_at_next_birthday__c >= 17 && preAppLoop.Age_at_next_birthday__c <= 30 && preAppLoop.PA_CI_Sum_Assured__c >= 0 && preAppLoop.PA_CI_Sum_Assured__c <= 450000) {
                PA_Pre_App_Life_Assured__c insertRecords2 = new PA_Pre_App_Life_Assured__c(Pre_Application__c = preAppLoop.Id,
                    CI_Assured__c = 'NO NML EVIDENCE NEEDED');
                insertRecords2.P2result = 'Full GPR, MER*, FBP, Ex ECG**, FQ';

                preAppCreateList.add(insertRecords2);



            }

		}
	
		System.debug('preApplist size = ' + preAppCreateList.size());
		
		if(preAppCreateList.size() > 0)
		insert preAppCreateList;
}

 

All Answers

Waqar Hussain SFWaqar Hussain SF
So you want to auto create the child record when the parent record is created?
Otherwise you can create formula field on child record to auto populate the P1Result, P2Result and P3Result based on parent object. 
Abdul KhatriAbdul Khatri
I don't think you need to object to achieve your requirement. Are you expecting multiple P1result in Child Object agains P1__c field in Parent Object and same goes for others. If not then you can get away with two object with the following design

Object
Testparent__c

Fields
P1__c            Currency
P2__c            Currency
P3__c            Currency
Age__c           Number
P1result__c    Formula             IF( AND(P1__c = 1000000, Age__c =  17), ''MER* Full GPR FBP','')

Let me know if this works
Gwirizanani SinoiaGwirizanani Sinoia
Yes I want to create child records when the parent is created 
Gwirizanani SinoiaGwirizanani Sinoia
Sorry Adul a  formula wont work. you need a trigger or a flow
Waqar Hussain SFWaqar Hussain SF
See trigger , which will auto create a child related to parent object when a parent record will be created.
trigger ParentObjectTrigger on Testparent__c (after insert){
    
    list<Testchild_c> childRecords = new list<Testchild_c>();
    for(Testparent__c p : trigger.new){
        Testchild_c ch = new Testchild_c ();
        ch.Parent__c = p.Id;
        //if children object name is not auto number
        ch.Name = p.Name+' child';
        if(p.P1__c == 100000 && p.Age__c == 17)
        ch.P1_result__c = 'MER* Full GPR FBP';
        if(p.P2__c == 800000)
        ch.P2result= 'Full GPR, MER*, FBP, Ex ECG**, FQ';
        
        childRecords .add(ch);
    }

    insert childRecords ;
}

 
Gwirizanani SinoiaGwirizanani Sinoia
Thanks for your response The logic is creating two child records with two different products instead of a single child record with two products
Waqar Hussain SFWaqar Hussain SF
Not sure, what you mean
Gwirizanani SinoiaGwirizanani Sinoia
For every Parent record I am creating the result is two child records created. The reason is  I have selected  two products on the parent object instead of one. hope this makes sense
Gwirizanani SinoiaGwirizanani Sinoia
one should create/insert and the other update
Gwirizanani SinoiaGwirizanani Sinoia
hope what i said makes sense
Gwirizanani SinoiaGwirizanani Sinoia
Can somebody please help?
Gwirizanani SinoiaGwirizanani Sinoia
Here is a copy of my code. will need something to exempt the second insertion and update. please help!

trigger TestPreapptrigger on Pre_Application__c (after insert, after update){
     

    List <PA_Pre_App_Life_Assured__c> preAppCreateList = new List <PA_Pre_App_Life_Assured__c>();

    for(Pre_Application__c preAppLoop : trigger.new){
        PA_Pre_App_Life_Assured__c insertRecords;
        
        

       insertRecords = new PA_Pre_App_Life_Assured__c(Pre_Application__c= preAppLoop.Id,
                                                      Life_Assured__c = 'NO NML EVIDENCE NEEDED');


        if(  preAppLoop.Age_at_next_birthday__c >=17 && preAppLoop.Age_at_next_birthday__c<=35 && preAppLoop.PA_Life_Sum_Assured__c>= 0 && preAppLoop.PA_Life_Sum_Assured__c<=750000){

            preAppCreateList.add(insertRecords);} 

 if ( preAppLoop.Age_at_next_birthday__c>=17 && preAppLoop.Age_at_next_birthday__c<=30 && preAppLoop.PA_CI_Sum_Assured__c>= 0 && preAppLoop.PA_CI_Sum_Assured__c<=450000  ){
            insertRecords = new PA_Pre_App_Life_Assured__c(Pre_Application__c = preAppLoop.Id,
                                                              CI_Assured__c    = 'NO NML EVIDENCE NEEDED'); 
            preAppCreateList.add(insertRecords);
                
                System.debug('preApplist size = ' + preAppCreateList.size());
                
                    }        
  
    
    
    insert preAppCreateList;
Waqar Hussain SFWaqar Hussain SF
try below code, I think this what you are looking for.
 
Here is a copy of my code.will need something to exempt the second insertion and update.please help!

    trigger TestPreapptrigger on Pre_Application__c(after insert, after update) {


        List < PA_Pre_App_Life_Assured__c > preAppCreateList = new List < PA_Pre_App_Life_Assured__c > ();

        for (Pre_Application__c preAppLoop: trigger.new) {

            if (preAppLoop.Age_at_next_birthday__c >= 17 && preAppLoop.Age_at_next_birthday__c <= 35 && preAppLoop.PA_Life_Sum_Assured__c >= 0 && preAppLoop.PA_Life_Sum_Assured__c <= 750000) {
                PA_Pre_App_Life_Assured__c insertRecords = new PA_Pre_App_Life_Assured__c(Pre_Application__c = preAppLoop.Id,
                    Life_Assured__c = 'NO NML EVIDENCE NEEDED');
                insertRecords.P1_result__c = 'MER* Full GPR FBP';
                preAppCreateList.add(insertRecords);

            }

            if (preAppLoop.Age_at_next_birthday__c >= 17 && preAppLoop.Age_at_next_birthday__c <= 30 && preAppLoop.PA_CI_Sum_Assured__c >= 0 && preAppLoop.PA_CI_Sum_Assured__c <= 450000) {
                PA_Pre_App_Life_Assured__c insertRecords2 = new PA_Pre_App_Life_Assured__c(Pre_Application__c = preAppLoop.Id,
                    CI_Assured__c = 'NO NML EVIDENCE NEEDED');
                insertRecords2.P2result = 'Full GPR, MER*, FBP, Ex ECG**, FQ';

                preAppCreateList.add(insertRecords2);



            }

		}
	
		System.debug('preApplist size = ' + preAppCreateList.size());
		
		if(preAppCreateList.size() > 0)
		insert preAppCreateList;
}

 
This was selected as the best answer
Abdul KhatriAbdul Khatri
Please check the code below. This is what I understood. Please correct the field names as per your need
 
trigger TestPreapptrigger on Pre_Application__c (after insert, after update){   

    List <PA_Pre_App_Life_Assured__c> preAppCreateList = new List <PA_Pre_App_Life_Assured__c>();

    for(Pre_Application__c preAppLoop : trigger.new){
        
		PA_Pre_App_Life_Assured__c insertRecords;

        if(  preAppLoop.Age_at_next_birthday__c >=17 && preAppLoop.Age_at_next_birthday__c<=35 && preAppLoop.PA_Life_Sum_Assured__c>= 0 && preAppLoop.PA_Life_Sum_Assured__c<=750000){
            
			insertRecords = new PA_Pre_App_Life_Assured__c(Pre_Application__c= preAppLoop.Id);
            insertRecords.Life_Assured__c = 'NO NML EVIDENCE NEEDED'
			insertRecords.P1result__c = 'MER* Full GPR FBP';
        
        } 

 		if ( preAppLoop.Age_at_next_birthday__c>=17 && preAppLoop.Age_at_next_birthday__c<=30 && preAppLoop.PA_CI_Sum_Assured__c>= 0 && preAppLoop.PA_CI_Sum_Assured__c<=450000  ){
            
            if(insertRecords == null)
            	insertRecords = new PA_Pre_App_Life_Assured__c(Pre_Application__c = preAppLoop.Id);
            
            insertRecords.CI_Assured__c = 'NO NML EVIDENCE NEEDED';
            insertRecords.P2result__c = 'Full GPR, MER*, FBP, Ex ECG**, FQ';            
		}     
        
        if(insertRecords != null)
            preAppCreateList.add(insertRecords);
       
    }

    System.debug('preApplist size = ' + preAppCreateList.size()); 
    
	if(!preAppCreateList.isEmpty())
    	insert preAppCreateList;
}

 
Abdul KhatriAbdul Khatri
So you didn't get the solution from any of us.
Gwirizanani SinoiaGwirizanani Sinoia
@ Abdul Khatri thank you very much for your help and support.
Abdul KhatriAbdul Khatri
Can you be so kind to mark that answer the best?
Gwirizanani SinoiaGwirizanani Sinoia
Hi Abdul Khatri  how do incorperate and update? ie if any changes happen to the parrent record a child must be updated accordingly.

Thanks once more
Abdul KhatriAbdul Khatri
Man, you rejected my solution so I am not sure what exactly you are looking for. I am sorry I think Waqar understood your solution so he will be the best to help out. Don't want to be rude but since my Solution didn't work your thoughts so I may guide you in a wrong direction.
Waqar Hussain SFWaqar Hussain SF
try below code for update.
 
trigger TestPreapptrigger on Pre_Application__c(after insert, after update) {

	if(trigger.isUpdate){
		list<PA_Pre_App_Life_Assured__c> RecToDelete = new list<PA_Pre_App_Life_Assured__c>();
		RecToDelete = [Select Id from PA_Pre_App_Life_Assured__c where Pre_Application__c IN Trigger.New.keyset()];
		
		if(RecToDelete.size() > 0)
		delete RecToDelete;
	}
		List < PA_Pre_App_Life_Assured__c > preAppCreateList = new List < PA_Pre_App_Life_Assured__c > ();
		for (Pre_Application__c preAppLoop: trigger.new) {

			if (preAppLoop.Age_at_next_birthday__c >= 17 && preAppLoop.Age_at_next_birthday__c <= 35 && preAppLoop.PA_Life_Sum_Assured__c >= 0 && preAppLoop.PA_Life_Sum_Assured__c <= 750000) {
				PA_Pre_App_Life_Assured__c insertRecords = new PA_Pre_App_Life_Assured__c(Pre_Application__c = preAppLoop.Id,
					Life_Assured__c = 'NO NML EVIDENCE NEEDED');
				insertRecords.P1_result__c = 'MER* Full GPR FBP';
				preAppCreateList.add(insertRecords);

			}

			if (preAppLoop.Age_at_next_birthday__c >= 17 && preAppLoop.Age_at_next_birthday__c <= 30 && preAppLoop.PA_CI_Sum_Assured__c >= 0 && preAppLoop.PA_CI_Sum_Assured__c <= 450000) {
				PA_Pre_App_Life_Assured__c insertRecords2 = new PA_Pre_App_Life_Assured__c(Pre_Application__c = preAppLoop.Id,
					CI_Assured__c = 'NO NML EVIDENCE NEEDED');
				insertRecords2.P2result = 'Full GPR, MER*, FBP, Ex ECG**, FQ';

				preAppCreateList.add(insertRecords2);

			}

		}

		System.debug('preApplist size = ' + preAppCreateList.size());
		
		if(preAppCreateList.size() > 0)
		insert preAppCreateList;
	
}

 
Gwirizanani SinoiaGwirizanani Sinoia
Hi  Waqar Hussain SF,

My working trigger is as follows. As you can see record insertion works perfectly well. However i am trying to add update functionality to it. 
Please help?. A few lines of test class will be good too.

Thanks so much


My trigger handler
public class PreAppLifeUpdateInsert {
    
    
       public static void myPreAppTable(List <Pre_Application__c> preApplication){
     

    List <PA_Pre_App_Life_Assured__c> preAppCreateList = new List <PA_Pre_App_Life_Assured__c>();

    for(Pre_Application__c preAppLoop : preApplication){
        PA_Pre_App_Life_Assured__c insertRecords;
        

       insertRecords = new PA_Pre_App_Life_Assured__c(Pre_Application__c = preAppLoop.Id); 
        // Begining of Life Prelife Table
        if(preAppLoop.Age_at_next_birthday__c>=17  && preAppLoop.Age_at_next_birthday__c<=35 && preAppLoop.PA_Life_Sum_Assured__c>= 0 && preAppLoop.PA_Life_Sum_Assured__c<=750000  ||
           preAppLoop.Age_at_next_birthday__c>=36  && preAppLoop.Age_at_next_birthday__c<=40 && preAppLoop.PA_Life_Sum_Assured__c>= 0 && preAppLoop.PA_Life_Sum_Assured__c<=600000  
          
              { insertRecords.Life_Assured__c    = 'NO NML EVIDENCE NEEDED';   } 
        
        //begining of Life NSE_c update
        if(preAppLoop.Age_at_next_birthday__c>=17 && preAppLoop.Age_at_next_birthday__c<=35 && preAppLoop.PA_Life_Sum_Assured__c>= 750001  && preAppLoop.PA_Life_Sum_Assured__c<=1000000 ||
           preAppLoop.Age_at_next_birthday__c>=36 && preAppLoop.Age_at_next_birthday__c<=40 && preAppLoop.PA_Life_Sum_Assured__c>= 600001  && preAppLoop.PA_Life_Sum_Assured__c<=1000000 
           
        { insertRecords.Life_Assured__c    = 'NSE*'; } //end of Life (NSE_c update)
        
        //begining of Life NSE_c NML_GPR__c  update
        if (preAppLoop.Age_at_next_birthday__c>=17 && preAppLoop.Age_at_next_birthday__c<=35 && preAppLoop.PA_Life_Sum_Assured__c>= 1000001  && preAppLoop.PA_Life_Sum_Assured__c<=1500000 ||
            preAppLoop.Age_at_next_birthday__c>=36 && preAppLoop.Age_at_next_birthday__c<=40 && preAppLoop.PA_Life_Sum_Assured__c>= 1000001  && preAppLoop.PA_Life_Sum_Assured__c<=1500000 
            
        { insertRecords.Life_Assured__c    = 'NSE* NML GPR';} // end of Life NSE_c NML_GPR__c  update
        
         //begining of Life NSE* NML GPR HIV~  update
         if (preAppLoop.Age_at_next_birthday__c>=17 && preAppLoop.Age_at_next_birthday__c<=35 && preAppLoop.PA_Life_Sum_Assured__c>= 1500001  && preAppLoop.PA_Life_Sum_Assured__c<=2000000 ||
             preAppLoop.Age_at_next_birthday__c>=36 && preAppLoop.Age_at_next_birthday__c<=40 && preAppLoop.PA_Life_Sum_Assured__c>= 1500001  && preAppLoop.PA_Life_Sum_Assured__c<=2000000 )
         {insertRecords.Life_Assured__c    = 'NSE* NML GPR HIV~';}//End of Life NSE* NML GPR HIV~  update
        
        //begining of Life NML GPR HIV~ FQ MER* FBP
         if (preAppLoop.Age_at_next_birthday__c>=17 && preAppLoop.Age_at_next_birthday__c<=35 && preAppLoop.PA_Life_Sum_Assured__c>= 2000001  && preAppLoop.PA_Life_Sum_Assured__c<=3500000 ||
             preAppLoop.Age_at_next_birthday__c>=36 && preAppLoop.Age_at_next_birthday__c<=40 && preAppLoop.PA_Life_Sum_Assured__c>= 2000001  && preAppLoop.PA_Life_Sum_Assured__c<=3500000 )
         {insertRecords.Life_Assured__c    = 'NML GPR HIV~ FQ MER* FBP';}//end of NML GPR HIV~ FQ MER* FBP update

          //begining of Life NML GPR HIV~ FQ MER* FBP FC  update
          if(preAppLoop.Age_at_next_birthday__c>=17 && preAppLoop.Age_at_next_birthday__c<=35 && preAppLoop.PA_Life_Sum_Assured__c>= 3500001  && preAppLoop.PA_Life_Sum_Assured__c<=7500000 ||
             preAppLoop.Age_at_next_birthday__c>=36 && preAppLoop.Age_at_next_birthday__c<=40 && preAppLoop.PA_Life_Sum_Assured__c>= 3500001  && preAppLoop.PA_Life_Sum_Assured__c<=7500000 ||
             preAppLoop.Age_at_next_birthday__c>=46 && preAppLoop.Age_at_next_birthday__c<=50 && preAppLoop.PA_Life_Sum_Assured__c>= 3500001  && preAppLoop.PA_Life_Sum_Assured__c<=4000000 )
          {insertRecords.Life_Assured__c    = 'NML GPR HIV~ FQ MER* FBP FC';} //end of NML GPR HIV~ FQ MER* FBP FC update
        
         preAppCreateList.add(insertRecords);} //End of for loop
  insert preAppCreateList;
       
       }// end of Method

}// end of class

My trigger class

trigger PreAppTrigger on Pre_Application__c ( after insert, after update) {
    PreAppLifeUpdateInsert.myPreAppTable(trigger.new);
    

}