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
SFDC_2706SFDC_2706 

help on custom objects using trigger....

this is my query...plzz help me out...
 
¢Create a custom object “Revenue”. Create a field named “Total Revenue”.
¢Create another custom object Revenue Line Items. Create field named “Add Revenue” and lookup to Revenue.
¢Create a trigger on Revenue Line Items to update “Total Revenue” field on Revenue with the total(sum) of “Add Revenue” on all child records to the parent revenue record.
¢Handle insert, update and delete. Also handle change of parent revenue on child record.
 
BRIEF SUMMARY :
suppose “Total Revenue” = 30 and “Add Revenue” = 20...if i add 10 in “Add Revenue” than 
  “Total Revenue” should become 40 automatically and if i subtract 10 from “Add Revenue” than
  “Total Revenue” should become 20....
 
Any help would be appreciated....!!!
Best Answer chosen by Admin (Salesforce Developers) 
PremanathPremanath

Hi you can do it By Roll-up summary also

But if you need to do by using trigger.

Write a trigger on child (Revenue Line items) object

 

Here i am posting one trigger just replace fields and objects

Revenue  -- Example2__c

Total Revenue -- Total_Amount__c 

Revenue Line Items -- ChildExample2__c 

Add Revenue -- Amount__c

 

 

trigger Testduplicate7 on ChildExample2__c (after delete, after insert, after update) {
    set<Id> Ids = new set<Id>();
    if(trigger.isInsert || trigger.isUpdate){
        for(ChildExample2__c p : trigger.new){
            Ids.add(p.Example2__c);
        }
    }
    if(trigger.isDelete){
        for(ChildExample2__c p : trigger.old){
            Ids.add(p.Example2__c);
        }
    }
    map<Id,Double> smap = new map <Id,Double>();
    for(AggregateResult q : [select Example2__c,sum(Amount__c)from ChildExample2__c where Example2__c IN :Ids group by Example2__c]){
        smap.put((Id)q.get('Example2__c'),(Double)q.get('expr0'));
    }
    List<Example2__c> ls = new List<Example2__c>();
    for(Example2__c a : [Select Id, Total_Amount__c from Example2__c where Id IN :Ids]){
        Double PipelineSum = smap.get(a.Id);
        a.Total_Amount__c = PipelineSum;
        ls.add(a);
    }
    update ls;
}

 

 

 

prem

All Answers

EnthEnth
Err, why not just make it a Master-Detail relationship and use a Roll Up Summary field to maintain Total Revenue ?
Sunny670Sunny670
I agree with Enth.
SFDC_2706SFDC_2706

Hi enth,

  i know that...but i want to use trigger in this...thats my actual task...

help me if u can...

thanks...

Sunny670Sunny670

trigger Revenue Line Items on Revenue_Line_Items__c (after insert,after update)
{
Revenue__c rev=Trigger.New[0];
Revenue_Line_Items__c rli=new Revenue_Line_Items__c ();
rev.Total Revenue__c=rev.Total Revenue__c+rli.addrevenue
upsert rev;
}

PremanathPremanath

Hi you can do it By Roll-up summary also

But if you need to do by using trigger.

Write a trigger on child (Revenue Line items) object

 

Here i am posting one trigger just replace fields and objects

Revenue  -- Example2__c

Total Revenue -- Total_Amount__c 

Revenue Line Items -- ChildExample2__c 

Add Revenue -- Amount__c

 

 

trigger Testduplicate7 on ChildExample2__c (after delete, after insert, after update) {
    set<Id> Ids = new set<Id>();
    if(trigger.isInsert || trigger.isUpdate){
        for(ChildExample2__c p : trigger.new){
            Ids.add(p.Example2__c);
        }
    }
    if(trigger.isDelete){
        for(ChildExample2__c p : trigger.old){
            Ids.add(p.Example2__c);
        }
    }
    map<Id,Double> smap = new map <Id,Double>();
    for(AggregateResult q : [select Example2__c,sum(Amount__c)from ChildExample2__c where Example2__c IN :Ids group by Example2__c]){
        smap.put((Id)q.get('Example2__c'),(Double)q.get('expr0'));
    }
    List<Example2__c> ls = new List<Example2__c>();
    for(Example2__c a : [Select Id, Total_Amount__c from Example2__c where Id IN :Ids]){
        Double PipelineSum = smap.get(a.Id);
        a.Total_Amount__c = PipelineSum;
        ls.add(a);
    }
    update ls;
}

 

 

 

prem

This was selected as the best answer