+ Start a Discussion
Siddharth LakhotiaSiddharth Lakhotia 

Trigger to calculate the sum of values from parent to child object field

Hi,

i need help in developing a trigger that calculates values from Parent Object Inquiry to child object Inquiry Details.

So every Inquiry record will have a field call  Cushion Number( this acts as a code) and Act Sales.

I need to capture information of sum of ActSales against a cushion number and store it in a child object field Actual Sales..

how can I achieve this ?

 
Sujeet PatelSujeet Patel
Hii Siddharth 
You wann say that copy the value of parent object to child  object.
If I am right then you can do this thing by using formula field as well as trigger.
You need to create formula field on child object.
and second method is trigger.

I hope my this trigger will help full for you
trigger Bi_ActSales on Inquiry_Detail__c (before insert) 
{
    List<Id> ls=new List<Id>();
    
    for(Inquiry_Detail__c inquiry_detail:trigger.new)
    {
        ls.add(inquiry_detail.Inquiry__c);
    }
    
    Map<Id,Decimal> data=new Map<Id,Decimal>();
    
    List<Inquiry__c> inq=[select id,Act_Sales__c from Inquiry__c where id in: ls];
    
    for(Inquiry__c i:inq)
    {
        System.debug('*****************id'+i.id);
        data.put(i.Id,i.Act_Sales__c);
    } 
    
    for(Inquiry__c inq_data:inq)
    {
        for(Inquiry_Detail__c inquiry_obj:trigger.new)
        {
            
            System.debug('+++++++++++++++++++++++'+inq_data.id);
            inquiry_obj.Act_Sales1__c=data.get(inq_data.id);
            
        }
    }
}

 
Siddharth LakhotiaSiddharth Lakhotia
I need to calculate the sum of ActSales against a blanket number...

So lets say, 

For Inquiry 1 , Cushion no : 123  , ActSales : 100

For Inquiry 2 , Cushion no : 123  , ActSales : 200

For Inquiry 3 , Cushion no : 123  , ActSales : 300

For Inquiry 4 , Cushion no : 456 , ActSales :  350

For Inquiry 5 , Cushion no : 456  , ActSales : 450

I have a lookup at inquiry details that looks up to the Inquiry object,

I need to pull in the blanket no. against the inquiry and sum of Act Sales against cushion number(Cushion No ; 123 Actual Sales :600, Cushion Number : 456 , Actual Sales : 800).
Sujeet PatelSujeet Patel
Siddharth if you change your lookup direction then your work could be easy.
if You make inquiry detail parent and inquiry child then you can total and save to the parent field.
But i am trying to slove also on your way.
 
Siddharth LakhotiaSiddharth Lakhotia
Hi Puneet, I will be getting invoice from erp system. Also inquiry detail now against inquiry.... So difficult to have it that way
Sujeet PatelSujeet Patel
This is My New Solution.You May try this one.If this will not full fill your requientment then inform me
User-added image
trigger AI_Inquiry_Detail on Inquiry_Detail__c  (after insert,before update,after delete)
{

    if(trigger.isAfter)
    {
        if(trigger.isInsert)
        {
    
            Map<id,Decimal> a_id=new Map<id,Decimal>();
            Decimal total=0;
            
            for(Inquiry_Detail__c b_obj:trigger.new)
            {    
                total=b_obj.Act_Sales1__c;
                a_id.put(b_obj.Inquiry__c,b_obj.Act_Sales1__c);
            }
            System.debug(total);
        /*
            List<B__c> list_of_b=[select A__c,total__c from B__c where A__c in: a_id.keySet()];
            Map<String,Decimal> a_obj=new Map<String,Decimal>();
        
            for(B__c total_data:list_of_b)
            { 
              total+=total_data.total__c;
              a_obj.put(total_data.A__c,total);
          
            }
        
         */   
                List<Inquiry__c> data_a=[select Actual_Sales__c from Inquiry__c where id in: a_id.keySet()];
                //List<A__c> new_update=new List<A__c>();
                
                System.debug('-----------------------------'+data_a);
                for(Inquiry__c a_ob:data_a)
                {
                    a_ob.Actual_Sales__c=a_ob.Actual_Sales__c+total;
                    //new_update.add(a_ob);
                }
                
                update data_a;
         }
         if(trigger.isDelete)
         {
            Map<id,Decimal> a_id_delete=new Map<id,Decimal>();
            for(Inquiry_Detail__c b_obj:trigger.old)
            {
                a_id_delete.put(b_obj.Inquiry__c,b_obj.Act_Sales1__c);
            }
            
            List<Inquiry__c> a_data=[select id,Actual_Sales__c from Inquiry__c where id in: a_id_delete.keySet()];
            
            List<Inquiry__c> d=new List<Inquiry__c>();
            for(Inquiry__c a_obj:a_data)
            {
                a_obj.Actual_Sales__c =a_obj.Actual_Sales__c -a_id_delete.get(a_obj.id);
                d.add(a_obj);
            }
            
            update d;
         }   
    }
    
}

 
Siddharth LakhotiaSiddharth Lakhotia
HI Puneet,

Where are we taking account of cushion number here , I require summation of ActSales against cushion no.

As shown in example

For Inquiry 1 , Cushion no : 123  , ActSales : 100

For Inquiry 2 , Cushion no : 123  , ActSales : 200

For Inquiry 3 , Cushion no : 123  , ActSales : 300

For Inquiry 4 , Cushion no : 456 , ActSales :  350

For Inquiry 5 , Cushion no : 456  , ActSales : 450

I need to pull in the sum of Act Sales against cushion number(Cushion No ; 123 Actual Sales :600, Cushion Number : 456 , Actual Sales : 800).
Siddharth LakhotiaSiddharth Lakhotia
Hi Sujeet,

Can you please help me with the solution based on the example given
Sujeet PatelSujeet Patel
Hii Siddharth 
I solve your problem.i hope this will help full for you.
i change schema of Object Relations.If You will do in this manner then your trigger would be easy so ..
below is given schema picture and triggers

User-added image
 
trigger AI_ActSales on Act_Sales__c (after insert)
{
    List<ID> cusion=new List<ID>();
     for(Act_Sales__c act:trigger.new)
     {
         cusion.add(act.Cushion__c);
         
     }   
     
     AggregateResult []sumOfAct=[select sum(Act_Sales__c)add from Act_Sales__c where Cushion__c in: cusion];
     Decimal sumAct=(Decimal)sumOfAct[0].get('add');
     System.debug('++++++++sum of Act Sales'+sumOfAct[0].get('add')) ;
     
     List<Inquiry_Detail__c> inqUpdate=new List<Inquiry_Detail__c>();
     List<Inquiry_Detail__c> inq=[Select Id,Act_Sales1__c from Inquiry_Detail__c where Cushion__c in: cusion];
     
     for(Inquiry_Detail__c inq_obj:inq)
     {
         inq_obj.Act_Sales1__c=sumAct;
         inqUpdate.add(inq_obj);
     }
     
     update inqUpdate;
     

}

You have to create one another trigger on Inquiry Detail in which just copy the value of act sales to there field after insert.
 
Siddharth LakhotiaSiddharth Lakhotia
Why a try on act_Sales.. it isn't an object even
Sujeet PatelSujeet Patel
Ya I know i told you if you will do in this way then you can get record as you want.I sent you schema diagram.Wait i send You Record Page then you can understand.
IF you wants to do this problem only using this two object i will try to resolve it.
User-added image