Activity Forums Salesforce® Discussions How would anyone have an Apex trigger that simply rolls up the Opporunty Primary partner name up to the Opportunity record in Salesforce?

  • Deepak

    Member
    December 10, 2019 at 2:28 pm

    trigger rollupopp on Opportunity (after delete,after update,after insert,after undelete) {

    set<ID>AccIds = new set<ID>();

    if(trigger.isinsert || trigger.isundelete){
    for(opportunity opp : trigger.new){
    AccIds.add(opp.AccountId);
    }
    }
    if(trigger.isdelete){
    for(opportunity opp : trigger.old){
    AccIds.add(opp.AccountId);
    }
    }

    if(trigger.isupdate){
    for(opportunity opp:trigger.new){
    AccIds.add(opp.AccountId);
    if(trigger.oldmap.get(opp.id).AccountId != opp.AccountId && trigger.oldmap.get(opp.id).AccountId != null ){
    AccIds.add(trigger.oldmap.get(opp.id).AccountId);
    }
    }
    }
    map<id,double> amtmap = new map<id,double>();
    for(aggregateresult ag : [select AccountId ,SUM(Opty_Amount_C) SOA,count(id) cc from opportunity where AccountId in:AccIds AND RecordType.Name = 'Revenue Type' group by AccountId]){
    amtmap.put((ID)ag.get('AccountId'), double.valueof(ag.get('SOA')));
    // amtmap.put((ID)ag.get('AccountId'), double.valueof(ag.get('cc')));
    }
    list<account>acclist = new list<account>();

    for(id iid : AccIds){
    account acnt = new account(id=iid);
    if(amtmap.containskey(iid)){
    acnt.Total_Opty_Amount_C = amtmap.get(iid);
    }else{
    acnt.Total_Opty_Amount_C = 0;
    }
    acclist.add(acnt);
    }

    if(acclist.size()>0){
    update acclist;
    }

    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos