Forum Replies Created

  • Ashi

    Member
    January 15, 2018 at 6:39 am in reply to: Help me on the given Salesforce Trigger Scenario

    Hey sajid,

    trigger sumcontact on Contact (after insert , after update) {
    list<account> updateacc = new list<account>();
    set<id> aid = new set<id>();
    for(contact con : trigger.new)
    {
    aid.add(con.accountid);
    }
    list<account> acclist =[select id , name,Rollup_Amount__c ,(select id, name,Amount_X__c,Type__c ,Amount_Y__c from contacts ), Rollup_Amount_X__c ,Rollup_Amount_Y__c from account where id in : aid];
    for(account acc : acclist)
    {
    decimal amountx =0.0;
    decimal amounty =0.0;
    decimal totalsum ;
    for(contact con:acc.contacts)
    {
    if(con.Type__c == 'positive')
    {
    amountx += con.Amount_X__c;
    acc.Rollup_Amount_X__c = amountx;
    }
    else{
    amounty += con.Amount_Y__c;
    acc.Rollup_Amount_Y__c = amounty;
    }
    }
    totalsum = amountx + amounty ;
    acc.Rollup_Amount__c = totalsum ;
    updateacc.add(acc);
    }
    update updateacc;
    }

    Hope this help for others.

    Thanks

    Ashi

Popular Salesforce Blogs

Popular Salesforce Videos