function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HNT_NeoHNT_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? 
 
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;
        }
    }
}

 
Best Answer chosen by HNT_Neo
LBKLBK
I guess you have a lookup relationship with Account in this case.

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.
 
CAPdata__c objCAPdata = new CAPdata__c();

objCAPdata.Account__c = PROVIDE_THE_ACCOUNT_ID_HERE;
//objCAPdata.Volume_Market__c = 'A';
//objCAPdata.Region_Program_Requirements__c = 'Midwest';

//Please make sure you are including the mandatory fields here, if any.

insert objCAPdata;
Replace PROVIDE_THE_ACCOUNT_ID_HERE with the actual id of the account that belongs to Volume Market A and Region Midwest.
 

All Answers

LBKLBK
Hi,

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?
 
CAPdata__c objCAPdata = new CAPdata__c();

objCAPdata.Volume_Market__c = 'A';
objCAPdata.Region_Program_Requirements__c = 'Midwest';

//Please make sure you are including the mandatory fields here, if any.

insert objCAPdata;
Let me know how it goes.
 
HNT_NeoHNT_Neo
I do have one other issue with this, The volume market and region fields are formula fields that are extracted from the Account object. So in reality, those fields exist on the Account object. Unable to write to these fields since they are formulas.
LBKLBK
I guess you have a lookup relationship with Account in this case.

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.
 
CAPdata__c objCAPdata = new CAPdata__c();

objCAPdata.Account__c = PROVIDE_THE_ACCOUNT_ID_HERE;
//objCAPdata.Volume_Market__c = 'A';
//objCAPdata.Region_Program_Requirements__c = 'Midwest';

//Please make sure you are including the mandatory fields here, if any.

insert objCAPdata;
Replace PROVIDE_THE_ACCOUNT_ID_HERE with the actual id of the account that belongs to Volume Market A and Region Midwest.
 
This was selected as the best answer
HNT_NeoHNT_Neo
Hello, 

I will try this and let you know by this week. 

Thanks!