• Josh Schwerdtfeger
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 15
    Replies
Need some help, i'm in over my head.  I've got a custom field on a custom object and have the following apex running on a trigger.  I don't know how or what would be the best way to execute this field update automatically.  On one hand it would be great to exeute the trigger every night at midnight, as i will be duplicatingt he field and query for each month.  Any help or a point in the right direction would be great.
trigger WonNov on Course__c (before update) {

    Set<Id> setRecordId = new Set<Id>();
    
    for (Course__c obj : Trigger.new)
    {
        setRecordId.add(obj.id) ;
    }
    
    AggregateResult[] groupedResults = [ Select SUM (Amount) sum, Course_Lookup__c from Opportunity WHERE Courseid__c in :setRecordId AND Opportunity_Type__c ='Outing' AND Probability >94.00 AND Event_Date__c = THIS_YEAR AND Event_Month__c = '11_November' GROUP BY Course_Lookup__c ];
    
    Map<String , AggregateResult > mapAG = new Map<String , AggregateResult >();
    for(AggregateResult ar : groupedResults)
    {
        mapAG.put(String.valueOf(ar.get('Course_Lookup__c')) , ar );
    }
    
    for (Course__c obj : Trigger.new)
    {
    
        if( mapAG.containsKey(obj.id) )
        {
            AggregateResult ar = mapAG.get(obj.id);
            double sum =double.valueOf(ar.get('sum'));
            obj.Won_Nov_Outings__c = decimal.valueOf(sum);
            
        }
    }
}

 
I have Opportunity as the parent object to a custom object Course.  I want to build a soql that updates through a trigger a field on the course object, specific to each course.  This is what i've got so far but i am not finding how to pass the course object into the where of the query.  Any help would be great.
trigger WonNov on Course__c (before update) {
   AggregateResult[] groupedResults = [
    Select SUM (Amount) sum from Opportunity WHERE Courseid__c = :recordId];
        for (Course__c obj : Trigger.new)
        {
        double sum =
double.valueOf(groupedResults[0].get('sum'));
        obj.Won_Nov_Outings__c =
decimal.valueOf(sum);
            }
    }
I am trying to trigger a field update here's the code I have so far.  Won_Nov_Outings__C is a currency data field.  I can save the trigger, but when i update an opportunity i get this error:
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger testsum2 caused an unexpected exception, contact your administrator: testsum2: execution of AfterUpdate caused by: System.SObjectException: Invalid field sum for AggregateResult: Trigger.testsum2: line 7, column 1

Here is my code:
 
trigger testsum on Opportunity (after update) {
    AggregateResult[] groupedResults = [
    Select SUM (Amount) from Opportunity WHERE Opportunity_Type__c = 'Outing' and Event_Year__C = '2017' AND Event_Month__c = '11_November' AND Probability < 90];
        for (Opportunity obj : Trigger.new)
        {
        double sum =
double.valueOf(groupedResults[0].get('sum'));
        obj.Won_Nov_Outings__c =
decimal.valueOf(sum);
            }
    }

 
Need some help, i'm in over my head.  I've got a custom field on a custom object and have the following apex running on a trigger.  I don't know how or what would be the best way to execute this field update automatically.  On one hand it would be great to exeute the trigger every night at midnight, as i will be duplicatingt he field and query for each month.  Any help or a point in the right direction would be great.
trigger WonNov on Course__c (before update) {

    Set<Id> setRecordId = new Set<Id>();
    
    for (Course__c obj : Trigger.new)
    {
        setRecordId.add(obj.id) ;
    }
    
    AggregateResult[] groupedResults = [ Select SUM (Amount) sum, Course_Lookup__c from Opportunity WHERE Courseid__c in :setRecordId AND Opportunity_Type__c ='Outing' AND Probability >94.00 AND Event_Date__c = THIS_YEAR AND Event_Month__c = '11_November' GROUP BY Course_Lookup__c ];
    
    Map<String , AggregateResult > mapAG = new Map<String , AggregateResult >();
    for(AggregateResult ar : groupedResults)
    {
        mapAG.put(String.valueOf(ar.get('Course_Lookup__c')) , ar );
    }
    
    for (Course__c obj : Trigger.new)
    {
    
        if( mapAG.containsKey(obj.id) )
        {
            AggregateResult ar = mapAG.get(obj.id);
            double sum =double.valueOf(ar.get('sum'));
            obj.Won_Nov_Outings__c = decimal.valueOf(sum);
            
        }
    }
}

 
I have Opportunity as the parent object to a custom object Course.  I want to build a soql that updates through a trigger a field on the course object, specific to each course.  This is what i've got so far but i am not finding how to pass the course object into the where of the query.  Any help would be great.
trigger WonNov on Course__c (before update) {
   AggregateResult[] groupedResults = [
    Select SUM (Amount) sum from Opportunity WHERE Courseid__c = :recordId];
        for (Course__c obj : Trigger.new)
        {
        double sum =
double.valueOf(groupedResults[0].get('sum'));
        obj.Won_Nov_Outings__c =
decimal.valueOf(sum);
            }
    }
I am trying to trigger a field update here's the code I have so far.  Won_Nov_Outings__C is a currency data field.  I can save the trigger, but when i update an opportunity i get this error:
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger testsum2 caused an unexpected exception, contact your administrator: testsum2: execution of AfterUpdate caused by: System.SObjectException: Invalid field sum for AggregateResult: Trigger.testsum2: line 7, column 1

Here is my code:
 
trigger testsum on Opportunity (after update) {
    AggregateResult[] groupedResults = [
    Select SUM (Amount) from Opportunity WHERE Opportunity_Type__c = 'Outing' and Event_Year__C = '2017' AND Event_Month__c = '11_November' AND Probability < 90];
        for (Opportunity obj : Trigger.new)
        {
        double sum =
double.valueOf(groupedResults[0].get('sum'));
        obj.Won_Nov_Outings__c =
decimal.valueOf(sum);
            }
    }