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
Girbson BijouGirbson Bijou 

Trigger to Roll-up on lookup relationship

I have a lookup relationship between Contract(Parent) and Expenses__c
want to summarize the value of the field called Amout in Expenses__c to a custom field in Contract called Tot_instalment__c.  Please help me to find where the errors are in the code. Attached is the list of the problem.
trigger UpdateTotInstalment on Expenses__c (after insert, after delete, after undelete, after update) {
    set<id> ids=new set<id>();
    for(Expenses__c exp: trigger.new)
        ids.add(exp.ContractNumber__c);
    List<contract> contractToUpdate=new List<Contract>();
    Map<ID, contract> cont = new Map<ID, contract>([select id ,Tot_Instalment__c from Contract where id:=ids]);
    AggregateResult[] groupedResults = [SELECT ContractNumber__c,SUM(Amount__c)amt FROM Expenses__c where ContractNumber__c in:ids group by ContractNumber__c];

    for(AggregateResult Results: groupedResults){
         Id ContractNumber__c =(id) Results.get('ContractNumber__c');         
         Contract c = cont.get(ContractNumber__c);
         c.Tot_Instalment__c=(integer)Results.get('amt');
         contractToUpdate.add(c);
    }    
  update contractToUpdate;
}

User-added image
Best Answer chosen by Girbson Bijou
Raj VakatiRaj Vakati
Try this code
 
trigger UpdateTotInstalment on Expenses__c (after insert, after delete, after undelete, after update) {
    set<id> ids=new set<id>();
    for(Expenses__c exp: trigger.new)
        ids.add(exp.ContractNumber__c);
	List<contract> contractToUpdate=new List<Contract>();
    Map<ID, contract> cont = new Map<ID, contract>([select id ,Tot_Instalment__c from Contract where id IN :ids]);
    AggregateResult[] groupedResults = [SELECT ContractNumber__c cnt,SUM(Amount__c)amt FROM Expenses__c where ContractNumber__c in IN :ids group by ContractNumber__c];

    for(AggregateResult Results: groupedResults){
         Id recId =(Id) Results.get('cnt');         
         Contract c = cont.get(recId);
         c.Tot_Instalment__c=(integer)Results.get('amt');
         contractToUpdate.add(c);
    }    
  update contractToUpdate;
}

 

All Answers

Girbson BijouGirbson Bijou
I solve a part of the problem  by  changing " where id := ids to  where id IN :ids " In the line  6. Please help me with the error in the line 10
Raj VakatiRaj Vakati
Try this code
 
trigger UpdateTotInstalment on Expenses__c (after insert, after delete, after undelete, after update) {
    set<id> ids=new set<id>();
    for(Expenses__c exp: trigger.new)
        ids.add(exp.ContractNumber__c);
	List<contract> contractToUpdate=new List<Contract>();
    Map<ID, contract> cont = new Map<ID, contract>([select id ,Tot_Instalment__c from Contract where id IN :ids]);
    AggregateResult[] groupedResults = [SELECT ContractNumber__c cnt,SUM(Amount__c)amt FROM Expenses__c where ContractNumber__c in IN :ids group by ContractNumber__c];

    for(AggregateResult Results: groupedResults){
         Id recId =(Id) Results.get('cnt');         
         Contract c = cont.get(recId);
         c.Tot_Instalment__c=(integer)Results.get('amt');
         contractToUpdate.add(c);
    }    
  update contractToUpdate;
}

 
This was selected as the best answer
Andrew GAndrew G
Yep @ Raj

Try with the alias set for the field you are querying.
AggregateResult[] groupedResults = [SELECT ContractNumber__c contractID, SUM(Amount__c) amt FROM Expenses__c where ContractNumber__c in:ids group by ContractNumber__c];

    for(AggregateResult Results: groupedResults){
        System.debug('#### contract ID ' + string.valueOf(Results.get('contractID')));
        System.debug('#### amt number' + string.valueOf(Results.get('amt')));
    
        Contract c = new Contract( ID=string.valueof(Results.get('contractID')), Tot_Instalment__c= integer.valueof(Results.get('amt')));
        contractToUpdate.add(c);
    }

  update contractToUpdate;

 
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi 

Try the below code:
 
trigger UpdateTotInstalment on Expenses__c (after insert, after delete, after undelete, after update) {
    set<id> ids=new set<id>();
    for(Expenses__c exp: trigger.new)
        ids.add(exp.ContractNumber__c);
    List<contract> contractToUpdate=new List<Contract>();
    Map<ID, contract> cont = new Map<ID, contract>([select id ,Tot_Instalment__c from Contract where id in: ids]);
    AggregateResult[] groupedResults = [SELECT ContractNumber__c,SUM(Amount__c) amt FROM Expenses__c where ContractNumber__c in:ids group by ContractNumber__c];

    for(AggregateResult Results: groupedResults){
         Id ContractNumber =(id) Results.get('ContractNumber__c');         
         Contract c = cont.get(ContractNumber);
         c.Tot_Instalment__c=(integer)Results.get('amt');
         contractToUpdate.add(c);
    }    
  update contractToUpdate;
}

Hope this will helpful.
Thanks.