1

I have a method that is supposed to count custom attachment object records and update a list of rule records indicating how many attachment records of a category exist.

This works, however only for one attachment category at a time. For instance, if I have 1 category and 10 attachments of the same all rule records of the same category will update.

If I have another attachment of a different category it would not update its pertinent rule records. Something is off and I need your expertise.

public Static void updateCategoryQty(){

   Set<Id> prjId = new Set<Id>();  
   Set<Id> prjRuleAttaCatId = new Set<Id>();                                         
   List<Project_Rule__c> updatePrjRuleLst = new list<Project_Rule__c>();   

   for(Project__c p : (List<Project__c>)Trigger.New) {                
             prjId.add(p.Id);             
   } 

   List <Project_Rule__c> prjRuleLst = [select id,name,Measure_Category__c,Attachment_Category__c from Project_Rule__c where Project__c =: prjId];

   for(Project_Rule__c pr : prjRuleLst){
       prjRuleAttaCatId.add(pr.Attachment_Category__c);
   }

   AggregateResult[] attaQtyTotalRst = [SELECT count(Id)attaQty,Attachment_Category__c
                      FROM Attachment__c 
                      where Project__c =: prjId
                      and Attachment_Category__c != null //IN : prjRuleAttaCatId
                      and Stage__c = 'Approved'
                      group by Attachment_Category__c];

   for (AggregateResult ar : attaQtyTotalRst)  {
                 System.debug('The AggregateResult attaQtyTotalRst equal:'+ (decimal) attaQtyTotalRst[0].get('attaQty') + ' for ' + attaQtyTotalRst[0].get('Attachment_Category__c'));
             }             

   for(Project_Rule__c pr: prjRuleLst){                          
       if(prjRuleLst.size()> 0){
           Project_Rule__c prjRule = new Project_Rule__c(Id = pr.id);

           for(AggregateResult att: attaQtyTotalRst){  
              if(pr.Measure_Category__c == null && pr.Attachment_Category__c != null && pr.Attachment_Category__c == (Id)attaQtyTotalRst[0].get('Attachment_Category__c')){
                       prjRule.Quantity_Entered__c = (decimal) attaQtyTotalRst[0].get('attaQty');
              }
           }             

           updatePrjRuleLst.add(prjRule);
       }

       Map<Id,Project_Rule__c> updatePrMap = new Map<Id,Project_Rule__c>();

       updatePrMap.putall(updatePrjRuleLst);
       if(updatePrMap.size()>0){
        update updatePrMap.values();
       }                                             
    } 
}
1
  • N.B. if(prjRuleLst.size()> 0) and if(updatePrMap.size()>0) are superfluous
    – cropredy
    Jun 5 '19 at 16:42
2

Edit: Solution is here.

for(AggregateResult att: attaQtyTotalRst){  
    if(pr.Measure_Category__c == null && pr.Attachment_Category__c != null && 
       pr.Attachment_Category__c == (Id)att.get('Attachment_Category__c')){
       prjRule.Quantity_Entered__c = (decimal) att.get('attaQty');
    }
} 

Looks like you're looping through the attaQtyTotalRst list, but when you get to your if, you're only looking at the first att element for the category, instead of the item you're on. Same within the line inside the if statement. The loop itself doesn't seem to use the att variable.

Also, are we sure about the line: List <Project_Rule__c> prjRuleLst = [select id,name,Measure_Category__c,Attachment_Category__c from Project_Rule__c where Project__c =: prjId];? prjId is a Set, should that be the IN operator? Is that returning more than one result when expected, or no more than one?

The second query also has where Project__c =: prjId.

4
  • Hi, I made the change but this didn't change the result since there is only 1 project id at any given time. I have several debug statements and everything seems right including the id to update and the proper qty for all records. They are just not getting set for all category rules, almost as if it was exiting the loop early. I know I'm missing something but can't put my finger on it. Any other thoughts? Thanks!
    – Snights
    Jun 5 '19 at 17:12
  • Oh! Maybe this... for(AggregateResult att: attaQtyTotalRst){ if(... (Id)attaQtyTotalRst[0].get('Attachment_Category__c')){ Looks like you're looping through the attaQtyTotalRst list, but when you get to your if, you're only looking at the first att element for the category, instead of the item you're on. Same within the line inside the if statement. The loop itself doesn't seem to use the att variable.
    – pmiller3
    Jun 5 '19 at 17:24
  • You got it! Please edit your response and I'll mark it resolved - I used att as follows: for(AggregateResult att: attaQtyTotalRst){ if(pr.Measure_Category__c == null && pr.Attachment_Category__c != null && pr.Attachment_Category__c == (Id)att.get('Attachment_Category__c')){ prjRule.Quantity_Entered__c = (decimal) att.get('attaQty'); } }
    – Snights
    Jun 5 '19 at 19:14
  • Nice! Looks like it... ok, edited. Thanks!
    – pmiller3
    Jun 5 '19 at 19:17

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.