+ Start a Discussion
PetyaPetya 

Trigger question

Hello 

 

I created trigger to accumulate value from custom object into his master object. I have a lookup relationship, cannot use master detail and rollup fields, so the trigger is one possibility. When I change in the custom object the value, which I want to assume on the master object, the appropriate field on the master object is not updated  immediately from my trigger.

When I make small changes in the master object and save the record, the trigger works and the value is updated also on the master record.

I start with trigger CalculateTotalMargin on Account (before update)

 

How should I define my trigger, to have immediately update on the master record, when the detail record is changed?

 

Any Ideas

 

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

Try this

 

rigger CalculateTotalMargin on Project__c(after update) {

    Integer i = 0;

    Map < Id, Account > accMap = new Map < Id, Account > (); //<--- Changed

    // Current Account ID <--- Changed
    String parentid = Trigger.new[i].Account__c;

    //List all children projects from the record above. 
    List < Project__c > pr = [Select Project__c.Id, Project__c.Sum_Margin__c From Project__c where Project__c.Account__c = : parentid];

    Double j = 0;

    // Loop through the filtered projects and sum their margin.
    for (Project__c pro: pr) {
        if (accMap.get(pro.Account__c) == NULL) {
            accMap.put(pro.Account__c, new Account(Id = pro.Account__c,Total_Project_Margin__c = 0));
        }
        Account parObject = accMap.get(pro.Account__c);
        If(pro.Sum_Margin__c != Null) {
             parObject.Total_Project_Margin__c += pro.Sum_Margin__c;
        }
    }
    update accMap.values();

}

 I am not sure whether this will work and can have syntax errors. I am leaving those for you . :P

 

 

 

There are some obvious modification required like bulkyfing the code.

All Answers

Avidev9Avidev9

I guess the trigger should be in the Child object. Is it something that you are doing ?

PetyaPetya
hm the trigger is now on the parent object.
Avidev9Avidev9
To make the update instantaneously the trigger should be in the Child Object. In this way trigger will always fire when you make changes to the child records.
PetyaPetya

But I need to assume the children records (Project) on the parent record (Account). Here the trigger

 

trigger CalculateTotalMargin on Account (before update)
 {
 
 Integer i = 0;
 
 Account parObject = Trigger.new[i];

 // Current Account ID
 String parentid = Trigger.new[i].Id;
 
 //List all children projects from the record above. 
 List<Project__c> pr = [Select  Project__c.Id,  Project__c.Sum_Margin__c From  Project__c where  Project__c.Account__c =: parentid];
 
 Double j = 0;
 
 // Loop through the filtered projects and sum their margin.
  for(Project__c pro : pr)
 {
 If (pro.Sum_Margin__c != Null)
 {
 j += pro.Sum_Margin__c;
 }
 }
  parObject.Total_Project_Margin__c  = j;
 
 }

Avidev9Avidev9

Try this

 

rigger CalculateTotalMargin on Project__c(after update) {

    Integer i = 0;

    Map < Id, Account > accMap = new Map < Id, Account > (); //<--- Changed

    // Current Account ID <--- Changed
    String parentid = Trigger.new[i].Account__c;

    //List all children projects from the record above. 
    List < Project__c > pr = [Select Project__c.Id, Project__c.Sum_Margin__c From Project__c where Project__c.Account__c = : parentid];

    Double j = 0;

    // Loop through the filtered projects and sum their margin.
    for (Project__c pro: pr) {
        if (accMap.get(pro.Account__c) == NULL) {
            accMap.put(pro.Account__c, new Account(Id = pro.Account__c,Total_Project_Margin__c = 0));
        }
        Account parObject = accMap.get(pro.Account__c);
        If(pro.Sum_Margin__c != Null) {
             parObject.Total_Project_Margin__c += pro.Sum_Margin__c;
        }
    }
    update accMap.values();

}

 I am not sure whether this will work and can have syntax errors. I am leaving those for you . :P

 

 

 

There are some obvious modification required like bulkyfing the code.

This was selected as the best answer
PetyaPetya

thank you very much, yes I receive syntax error.

PetyaPetya

it works now, thank you

Only the Project_c in the select section is not needed, because the trigger is working on the project object.