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
bryansosa01bryansosa01 

Summarize form CustomObject1 and save results into CustomObject2

I want to make a trigger that summarizes field_A from CustomObject1 and save the result into Field_B from CustomObject2.

 

These objects are NOT related.

 

Can someone give me an example?

Dhaval PanchalDhaval Panchal
Can you please give some more details.

i.e. on which condition you want to summarize that field?
because both objects are not related then there should be some logic to get records of customobject2 based on some values of customobject1. Please give some more details.
bryansosa01bryansosa01

I have two custom Objects with the following fields:

CustomObject1 with the fields Amount and Date1

CustomObject2 with the fields TotalAmount and Date 2

 

 

What I want to make is a trigger that summarizes CustomObject1.Amount  and save the result into CustomObject2.TotalAmount  where CustomObject1.Date1 = CustomObject2.Date2

 

 CustomObject1 and CustomObject2 are NOT related.

 

I would really appreciate your help. Thanks.

Dhaval PanchalDhaval Panchal

Hi,

 

Try below Trigger.

 

trigger trgUpdateTotalAmount on CustomObject1__c (after insert, after update, after delete) {
    Set<Date> setDate = new Set<Date>();
    Set<ID> setObj1Id = new Set<ID>();
    Map<Date, List<CustomObject1__c>> mapTotal = new Map<Date, List<CustomObject1__c>>();
    if(Trigger.isInsert || Trigger.isUpdate){
        for(CustomObject1__c obj1:Trigger.New){
            setDate.add(obj1.Date1__c);
            setObj1Id.add(obj1.Id);
            if(mapTotal.size()>0 && mapTotal.containsKey(obj1.Date1__c)){
                mapTotal.get(obj1.Date1__c).add(obj1);
            }
            else{
                List<CustomObject1__c> lstTemp = new List<CustomObject1__c>();
                lstTemp.add(obj1);
                mapTotal.put(obj1.Date1__c, lstTemp);
            }
        }
    }
    if(setDate.size()>0){
        List<CustomObject1__c> lstObj1 = [Select Id, Date1__c, Amount__c from CustomObject1__c where Date1__c in:setDate and Id not in:setObj1Id];
        if(lstObj1.size()>0){
            for(CustomObject1__c obj1:lstObj1){
                if(mapTotal.size()>0 && mapTotal.containsKey(obj1.Date1__c)){
                    mapTotal.get(obj1.Date1__c).add(obj1);
                }
                else{
                    List<CustomObject1__c> lstTemp = new List<CustomObject1__c>();
                    lstTemp.add(obj1);
                    mapTotal.put(obj1.Date1__c, lstTemp);
                }
            }
        }
        List<CustomObject2__c> lstObj2 = [Select Id, Date2__c, TotalAmount__c from CustomObject2__c where Date2__c in:setDate];
        if(lstObj2.size()>0){
            for(CustomObject2__c obj2:lstObj2){
                if(mapTotal.size()>0 && mapTotal.containsKey(obj2.Date2__c)){
                    obj2.TotalAmount__c = 0;
                    for(CustomObject1__c obj1:mapTotal.get(obj2.Date2__c)){
                        if(obj1.Amount__c <> null){
                            obj2.TotalAmount__c += obj1.Amount__c;
                        }
                    }
                }
            }
            Update lstObj2;
        }
    }
}

 

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Try the following trigger,

 

trigger CreateObj2 on customobject1__c (after insert) {
    for(Customobject1__c c1 : Trigger.new){
        Customobject2__c c2 = new Customobject2__c(TotalAmount__c = c1.Amount__c, Date2__c = c1.Date1__c);
        insert c2;
    }
}

 

this will create a new record in customobject2 if a customobject1 record is created.


Hope this will help you...!