You need to sign in to do that
Don't have an account?
Mary5478
Separate logic from trigger
Hello guys,
I have a trigger and I want to separate it into trigger and class. Please help.Thanks. Bellow is my trigger
trigger SumCalculation on House_Improvements__c (after insert,after update, after delete,after undelete) {
List<id> PropertyIds = new List<id>();
if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
For(House_Improvements__c himp1 : Trigger.new){
PropertyIds.add(himp1.Property__c);
}
}
if(Trigger.isDelete){
For(House_Improvements__c himp1 : Trigger.old){
PropertyIds.add(himp1.Property__c);
}
}
List<Property__c> PropertyToUpdate = new List<Property__c>();
decimal sum;
if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
For (Property__c q: [SELECT id,SUM_Calculations__c, (SELECT id, Price__c FROM House_Improvements__r) FROM Property__c WHERE id =:PropertyIds]){
decimal sum=0;
for(House_Improvements__c p : q.House_Improvements__r)
sum = sum + p.Price__c ;
q.SUM_Calculations__c = sum;
PropertyToUpdate .add(q);
}
try{
update PropertyToUpdate ;
}Catch(Exception e){
System.debug('Exception :'+e.getMessage());
}
}
if(Trigger.isDelete){
For(Property__c q : [SELECT SUM_Calculations__c ,(SELECT id,Price__c FROM House_Improvements__r) FROM Property__c WHERE id =: PropertyIds]){
decimal sum = 0;
for(House_Improvements__c p : q.House_Improvements__r)
sum = sum + p.Price__c ;
q.SUM_Calculations__c = sum;
PropertyToUpdate .add(q);
}
try{
update PropertyToUpdate ;
}Catch(Exception e){
System.debug('Exception :'+e.getMessage());
}
}
}
I have a trigger and I want to separate it into trigger and class. Please help.Thanks. Bellow is my trigger
trigger SumCalculation on House_Improvements__c (after insert,after update, after delete,after undelete) {
List<id> PropertyIds = new List<id>();
if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
For(House_Improvements__c himp1 : Trigger.new){
PropertyIds.add(himp1.Property__c);
}
}
if(Trigger.isDelete){
For(House_Improvements__c himp1 : Trigger.old){
PropertyIds.add(himp1.Property__c);
}
}
List<Property__c> PropertyToUpdate = new List<Property__c>();
decimal sum;
if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
For (Property__c q: [SELECT id,SUM_Calculations__c, (SELECT id, Price__c FROM House_Improvements__r) FROM Property__c WHERE id =:PropertyIds]){
decimal sum=0;
for(House_Improvements__c p : q.House_Improvements__r)
sum = sum + p.Price__c ;
q.SUM_Calculations__c = sum;
PropertyToUpdate .add(q);
}
try{
update PropertyToUpdate ;
}Catch(Exception e){
System.debug('Exception :'+e.getMessage());
}
}
if(Trigger.isDelete){
For(Property__c q : [SELECT SUM_Calculations__c ,(SELECT id,Price__c FROM House_Improvements__r) FROM Property__c WHERE id =: PropertyIds]){
decimal sum = 0;
for(House_Improvements__c p : q.House_Improvements__r)
sum = sum + p.Price__c ;
q.SUM_Calculations__c = sum;
PropertyToUpdate .add(q);
}
try{
update PropertyToUpdate ;
}Catch(Exception e){
System.debug('Exception :'+e.getMessage());
}
}
}
Please refer below code and comments inline:
All Answers
Please refer below code and comments inline:
Please try the below trigger and class:
First create the Apex class then refer the class and method in Trigger.
Apex Trigger:
Apex Class:
Thanks,
Maharajan.C