-2

I have a trigger bellow which calculates a sum of field in a related field in custom object . I am getting an error massage Variable does not exist:House_Improvements__c. Any ideas how can I resolve the issue? Thanks

trigger SumCalculations 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 = 0;
    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]){
            sum = 0;
            for(House_Improvements__c   p : q.House_Improvements__r)
                sum = sum + p.House_Improvements__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]){
            sum = 0;
            for(House_Improvements__c  p : q.House_Improvements__r)
                sum = sum + p.House_Improvements__c ;
            q.SUM_Calculations__c  = sum;
           PropertyToUpdate .add(q);
        } 
        try{
            update PropertyToUpdate ;
        }Catch(Exception e){
            System.debug('Exception :'+e.getMessage());
        }
    }
}
  • 2
    Please edit the post to include the exact error message and line # it references. Also, do you have an object called House_Improvements__c with a field called House_Improvements__c? – Kris Goncalves Mar 27 at 19:01
  • KrisG is suggesting it might be the line sum = sum + p.House_Improvements__c ; that is causing this failure, I believe. – Phil W Mar 27 at 19:02
  • I would also suggest that you review best practice around exception handling; what you have here will likely lead to data inconsistency in the org since you allow house improvements to be inserted/updated/deleted even if you are unable to also update the property data. – Phil W Mar 27 at 19:05
2

You can't add a List<SObject> to a Decimal, so sum = sum + p.House_Improvements__c would fail even if you used the correct reference to the child list (House_Improvements__r).

Incorrect:

sum = sum + p.House_Improvements__c;

Correct

for (House_Improvements__c child : p.House_Improvements__r)
    sum += child.Price__c;

Please note, you should always use singular object names. Your code would be much less confusing if you had done so here. If it is not too late, rename your object to House_Improvement__c.

| improve this answer | |
  • I would also add that using single character variable names is a bad practice... you might expect "p" to represent a property, but here it represented a home improvement... use an IDE for development, type the variable names to be meaningful and use the IDE's auto-completion to still keep the amount of typing to a minimum. – Phil W Mar 27 at 19:09
  • Totally agree with you on that point. – Adrian Larson Mar 27 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.