+ Start a Discussion
bhaskar reddy kbhaskar reddy k 

regarding trigger

Hi Board,
              This is vijay,please help me on this .I have Two objects,Obj1 and Obj2 and look up is exists between them.in object there is a field called total_Amount__c in obj1 and amount__c in obj2.i want calculate total amount in obj2 and update that to Obj1 in Toatal_amount__c field.

Thanks in advance

Regards
vijay
Onesh ReddyOnesh Reddy
Hi Vijay,
Can u specify lookup is from which obj to other obj.
As there is a Lookup relation betwwen objects you can make the field of obj1 as formula field (total_Amount__c) and use the field amount__c to calculate total sum.
Please let me know if it helps you.

Regards,
Onesh.K
VineetKumarVineetKumar
Dayakar.DDayakar.D
Hi Vijay,

Here I am taking two custom Objects SetUp lookup relation with site. 
Below is the trigger to update total amount on site.
trigger TriggerOnsetup on SetUp__c (after insert,after update) {
    list<string> siteIds=new list<string>();
    for(setUp__c setUpVar:trigger.new)
    {
        siteIds.add(setUpVar.site__c);
    }
	list<site__c> listOfSites=[select id,name,Total_amount__c,(select id,Amount__c from setup__r) from site__c where id in :siteIds];
    Map<site__c,list<setUp__c>> siteMap=new Map<site__c,list<setUp__c>>();
    for(Site__c siteVar:listOfSites)
    {
        siteMap.put(siteVar, siteVar.setup__r);
    }
    list<Decimal> amounts=new list<Decimal>();
    Decimal total=0;
    list<Site__c> siteListToUpdate=new list<Site__c>();
    for(Site__c siteVar:siteMap.keySet())
    {
        for(SetUp__c setUpVar:siteMap.get(siteVar))
        {
            amounts.add(setUpVar.Amount__c);   
        }
        for(Decimal d:amounts)
        {
            total=total+d;
        }
        siteVar.Total_amount__c=total;
        siteListToUpdate.add(siteVar);
    }
   update siteListToUpdate;
}

Please let me know if it helps you.

Regards,
Dayakar.D