0
votes

My title for this post might be little confusing but i will try to make it as clear as possible. I am running into an issue with Apex trigger. We have a custom object called Receivables (Managed Packaged). Each Opportunity relates with one or more receivable record. Master Detailed Relationship is not an option since Receivable object is managed packaged.

Here is my logic:

Create a trigger on Opportunity (insert and/or update) > Loop all receivables which have matching id with triggered opportunity id and Receivable Opportunity field id (This is an Opportunity look up field in Receivables) > Use aggregated to sum the amount > Auto Populate Total Commission field.

Trigger does not throw any error but it is not auto populating as well.

trigger newRecaivables on Opportunity (after insert, after update) 
{
    set<Id> oppid = new set<id>();

    list<opportunity> opplist = [select id  from opportunity where id in : oppid ];

    for(Opportunity Opp : trigger.new)
    {
        List<aggregateResult> results = [select Fees_Received_Category__c ,sum(McaApp__Amount__c) total from McaApp__Receivable__c Where McaApp__Opportunity__c in:oppid  group by Fees_Received_Category__c];

        for(AggregateResult ar : results)
        {
            if (String.valueOf(ar.get('Fees_Received_Category__c'))=='Received')
            {
                Opp.Total_Commission__c = String.valueOf(ar.get('total'));
            }
        }
    }
}

Any help would be appreciated.

1
oppid is empty. You are not adding values to it and querying using that set.Reshma
Hi Reshma, Thank you for the clue. I was able to make it work.Burak Yurttas
Try to avoid SOQL inside for loopReshma

1 Answers

0
votes

Your trigger should fire on before insert, before update.

Since you're operating on the trigger.new enumeration, that will set the proper value when the object is either first created or updated separately.