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
Mike Broughton 4Mike Broughton 4 

summary of opportunity from subscriptions

Hi, I'm new here and just starting out so apologise if this seems like a remedial question,

I currently have a custom object for subscription packages (Subscription_Type__c) within the subscription there is a field that details the cost of the subscription (Estimated_Monthly_Cost__c). I want my users to be able to add mutiple subscriptions and the oppertunities amount field (Amount) automatically total all the subsriptions thus giving me a total income for an oppertunity?

Thanks for any help,

Mike
bhanu_prakashbhanu_prakash
Hi Mike
Mark as best answer, If it resloves !!​
trigger updateMaxtotal on Subscription_Type__c (after insert, after update, after undelete, after delete) {
    List<Subscription_Type__c> Subscription_Type__cs = Trigger.isDelete ? Trigger.old : Trigger.new;
    Set<Id> oppids = new Set<Id>();   
    for (Subscription_Type__c c : Subscription_Type__cs) {
       if (c.Opportunity Id != null) {
            oppids.add(c.Opportunity Id);
        }
    }   
    List<Opportunity > acc = new List<Opportunity >();    
    for (AggregateResult AggRes : [SELECT Opportunity Id accid, sum(Estimated_Monthly_Cost__c) total FROM Subscription_Type__c WHERE Opportunity Id in: oppids 
                               GROUP BY Opportunity Id]){
        Opportunity  opp = new Opportunity ();
        opp.Id = (Id) AggRes.get('accid');
        opp.Amount = (Decimal) AggRes.get('total');
        acc.add(opp);
        system.debug('Error'+AggRes);
    }    
    update acc;
}

Check these link
https://developer.salesforce.com/forums/?id=906F0000000AeAyIAK

Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com (https://www.forcelearn.com/)  ​ 
Ajay K DubediAjay K Dubedi
Hi Mike,

Please refer to the below code. Hope it helps you.
 
trigger updateOpportunityField on Subscription_Type__c  (after insert, after update,after delete)
{
     Set<Id> subscriptionIdSet = new Set<Id>();
    
     if(trigger.isInsert || trigger.isUpdate)
     {
     for(Subscription_Type__c sub :trigger.new)
     {
       if(sub.OpportunityId!=NULL)
       {
        subscriptionIdSet.add(sub.OpportunityId);
       }
     }
     }
    
    Map<Id,Double> oppmap = new Map<Id,Double>();
   
    for(aggregateresult ar :[ SELECT OpportunityId , sum(Estimated_Monthly_Cost__c) FROM Subscription_Type__c WHERE OpportunityId IN : subscriptionIdSet GROUP BY OpportunityId])
    {
   
    oppmap.put((Id)ar.get('OpportunityId'),(double)ar.get('expr0'));
   
    }
   
    List<Opportunity> opportunitytoupdate = new List<Opportunity>();
   
    for( Opportunity op :[SELECT Id, Amount FROM Opportunity WHERE Id IN : subscriptionIdSet])
    {
     double totalcost= oppmap.get(op.Id);
     op.Amount = totalcost;
     opportunitytoupdate.add(op);
     }
    
     update opportunitytoupdate;
    
     }
Please select as best answer if it helps you.

Thank You,
Ajay Dubedi