function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Matt TindallMatt Tindall 

Trigger Error.

Hello,

I am trying to create a trigger that will select the quantity of products that an opportunity has using a specific criteria.

The issue I'm having is:

1) I am un sure on how to make the query string match the opportunity from the list.
2) The string i am using is giving the following error "Illegal assignment from LIST<aggregateResult> to Decimal

Here is the code i am trying to use:

trigger CountWasteBins on Opportunity (after UPDATE) {
// List all customers
list<Opportunity> CustomerList = [SELECT id, General_Waste_Containers__c FROM Opportunity];

// Update all customers from the list
public void UpdateField()
{
for(Opportunity o : CustomerList)
{
o.General_Waste_Containers__c = [SELECT sum(Quantity) FROM OpportunityLineItem WHERE PricebookEntry.Product2.Family = 'General Waste'];

}
}

public void UpdateCustomerList()
{
// Update List
update CustomerList;
}
}
Blake TanonBlake Tanon
I don't think this is written the way you want, try this.  You're also going to want to make sure that your opportunities are being updated when a lineitem is created/edited/delete so this trigger fires.

    set<id> opSet = new set<id>();
    for(opportunity o:trigger.new){
     opSet.add(o.id);
    }

    map<id,decimal> aggMap = new map<id,decimal>();
    for(aggregateResult r: [SELECT sum(Quantity), OpportunityId FROM OpportunityLineItem WHERE PricebookEntry.Product2.Family = 'General Waste' GROUP BY OpportunityId]){
     aggMap.put(string.valueof(r.get('OpportunityId')),decimal.valueof(r.get('expr0')));
    }
   


    for(opportunity o:trigger.new){
     o.General_Waste_Containers__c = 0;
     if(aggMap.get(o.id) != null){
      o.General_Waste_Containers__c = aggMap.get(o.id);
     }
    }
Matt TindallMatt Tindall

Hi Blake,

Thanks for your reply, but after i have tried this code I'm getting an error on line 10 "Variable does not exist: decimal".

 

aggMap.put(string.valueof(r.get('OpportunityId')),decimal.valueof(r.get('expr0')));

 

Any ideas?

Blake TanonBlake Tanon
Sorry, i always make this mistake - there isn't a valueof method for decimal - replace it with this...

aggMap.put(string.valueof(r.get('OpportunityId')),double.valueof(r.get('expr0')));