0

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;
}

List of error

1

1 Answer 1

0

The errors give you a good hint at what's wrong here.

Let's look at line 6, with some line breaks to make things easy to see

Map<ID, contract> cont = new Map<ID, contract>(
    [select id, Tot_Instalment__c 
    from Contract 
    where id := ids]
);

We have 2 equals signs here.

cont = new map...
where id := ids

The first one is fine, and the second one is where your error lies.

Binding apex variables in a SOQL query uses : <variable> syntax.

You need to change where id := ids to where id = :ids or where id IN :ids
Notice the order of the colon and equals sign.

5
  • Thanks!!! Now, i have only on problem in the line 10. The error message is : Invalid Caracter in identifier "ContractNumber__c" Oct 29, 2018 at 1:31
  • @GirbsonBIJOU Try to figure this one out on your own. It's a straightforward error and should be easy to resolve with a little bit of effort.
    – Derek F
    Oct 29, 2018 at 1:34
  • Thanks, the problem is i do not have strong code skills. But i will try. Thank you Oct 29, 2018 at 1:36
  • @GirbsonBIJOU All the more reason to use the DLRS tool then. Alternatively, struggling with and overcoming errors is a way to become a better programmer. When you run into an error (or a bunch of errors), start looking at the lowest line number (one syntax error can cascade into more errors). Take the time to read and digest the error message(s). Try to work out what might be a problem and, just as importantly, what can't be a problem. This is the process I used to arrive at my answer.
    – Derek F
    Oct 29, 2018 at 1:47
  • Problem solved: I used the name of the field ContractNumber__c instead of any variable name. Oct 29, 2018 at 1:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .