You need to sign in to do that
Don't have an account?
Why an after update trigger is fired after an after insert trigger completes execution without updating a record?
I have this code below - After inserting a new record, the values are getting overridden with another value as an after update is fired as soon as an after insert operation is complete.
trigger TPI_Update_MCCP_Prod_GoalEdit_on_MCCP on MC_Cycle_Plan_Product_vod__c (after update, after insert) {
set<Id> prod_id=new set<Id>();
set<Id> channel_id=new set<Id>();
List<Id> target_id=new List<Id>();
set<Id> target_id2=new set<Id>();
set<Id> plan_id=new set<Id>();
set<Id> channel_id1=new set<Id>();
set<Id> prod_id1=new set<Id>();
decimal before_val;
decimal after_val;
string prod_name;
AggregateResult[] groupedResults;
integer aggregated_goal_val;
decimal old_goal_val;
decimal new_goal_val;
set<Id> sum = new Set<Id>();
boolean flag;
List<MC_Cycle_Plan_vod__c> mcptoUpdate = New List<MC_Cycle_Plan_vod__c>();
//
//hold value of products edited in set 'prod_id'
//
for (MC_Cycle_Plan_Product_vod__c p: trigger.new)
{
if(trigger.isInsert)
{
prod_name = Trigger.newMap.get(p.id).Product_Name_vod__c;
new_goal_val = Trigger.newMap.get(p.Id).Product_Activity_Goal_vod__c;
flag = true;
}
else if(trigger.isUpdate){
old_goal_val = Trigger.oldMap.get(p.Id).Product_Activity_Goal_vod__c;
prod_name = Trigger.oldMap.get(p.id).Product_Name_vod__c;
new_goal_val = Trigger.newMap.get(p.Id).Product_Activity_Goal_vod__c;
flag=false;
}
//
prod_id.add(p.id);
//
system.debug('MCCP product - ' + prod_id);
System.debug('Old value of MCCP product - '+old_goal_val);
//
//hold values of channels related to products in set 'channel_id' where channel is 'Interaction'
//
if(p.Channel_vod__c=='Interaction')
{
channel_id.add(p.Cycle_Plan_Channel_vod__c);
system.debug('MCCP channel - ' + channel_id);
}
//
//hold values of target in 'target_id'
//
List<MC_Cycle_Plan_Channel_vod__c> t= [select Cycle_Plan_Target_vod__c from MC_Cycle_Plan_Channel_vod__c where Id IN: channel_id];
for (MC_Cycle_Plan_Channel_vod__c mc : t)
{
target_id.add(mc.Cycle_Plan_Target_vod__c);
}
system.debug('MCCP Target - ' + target_id);
//
//hold values of plan in 'plan_id'
//
List<MC_Cycle_Plan_Target_vod__c> t2 = [select id,Cycle_Plan_vod__r.id from MC_Cycle_Plan_Target_vod__c where Id IN : target_id];
for (MC_Cycle_Plan_Target_vod__c mc2 : t2)
{
plan_id.add(mc2.Cycle_Plan_vod__r.id);
}
system.debug('MC Cycle Plan - ' + plan_id);
//
//hold values of all target in 'target_id2'
//
List<MC_Cycle_Plan_Target_vod__c> plan_1 = [select id from MC_Cycle_Plan_Target_vod__c where Cycle_Plan_vod__c in : plan_id];
for (MC_Cycle_Plan_Target_vod__c mc3 : plan_1)
{
target_id2.add(mc3.id);
}
//
//hold values of all channels of above targets where channel is 'Interaction' in 'channel_id1'
//
system.debug('All MCCP Targets of above MC Cycle Plan - ' + target_id2);
List<MC_Cycle_Plan_Channel_vod__c> t3 = [select id,Channel_vod__c from MC_Cycle_Plan_Channel_vod__c where Cycle_Plan_Target_vod__c IN : target_id2];
for (MC_Cycle_Plan_Channel_vod__c mc4 : t3)
{
if(mc4.Channel_vod__c=='Interaction'){
channel_id1.add(mc4.id);
}
}
system.debug('All MCCP Channels of above MCCP Target - ' + channel_id1);
//
//hold value of products edited in set 'prod_id'
//
List<MC_Cycle_Plan_Product_vod__c> p1 = [select id,Product_Activity_Goal_vod__c from MC_Cycle_Plan_Product_vod__c where Cycle_Plan_Channel_vod__c in : channel_id1];
for (MC_Cycle_Plan_Product_vod__c mc5 : p1)
{
prod_id1.add(mc5.id);
}
system.debug('All MCCP Products of above MCCP Channels - '+ prod_id1);
Set<id> result = prod_id1.clone();
system.debug('Removing the product where the value of goal is changed - ' + result.removeall(prod_id));
system.debug('Resulting set of products where goal is not changed -'+result);
List<MC_Cycle_Plan_Product_vod__c> mcp1 =[select Product_Name_vod__c from MC_Cycle_Plan_Product_vod__c where Id in: result];
System.debug(mcp1.size());
//
// Aggregation of goals grouped by product with no change is stored in 'groupedResults'
//
for(MC_Cycle_Plan_Product_vod__c aq : mcp1)
{
if(aq.Product_Name_vod__c ==prod_name)
{
sum.add(aq.id);
}
}
System.debug('Sum'+sum);
groupedResults = [SELECT SUM(Product_Activity_Goal_vod__c)aver FROM MC_Cycle_Plan_Product_vod__c where Id in : sum];
system.debug('Sum of goals grouped by product with no change - ' + integer.valueOf(groupedResults[0].get('aver')) );
//
// typecasting the result into integer data type (aggregated_goal_val = total of all goals which are not edited)
//
aggregated_goal_val = integer.valueOf(groupedResults[0].get('aver'));
System.debug('Aggregated_goal_val in Insert - '+aggregated_goal_val);
//
//Old sum of goals (edited and non edited) grouped by product
//
system.debug('xxx'+trigger.isInsert);
system.debug('xxx'+trigger.isUpdate);
if(flag)
{
before_val = aggregated_goal_val;
System.debug('Before Value in Insert - '+before_val);
System.debug('Aggregated_goal_val in Insert - '+aggregated_goal_val);
}
else
{
before_val = aggregated_goal_val + old_goal_val;
system.debug('Old value of edit goals -'+before_val);
}
//
//New sum of goals (edited and non edited) grouped by product
//
after_val= aggregated_goal_val + new_goal_val;
system.debug('New value of edit goals -'+after_val);
}
List<MC_Cycle_Plan_vod__c> mcp =[select Product_1_TPI__c,Product_2_TPI__c,Product_3_TPI__c,Product_4_TPI__c,HQ_Target_1_TPI__c,HQ_Target_2_TPI__c,HQ_Target_3_TPI__c,HQ_Target_4_TPI__c,Proposed_Target_1_TPI__c,Proposed_Target_2_TPI__c,Proposed_Target_3_TPI__c,Proposed_Target_4_TPI__c from MC_Cycle_Plan_vod__c Where Id IN :plan_id];
for(MC_Cycle_Plan_vod__c mc:mcp){
for(MC_Cycle_Plan_Product_vod__c cyclprod : [Select Product_Name_vod__c from MC_Cycle_Plan_Product_vod__c WHERE ID =:prod_id]){
if (mc.Product_1_TPI__c == ''){
System.debug('Inserting in 1st prod');
mc.Product_1_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_1_TPI__c = before_val;
mc.Proposed_Target_1_TPI__c = after_val;
}
else if (mc.Product_1_TPI__c != '' && mc.Product_1_TPI__c == cyclprod.Product_Name_vod__c)
{
System.debug('Updating in 1st prod');
mc.Product_1_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_1_TPI__c = before_val;
mc.Proposed_Target_1_TPI__c = after_val;
system.debug('satisfy 1');
}
else if (mc.Product_2_TPI__c == '')
{
System.debug('Inserting in 2nd prod');
mc.Product_2_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_2_TPI__c = before_val;
mc.Proposed_Target_2_TPI__c = after_val;
system.debug('satisfy 2');
}
else if (mc.Product_2_TPI__c != '' && mc.Product_2_TPI__c == cyclprod.Product_Name_vod__c)
{
System.debug('Updating in 2nd prod');
mc.Product_2_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_2_TPI__c = before_val;
mc.Proposed_Target_2_TPI__c = after_val;
}
else if (mc.Product_3_TPI__c == '')
{
System.debug('Inserting in 3rd prod');
mc.Product_3_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_3_TPI__c = before_val;
mc.Proposed_Target_3_TPI__c = after_val;
}
else if (mc.Product_3_TPI__c != '' && mc.Product_3_TPI__c == cyclprod.Product_Name_vod__c)
{
System.debug('Updating in 3rd prod');
mc.Product_3_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_3_TPI__c = before_val;
mc.Proposed_Target_3_TPI__c = after_val;
}
else if (mc.Product_4_TPI__c == '')
{
System.debug('Inserting in 4th prod');
mc.Product_4_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_4_TPI__c = before_val;
mc.Proposed_Target_4_TPI__c = after_val;
}
else if (mc.Product_4_TPI__c != '' && mc.Product_4_TPI__c == cyclprod.Product_Name_vod__c)
{
System.debug('Inserting in 4th prod');
mc.Product_4_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_4_TPI__c = before_val;
mc.Proposed_Target_4_TPI__c = after_val;
}
}
mcptoUpdate.add(mc);
}
try{
update mcptoUpdate;
system.debug('\n mc cycle plan after update -> '+mcptoUpdate);
}
catch(Exception ex){
System.debug('\n exception aa => '+ex.getMessage());
}
}
trigger TPI_Update_MCCP_Prod_GoalEdit_on_MCCP on MC_Cycle_Plan_Product_vod__c (after update, after insert) {
set<Id> prod_id=new set<Id>();
set<Id> channel_id=new set<Id>();
List<Id> target_id=new List<Id>();
set<Id> target_id2=new set<Id>();
set<Id> plan_id=new set<Id>();
set<Id> channel_id1=new set<Id>();
set<Id> prod_id1=new set<Id>();
decimal before_val;
decimal after_val;
string prod_name;
AggregateResult[] groupedResults;
integer aggregated_goal_val;
decimal old_goal_val;
decimal new_goal_val;
set<Id> sum = new Set<Id>();
boolean flag;
List<MC_Cycle_Plan_vod__c> mcptoUpdate = New List<MC_Cycle_Plan_vod__c>();
//
//hold value of products edited in set 'prod_id'
//
for (MC_Cycle_Plan_Product_vod__c p: trigger.new)
{
if(trigger.isInsert)
{
prod_name = Trigger.newMap.get(p.id).Product_Name_vod__c;
new_goal_val = Trigger.newMap.get(p.Id).Product_Activity_Goal_vod__c;
flag = true;
}
else if(trigger.isUpdate){
old_goal_val = Trigger.oldMap.get(p.Id).Product_Activity_Goal_vod__c;
prod_name = Trigger.oldMap.get(p.id).Product_Name_vod__c;
new_goal_val = Trigger.newMap.get(p.Id).Product_Activity_Goal_vod__c;
flag=false;
}
//
prod_id.add(p.id);
//
system.debug('MCCP product - ' + prod_id);
System.debug('Old value of MCCP product - '+old_goal_val);
//
//hold values of channels related to products in set 'channel_id' where channel is 'Interaction'
//
if(p.Channel_vod__c=='Interaction')
{
channel_id.add(p.Cycle_Plan_Channel_vod__c);
system.debug('MCCP channel - ' + channel_id);
}
//
//hold values of target in 'target_id'
//
List<MC_Cycle_Plan_Channel_vod__c> t= [select Cycle_Plan_Target_vod__c from MC_Cycle_Plan_Channel_vod__c where Id IN: channel_id];
for (MC_Cycle_Plan_Channel_vod__c mc : t)
{
target_id.add(mc.Cycle_Plan_Target_vod__c);
}
system.debug('MCCP Target - ' + target_id);
//
//hold values of plan in 'plan_id'
//
List<MC_Cycle_Plan_Target_vod__c> t2 = [select id,Cycle_Plan_vod__r.id from MC_Cycle_Plan_Target_vod__c where Id IN : target_id];
for (MC_Cycle_Plan_Target_vod__c mc2 : t2)
{
plan_id.add(mc2.Cycle_Plan_vod__r.id);
}
system.debug('MC Cycle Plan - ' + plan_id);
//
//hold values of all target in 'target_id2'
//
List<MC_Cycle_Plan_Target_vod__c> plan_1 = [select id from MC_Cycle_Plan_Target_vod__c where Cycle_Plan_vod__c in : plan_id];
for (MC_Cycle_Plan_Target_vod__c mc3 : plan_1)
{
target_id2.add(mc3.id);
}
//
//hold values of all channels of above targets where channel is 'Interaction' in 'channel_id1'
//
system.debug('All MCCP Targets of above MC Cycle Plan - ' + target_id2);
List<MC_Cycle_Plan_Channel_vod__c> t3 = [select id,Channel_vod__c from MC_Cycle_Plan_Channel_vod__c where Cycle_Plan_Target_vod__c IN : target_id2];
for (MC_Cycle_Plan_Channel_vod__c mc4 : t3)
{
if(mc4.Channel_vod__c=='Interaction'){
channel_id1.add(mc4.id);
}
}
system.debug('All MCCP Channels of above MCCP Target - ' + channel_id1);
//
//hold value of products edited in set 'prod_id'
//
List<MC_Cycle_Plan_Product_vod__c> p1 = [select id,Product_Activity_Goal_vod__c from MC_Cycle_Plan_Product_vod__c where Cycle_Plan_Channel_vod__c in : channel_id1];
for (MC_Cycle_Plan_Product_vod__c mc5 : p1)
{
prod_id1.add(mc5.id);
}
system.debug('All MCCP Products of above MCCP Channels - '+ prod_id1);
Set<id> result = prod_id1.clone();
system.debug('Removing the product where the value of goal is changed - ' + result.removeall(prod_id));
system.debug('Resulting set of products where goal is not changed -'+result);
List<MC_Cycle_Plan_Product_vod__c> mcp1 =[select Product_Name_vod__c from MC_Cycle_Plan_Product_vod__c where Id in: result];
System.debug(mcp1.size());
//
// Aggregation of goals grouped by product with no change is stored in 'groupedResults'
//
for(MC_Cycle_Plan_Product_vod__c aq : mcp1)
{
if(aq.Product_Name_vod__c ==prod_name)
{
sum.add(aq.id);
}
}
System.debug('Sum'+sum);
groupedResults = [SELECT SUM(Product_Activity_Goal_vod__c)aver FROM MC_Cycle_Plan_Product_vod__c where Id in : sum];
system.debug('Sum of goals grouped by product with no change - ' + integer.valueOf(groupedResults[0].get('aver')) );
//
// typecasting the result into integer data type (aggregated_goal_val = total of all goals which are not edited)
//
aggregated_goal_val = integer.valueOf(groupedResults[0].get('aver'));
System.debug('Aggregated_goal_val in Insert - '+aggregated_goal_val);
//
//Old sum of goals (edited and non edited) grouped by product
//
system.debug('xxx'+trigger.isInsert);
system.debug('xxx'+trigger.isUpdate);
if(flag)
{
before_val = aggregated_goal_val;
System.debug('Before Value in Insert - '+before_val);
System.debug('Aggregated_goal_val in Insert - '+aggregated_goal_val);
}
else
{
before_val = aggregated_goal_val + old_goal_val;
system.debug('Old value of edit goals -'+before_val);
}
//
//New sum of goals (edited and non edited) grouped by product
//
after_val= aggregated_goal_val + new_goal_val;
system.debug('New value of edit goals -'+after_val);
}
List<MC_Cycle_Plan_vod__c> mcp =[select Product_1_TPI__c,Product_2_TPI__c,Product_3_TPI__c,Product_4_TPI__c,HQ_Target_1_TPI__c,HQ_Target_2_TPI__c,HQ_Target_3_TPI__c,HQ_Target_4_TPI__c,Proposed_Target_1_TPI__c,Proposed_Target_2_TPI__c,Proposed_Target_3_TPI__c,Proposed_Target_4_TPI__c from MC_Cycle_Plan_vod__c Where Id IN :plan_id];
for(MC_Cycle_Plan_vod__c mc:mcp){
for(MC_Cycle_Plan_Product_vod__c cyclprod : [Select Product_Name_vod__c from MC_Cycle_Plan_Product_vod__c WHERE ID =:prod_id]){
if (mc.Product_1_TPI__c == ''){
System.debug('Inserting in 1st prod');
mc.Product_1_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_1_TPI__c = before_val;
mc.Proposed_Target_1_TPI__c = after_val;
}
else if (mc.Product_1_TPI__c != '' && mc.Product_1_TPI__c == cyclprod.Product_Name_vod__c)
{
System.debug('Updating in 1st prod');
mc.Product_1_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_1_TPI__c = before_val;
mc.Proposed_Target_1_TPI__c = after_val;
system.debug('satisfy 1');
}
else if (mc.Product_2_TPI__c == '')
{
System.debug('Inserting in 2nd prod');
mc.Product_2_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_2_TPI__c = before_val;
mc.Proposed_Target_2_TPI__c = after_val;
system.debug('satisfy 2');
}
else if (mc.Product_2_TPI__c != '' && mc.Product_2_TPI__c == cyclprod.Product_Name_vod__c)
{
System.debug('Updating in 2nd prod');
mc.Product_2_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_2_TPI__c = before_val;
mc.Proposed_Target_2_TPI__c = after_val;
}
else if (mc.Product_3_TPI__c == '')
{
System.debug('Inserting in 3rd prod');
mc.Product_3_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_3_TPI__c = before_val;
mc.Proposed_Target_3_TPI__c = after_val;
}
else if (mc.Product_3_TPI__c != '' && mc.Product_3_TPI__c == cyclprod.Product_Name_vod__c)
{
System.debug('Updating in 3rd prod');
mc.Product_3_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_3_TPI__c = before_val;
mc.Proposed_Target_3_TPI__c = after_val;
}
else if (mc.Product_4_TPI__c == '')
{
System.debug('Inserting in 4th prod');
mc.Product_4_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_4_TPI__c = before_val;
mc.Proposed_Target_4_TPI__c = after_val;
}
else if (mc.Product_4_TPI__c != '' && mc.Product_4_TPI__c == cyclprod.Product_Name_vod__c)
{
System.debug('Inserting in 4th prod');
mc.Product_4_TPI__c = cyclprod.Product_Name_vod__c;
mc.HQ_Target_4_TPI__c = before_val;
mc.Proposed_Target_4_TPI__c = after_val;
}
}
mcptoUpdate.add(mc);
}
try{
update mcptoUpdate;
system.debug('\n mc cycle plan after update -> '+mcptoUpdate);
}
catch(Exception ex){
System.debug('\n exception aa => '+ex.getMessage());
}
}
Object Relationship is - MC_Cycle_Plan_vod__c -> MC_Cycle_Plan_Target_vod__c -> MC_Cycle_Plan_Channel_vod__c -> MC_Cycle_Plan_Product_vod__c (MD->MD->MD->MD). I am doing an insert on 'MC_Cycle_Plan_Product_vod__c '. As per the log, update is happening on MC_Cycle_Plan (Top Parent) - I have no idea what is going on.

Is it a master detail relation ship, between any of the object? and any rollup summery field there?