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
anu_karthianu_karthi 

how to find sum of different input fields from different objects

Hi every one,

 

I want to find sum of different input fields from different objects and assign total sumto one field in main object using salesforce.All objects have one common field like SFN Number.In main object this SFN Number is input field.Remaing all other objects have SFN Number field look over to main object SFN Number.Main object have Total amount input field.Remaining all objects have amount inputfield.i want to find the sumof all amounts which have same SFN Numbers and assign it to the total amount field which is in main object corresponding SFN Number record.

 

Plz give me some guidance how to achieve this functionality.

 

Thanks in advance

 

 

Venkat PolisettVenkat Polisett

anu_karthi wrote:

Hi every one,

 

I want to find sum of different input fields from different objects and assign total sumto one field in main object using salesforce.All objects have one common field like SFN Number.In main object this SFN Number is input field.Remaing all other objects have SFN Number field look over to main object SFN Number.Main object have Total amount input field.Remaining all objects have amount inputfield.i want to find the sumof all amounts which have same SFN Numbers and assign it to the total amount field which is in main object corresponding SFN Number record.

 

Plz give me some guidance how to achieve this functionality.

 

Thanks in advance

 

 


I saw a previous post from you in which you gave the below details and I belive you are talking about these custom objects. You were trying to write a trigger to get these summary values to the top level parent object.

 

Obj A-SFN Number(Input field(text),Total Amount(Number))

Obj B-SFN Number(lookup(ObjA),Amount(Number))

Obj C-SFN Number(lookup(ObjA),Amount(Number))

 

My recommendation to you would be to use Master details relationships and Roll-up summary fields instead of triggers. You do note need to write any Apex code for this.

 

- Change the relationship between your main Obj A and child object Obj B to master detail relationship (currently you have lookup relationship).

- Do the same between Obj A and Obj C

- Create a roll-up summary field on Obj A for summing up amounts from Obj B

- Do the same on Obj A for summing up amounts from Obj C

- Create a formula field on Obj A that adds the two rollup summary fields that you have created above to give you a combined total from ObjA and ObjB

 

 

 

Hope this helps.

 

Message Edited by Venkat Polisett on 02-08-2010 12:36 PM
anu_karthianu_karthi

Thanks fo ur reply,

 

In my project no.of objects having this requirement.in this project there is no master-detail relation ship.Only lookup relationship  is there from one object to another object related to SFN Number.I need to write either trigger or workflow.But i am new to these concepts.my requirement is:

ObjA is having total amount field.ObjB is having amount filed.suppose objectB is having 4 records of same sfn number i need add all 4 records amount and assign that value to total amount of corresponding sfn number in objA.Here i need to accumulate the amount every time and assign to total amount.

How to do this functionality using triggers.

Plz help me.

 

Thanks & Regards,

Anu..

 

Rajesh ShahRajesh Shah

Write a trigger on Obj B

For Insert - Find the corresponding parent which has the same sfn and add to the amount.

 

For Update - Find the corresponding parent which has the same sfn and update the amount using the previous and new value in the trigger.

 

For Delete - Find the corresponding parent which has the same sfn and delete the amount.

 

The above logic can get complex and you will have to be careful if you are doing for bulk records. You cannot use workflow since the relationship is not master detail.

 

anu_karthianu_karthi

Thanks for ur reply,

 

Can u plz give me the example code.

 

Thanks in advance,

Anu..

Venkat PolisettVenkat Polisett

In the given below code:

 Test__c is parent

 ObjA__c is child

 

Below code does not implement delete and undeletes. It's for your exercise.

 

trigger updateTestAmountFromObjA on ObjA__c (after insert, before update)
{
    Set<Id> parentIds = new Set<Id>();
    
    for (ObjA__c p : Trigger.New)
        parentIds.add(p.Test__c);

    Map<Id, Test__c> parents = new Map<Id, Test__c>(

[select id, ObjA_Total_Amount__c from Test__c where id in :parentIds]);

Map<Id, Test__c> updatables = new Map<Id, Test__c>();  

for (ObjA__c n : Trigger.New) { Test__c t = parents.get(n.Test__c); if (Trigger.isInsert) {             Double AllTotal = (t.ObjA_Total_Amount__c == null ? 0 : t.ObjA_Total_Amount__c);             Double current = (n.Amount__c == null ? 0 : n.Amount__c);
            t.ObjA_Total_Amount__c = AllTotal + current;

            updatables.put(t.id, t); } else { // update

            Double AllTotal = (t.ObjA_Total_Amount__c == null ? 0 : t.ObjA_Total_Amount__c);             Double previous = (Trigger.oldMap.get(n.Id).Amount__c == null ? 0 : 

Trigger.oldMap.get(n.Id).Amount__c);             Double current = (n.Amount__c == null ? 0 : n.Amount__c);
            // update only if amount changes             if (previous != current)             {                 t.ObjA_Total_Amount__c = AllTotal + (-1 * previous) + current;                 updatables.put(t.id, t);             }

 

} }

 

 

    if (updatables.size() > 0)

update updatables.values(); }

 

Venkat Polisetti