You need to sign in to do that
Don't have an account?
HNT_Neo
Updating my Apex Trigger to insert new records
hello,
I'm hoping someone can help me out on this issue.
A while back I created this apex trigger that would update a record from the account and a custom ojbect when 3 variables were present.
However, I noticed that new records were not being entered but only udpated.
Can someone tell me how I would modify my code so that if a new record is identified it will create a new record and update the existing record?
I'm hoping someone can help me out on this issue.
A while back I created this apex trigger that would update a record from the account and a custom ojbect when 3 variables were present.
However, I noticed that new records were not being entered but only udpated.
Can someone tell me how I would modify my code so that if a new record is identified it will create a new record and update the existing record?
trigger CAPdataTiers on CAPdata__c (before insert,before update) { for (CAPdata__c CAP : Trigger.new) { if(CAP.Volume_Market__c.equalsIgnoreCase('A') && CAP.Region_Program_Requirements__c != null && CAP.Region_Program_Requirements__c.equalsIgnoreCase('Midwest')) { //Midwest Market A CAP.TierElitePreferredJobs__c = 36; CAP.TierElitePreferredRV__c = 90000; CAP.TierElitePreferredL6MosTA__c = 90; CAP.TierElitePreferredL6MosDA__c = 90; CAP.TierPreferredJobs__c = 24; CAP.TierPreferredRV__c = 60000; CAP.TierPreferredL6MosTA__c = 75; CAP.TierRewardsAdvantageJobs__c = 12; CAP.TierRewardsAdvantageRV__c = 30000; CAP.TierRewardsAdvantageL6MosTA__c = 75; } else if(CAP.Volume_Market__c.equalsIgnoreCase('B') && CAP.Region_Program_Requirements__c != null && CAP.Region_Program_Requirements__c.equalsIgnoreCase('Midwest')) { //Midwest Market B CAP.TierElitePreferredJobs__c = 18; CAP.TierElitePreferredRV__c = 45000; CAP.TierElitePreferredL6MosTA__c = 90; CAP.TierElitePreferredL6MosDA__c = 90; CAP.TierPreferredJobs__c = 12; CAP.TierPreferredRV__c = 30000; CAP.TierPreferredL6MosTA__c = 50; CAP.TierRewardsAdvantageJobs__c = 6; CAP.TierRewardsAdvantageRV__c = 15000; CAP.TierRewardsAdvantageL6MosTA__c = 25; } else if(CAP.Volume_Market__c.equalsIgnoreCase('A') && CAP.Region_Program_Requirements__c != null && CAP.Region_Program_Requirements__c.equalsIgnoreCase('Northeast')) { //Northeast Market A CAP.TierElitePreferredJobs__c = 36; CAP.TierElitePreferredRV__c = 90000; CAP.TierElitePreferredL6MosTA__c = 75; CAP.TierElitePreferredL6MosDA__c = 90; CAP.TierPreferredJobs__c = 24; CAP.TierPreferredRV__c = 60000; CAP.TierPreferredL6MosTA__c = 50; CAP.TierRewardsAdvantageJobs__c = 12; CAP.TierRewardsAdvantageRV__c = 30000; CAP.TierRewardsAdvantageL6MosTA__c = 50; } else if(CAP.Volume_Market__c.equalsIgnoreCase('B') && CAP.Region_Program_Requirements__c != null && CAP.Region_Program_Requirements__c.equalsIgnoreCase('Northeast')) { //Northeast Market B CAP.TierElitePreferredJobs__c = 18; CAP.TierElitePreferredRV__c = 45000; CAP.TierElitePreferredL6MosTA__c = 75; CAP.TierElitePreferredL6MosDA__c = 90; CAP.TierPreferredJobs__c = 12; CAP.TierPreferredRV__c = 30000; CAP.TierPreferredL6MosTA__c = 50; CAP.TierRewardsAdvantageJobs__c = 6; CAP.TierRewardsAdvantageRV__c = 15000; CAP.TierRewardsAdvantageL6MosTA__c = 25; } else if(CAP.Volume_Market__c.equalsIgnoreCase('A') && CAP.Region_Program_Requirements__c != null && CAP.Region_Program_Requirements__c.equalsIgnoreCase('Pacific Northwest')) { //Pacific Northwest Market A CAP.TierElitePreferredJobs__c = 36; CAP.TierElitePreferredRV__c = 90000; CAP.TierElitePreferredL6MosTA__c = 75; CAP.TierElitePreferredL6MosDA__c = 50; CAP.TierPreferredJobs__c = 24; CAP.TierPreferredRV__c = 60000; CAP.TierPreferredL6MosTA__c = 50; CAP.TierRewardsAdvantageJobs__c = 12; CAP.TierRewardsAdvantageRV__c = 30000; CAP.TierRewardsAdvantageL6MosTA__c = 50; } else if(CAP.Volume_Market__c.equalsIgnoreCase('B') && CAP.Region_Program_Requirements__c != null && CAP.Region_Program_Requirements__c.equalsIgnoreCase('Pacific Northwest')) { //Pacific Northwest Market B CAP.TierElitePreferredJobs__c = 18; CAP.TierElitePreferredRV__c = 45000; CAP.TierElitePreferredL6MosTA__c = 75; CAP.TierElitePreferredL6MosDA__c = 50; CAP.TierPreferredJobs__c = 12; CAP.TierPreferredRV__c = 30000; CAP.TierPreferredL6MosTA__c = 50; CAP.TierRewardsAdvantageJobs__c = 6; CAP.TierRewardsAdvantageRV__c = 15000; CAP.TierRewardsAdvantageL6MosTA__c = 50; } else if(CAP.Volume_Market__c.equalsIgnoreCase('A') && CAP.Region_Program_Requirements__c != null && CAP.Region_Program_Requirements__c.equalsIgnoreCase('South/West')) { //South/West Market A CAP.TierElitePreferredJobs__c = 36; CAP.TierElitePreferredRV__c = 90000; CAP.TierElitePreferredL6MosTA__c = 90; CAP.TierElitePreferredL6MosDA__c = 50; CAP.TierPreferredJobs__c = 24; CAP.TierPreferredRV__c = 60000; CAP.TierPreferredL6MosTA__c = 75; CAP.TierRewardsAdvantageJobs__c = 12; CAP.TierRewardsAdvantageRV__c = 30000; CAP.TierRewardsAdvantageL6MosTA__c = 75; } else if(CAP.Volume_Market__c.equalsIgnoreCase('B') && CAP.Region_Program_Requirements__c != null && CAP.Region_Program_Requirements__c.equalsIgnoreCase('South/West')) { //South/West Market B CAP.TierElitePreferredJobs__c = 18; CAP.TierElitePreferredRV__c = 45000; CAP.TierElitePreferredL6MosTA__c = 90; CAP.TierElitePreferredL6MosDA__c = 50; CAP.TierPreferredJobs__c = 12; CAP.TierPreferredRV__c = 30000; CAP.TierPreferredL6MosTA__c = 75; CAP.TierRewardsAdvantageJobs__c = 6; CAP.TierRewardsAdvantageRV__c = 15000; CAP.TierRewardsAdvantageL6MosTA__c = 75; } } }
In this case, you just need to create the new CAPdata__c record with the right account attached to it.
and the formula will be auto populated.
Code would be like this.
Replace PROVIDE_THE_ACCOUNT_ID_HERE with the actual id of the account that belongs to Volume Market A and Region Midwest.
All Answers
BEFORE triggers do not INSERT or UPDATE a record. They merely change values of a record when an INSERT or UPDATE operation triggers them.
Your trigger as it is should work for both INSERT and UPDATE scenario.
Can you execute the following code in "Developer Console" to see if the newly created record has the Tier information added by the trigger?
Let me know how it goes.
In this case, you just need to create the new CAPdata__c record with the right account attached to it.
and the formula will be auto populated.
Code would be like this.
Replace PROVIDE_THE_ACCOUNT_ID_HERE with the actual id of the account that belongs to Volume Market A and Region Midwest.
I will try this and let you know by this week.
Thanks!