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
Hughbert StrongHughbert Strong 

Apex Trigger - Illegal assignment from List to Decimal

Hello friends,

This is my second attempt at writing an apex trigger. I receiving an error when I save my trigger, "Illegal assignment from List to Decimal."
Hopefully someone can give me some pointers.

Here's my code:
trigger SRTake1 on Stack_Rank__c (after update) {
    Date myDate = Date.newInstance(2017, 01, 01);
    
    for( Stack_Rank__c s: Trigger.new){
        if(s.SR_Date__c == myDate && s.SR_Main_Sales__c == null){
            try{
                s.SR_Main_Sales__c=[Select OwnerID, SUM(quota_credit__c) from Opportunity Where OwnerID in (Select User__c From Stack_Rank__c Where SR_Date__c = 2017-01-01) AND CloseDate >= 2017-01-01 AND CloseDate <= 2017-01-31 Group By OwnerId];
            
            }
            catch(QueryException ex) {
                //todo add other entitlements
            }
            
        }
    }

}

Hughbert StrongHughbert Strong
I just adjusted the code. Still need help please!

trigger SRTake1 on Stack_Rank__c (after update) {
    Date myDate = Date.newInstance(2017, 01, 01);
    
    for( Stack_Rank__c s: Trigger.new){
        if(s.SR_Date__c == myDate && s.SR_Main_Sales__c == null){
            try{
                s.SR_Main_Sales__c=[Select SUM(quota_credit__c) from Opportunity Where OwnerID in (Select User__c From Stack_Rank__c Where SR_Date__c = 2017-01-01) AND CloseDate >= 2017-01-01 AND CloseDate <= 2017-01-31 AND StageName ='Closed Won'];
            
            }
            catch(QueryException ex) {
                //todo add other entitlements
            }
            
        }
    }

}
Jarosław KołodziejczykJarosław Kołodziejczyk
"Illegal assignment from List to Decimal." is triggered on the soql select.

Even though you are using aggregate function it's still returning a list.
 
AggregateResult[] groupedResults
  = [SELECT AVG(Amount)aver FROM Opportunity];
Object avgAmount = groupedResults[0].get('aver');

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_agg_fns.htm

In your case creating a list, then getting [0] element and assigning it to s.SR_Main_Sales__c should do the trick.