+ Start a Discussion
Gwirizanani SinoiaGwirizanani Sinoia 

Help in adding trigger update

Hi everyone. I am a newbie..
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 in advance


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);
    

}
 
Best Answer chosen by Gwirizanani Sinoia
Hemant_SoniHemant_Soni
Please try Below code and i am not 100% sure that it would work because i am not able to test it so please test it and let me know if you are facing any issue.
public class PreAppLifeUpdateInsert {
    public static boolean preventRecusion = false;
public static boolean getpreRec(){
return preventRecusion;
}
public static void setPreRec(){
preventRecusion = true;
}
    
       public static void myPreAppTable(List <Pre_Application__c> preApplication){
     if(!getpreRec()){
setPreRec();
    List <PA_Pre_App_Life_Assured__c> preAppCreateList = new List <PA_Pre_App_Life_Assured__c>();
				set<Id> setAppId = new set<Id>();
				map<Id,PA_Pre_App_Life_Assured__c> mapAppIdWiseApp_Life_Assured = new map<Id,PA_Pre_App_Life_Assured__c>();
				for(Pre_Application__c preAppLoop : preApplication){
					setAppId.add(preAppLoop.Id);
				}
				
				for(PA_Pre_App_Life_Assured__c oPAL : [Select Id,Name,Pre_Application__c From PA_Pre_App_Life_Assured__c Where Pre_Application__c IN : setAppId]){
					mapAppIdWiseApp_Life_Assured.put(oPAL.Pre_Application__c,oPAL);
				}
				
    for(Pre_Application__c preAppLoop : preApplication){
        PA_Pre_App_Life_Assured__c insertRecords;
        if(!mapAppIdWiseApp_Life_Assured.IsEmpty() && mapAppIdWiseApp_Life_Assured.containsKey(preAppLoop.Id)){
									insertRecords = mapAppIdWiseApp_Life_Assured.get(preAppLoop.Id);
								}else{
									insertRecords = new PA_Pre_App_Life_Assured__c();
									insertRecords.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
  upsert preAppCreateList;
       
       }// end of Method
}
}// end of class

 

All Answers

Hemant_SoniHemant_Soni
Hi,
According to me it is already working in both update and insert event scenarios with same functionality.
Thanks
 
Gwirizanani SinoiaGwirizanani Sinoia
its not updating tried numerous times.

thanks

 
Gwirizanani SinoiaGwirizanani Sinoia
when the record changes on the parent its suppose to update the child record 
Hemant_SoniHemant_Soni
I mean whenever you are inserting or updating "Pre_Application__c" then it will always insert new record of "PA_Pre_App_Life_Assured__c".
If You Can see in your trigger there are no condition for insert or update.
If you can explain what you need to in update then i will suggest you a solution.
 
Gwirizanani SinoiaGwirizanani Sinoia
Thanks
What I am trying to do is insert a child record (PA_Pre_App_Life_Assured__c)  when a parent records (Pre_Application__c) is created based on conditions . Also update PA_Pre_App_Life_Assured__c when Pre_Application__c is updated. At the moment its inserting but not updating. As a new devoloper i think i might be over complicating things . Your help is most appreciated. thanks again
Gwirizanani SinoiaGwirizanani Sinoia
forgot to include that this is based on age field Age_at_next_birthday__c and three amont fields PA_Life_Sum_Assured__c,  PA_CI_Sum_Assured__c and PA_PP1_Sum_Assured__c on the Pre_Application__c(parent object) . The results of these fields will create and update a child record and populate Life_Assured__c, CI_Assured__c and PPI_Assured__c fields on the PA_Pre_App_Life_Assured__c Child object.
Hemant_SoniHemant_Soni
Please try Below code and i am not 100% sure that it would work because i am not able to test it so please test it and let me know if you are facing any issue.
public class PreAppLifeUpdateInsert {
    public static boolean preventRecusion = false;
public static boolean getpreRec(){
return preventRecusion;
}
public static void setPreRec(){
preventRecusion = true;
}
    
       public static void myPreAppTable(List <Pre_Application__c> preApplication){
     if(!getpreRec()){
setPreRec();
    List <PA_Pre_App_Life_Assured__c> preAppCreateList = new List <PA_Pre_App_Life_Assured__c>();
				set<Id> setAppId = new set<Id>();
				map<Id,PA_Pre_App_Life_Assured__c> mapAppIdWiseApp_Life_Assured = new map<Id,PA_Pre_App_Life_Assured__c>();
				for(Pre_Application__c preAppLoop : preApplication){
					setAppId.add(preAppLoop.Id);
				}
				
				for(PA_Pre_App_Life_Assured__c oPAL : [Select Id,Name,Pre_Application__c From PA_Pre_App_Life_Assured__c Where Pre_Application__c IN : setAppId]){
					mapAppIdWiseApp_Life_Assured.put(oPAL.Pre_Application__c,oPAL);
				}
				
    for(Pre_Application__c preAppLoop : preApplication){
        PA_Pre_App_Life_Assured__c insertRecords;
        if(!mapAppIdWiseApp_Life_Assured.IsEmpty() && mapAppIdWiseApp_Life_Assured.containsKey(preAppLoop.Id)){
									insertRecords = mapAppIdWiseApp_Life_Assured.get(preAppLoop.Id);
								}else{
									insertRecords = new PA_Pre_App_Life_Assured__c();
									insertRecords.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
  upsert preAppCreateList;
       
       }// end of Method
}
}// end of class

 
This was selected as the best answer
Gwirizanani SinoiaGwirizanani Sinoia
Thanks very much @Hemant_Soni.

 is it possible to start me up with a test class?
thanks once again
Gwirizanani SinoiaGwirizanani Sinoia
Many thanks @Hemant_Soni.