You need to sign in to do that
Don't have an account?
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
Try this
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
I guess the trigger should be in the Child object. Is it something that you are doing ?
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;
}
Try this
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.
thank you very much, yes I receive syntax error.
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.