You need to sign in to do that
Don't have an account?
Gwirizanani 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
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
All Answers
Otherwise you can create formula field on child record to auto populate the P1Result, P2Result and P3Result based on parent object.
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
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;
Thanks once more
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);
}