+ Start a Discussion
HNT_NeoHNT_Neo 

Creating an Apex Class to invoke an Apex Trigger HELP :)

Hello, 

I'm pretty new with creating APEX triggers and with the help of other community developers here, was able to successfully create the APEX trigger below. 

My next challenge is to run this trigger when a user makes edits to the records these fields are associated with. 

My understanding is that an APEX class is needed to execute the APEX trigger, is this the case? If so, can someone help me in creating the APEX class to accomplish this? 

Here is my test class I created and is working fine and passed. Can we use this as the base to create my APEX Class to run my APEX trigger that is listed below this?

thanks!!!! 
 
public class TestCAPdataTiers {
    static testMethod void insertNewCAPdata() {
        Account acc = new account();
        acc.name = 'test';
        acc.Volume_Market__c = 'A';
        insert acc;
    
        CAPdata__c capdataToCreate = new CAPdata__c();
        
        capdataToCreate.Business_Review_Form_Owner__c = '005d0000000jQ8j';
        capdataToCreate.TierElitePreferredJobs__c = 36;
        capdataToCreate.Points__c = 100000;
        capdataToCreate.Account__c = acc.id;

        insert capdataToCreate;
        }
}


 
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;
        }
    }
}

 
Swayam@SalesforceGuySwayam@SalesforceGuy
Hi,

First of all, Welcome to Salesforce Apex Development World,  You dont need apex class to inovke a Trigger, but you can invoke a apex class from trigger i.e If you want to write custom logic then you can user Apex Class. Trigger will automatically get invoked when you create, edit, delete record.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm



Hope this helps !!
--
Thanks,
Swayam
UC InnovationUC Innovation
You don't need to write an Apex class to run a trigger.  The code within your trigger should run fine as is, assuming it works.  Most people move their trigger code over to an Apex class for better readability/modularity, but it's not necessary.