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
Scott Janis 20Scott Janis 20 

Help with a very unusual Trigger

Hello, 

I need help in getting  test coverage for this trigger. I am a newbie but I've looked at some trailheads and the type triggers they show as examples are much different then this trigger. I just need to know HOW I showd starte as this looks like it access a bunch of oother APEX classes. I really just want to deactivate it in production which is the only reason I need to push it from Dev to Prod. 
Thanks. 

trigger MDP_Tgr_Opportunity_After on Opportunity (after insert, after update, after delete) {

    
    system.debug('in the MDP_Tgr_Opportunity_After trigger');
    
    if(!MDP_Utils.updateOpportunityfromSalesTeamHandler){

      
        if(!V2SA_Trg_Opportunity_Helper.isValidVPAccountOwner(Trigger.newMap)) return;

        
        if (trigger.isInsert) {
            system.debug('in the insert statement of MDP_Tgr_Opportunity_After trigger');
            
            system.debug('LR: AFTER VP VALIDATION');

           
            MDP_Tgr_Opportunity_Handler.CreateSharesOnInsert(trigger.new);
            
            
            
           
            system.debug('end of the insert statement of MDP_Tgr_Opportunity_After trigger');
     
        if(trigger.isUpdate){
            system.debug('in the update statement of MDP_Tgr_Opportunity_After trigger');
            
            MDP_Tgr_Opportunity_Handler.CreateSharesOnUpdate(trigger.new, trigger.oldMap);
            
            
            MDP_Tgr_Opportunity_Handler.sendOMSMessages(Trigger.new, Trigger.oldMap);
            
            MDP_Tgr_Opportunity_Handler.setPriceBookAfterUpdate(Trigger.new, Trigger.oldMap);
            system.debug('end of the update statement of MDP_Tgr_Opportunity_After trigger'); 
        }
    }
    system.debug('end of the MDP_Tgr_Opportunity_After trigger');

} // End: trigger MDP_Tgr_Opportunity_After
 
Best Answer chosen by Scott Janis 20
Andrew GAndrew G
Hi Scott

If we are to assume that the trigger exists in Production and you want to disable the entire trigger, you have a few options that will save you the need to do a test class.

https://help.salesforce.com/articleView?id=000327373&language=en_US&type=1&mode=1


Now, if you really want to write some test code, then you will need to work out what the different methods are doing:
Reading these two lines:
if(!MDP_Utils.updateOpportunityfromSalesTeamHandler){
      
        if(!V2SA_Trg_Opportunity_Helper.isValidVPAccountOwner(Trigger.newMap)) return;
I suspect that they will be some sort of security utility (this is a guess) that works out if the Opportunity is being updated by someone from the Sales Team and then if the Account Owner is Valid. (what ever valid means within the context of your code and organisation).
So I would look at creating a RunAs test user who has the membership (that is a member of the Sales team) to run the trigger .

Now the good (and or bad) news, depending on your point of view - Triggers do not need 75% coverage, just at least 1% on the trigger, but you need 75% across all apex in the org.
So, a simple test class where you create a RunAs User and then get that user account to do the insert of a test Oppty should give you coverage for the trigger. This however does assume that your code coverage for the called methods is satisfactory.

I would however first check whether the entire trigger can be deactivated and review the suggestions in the linked article above.

Regards
Andrew
 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Scott,

In case if you would like to check the flow of the trigger then you might want to capture the debug logs while create a new record and inserting the newly created record.

I hope this helps.

Regar,
Anutej
Andrew GAndrew G
Hi Scott

If we are to assume that the trigger exists in Production and you want to disable the entire trigger, you have a few options that will save you the need to do a test class.

https://help.salesforce.com/articleView?id=000327373&language=en_US&type=1&mode=1


Now, if you really want to write some test code, then you will need to work out what the different methods are doing:
Reading these two lines:
if(!MDP_Utils.updateOpportunityfromSalesTeamHandler){
      
        if(!V2SA_Trg_Opportunity_Helper.isValidVPAccountOwner(Trigger.newMap)) return;
I suspect that they will be some sort of security utility (this is a guess) that works out if the Opportunity is being updated by someone from the Sales Team and then if the Account Owner is Valid. (what ever valid means within the context of your code and organisation).
So I would look at creating a RunAs test user who has the membership (that is a member of the Sales team) to run the trigger .

Now the good (and or bad) news, depending on your point of view - Triggers do not need 75% coverage, just at least 1% on the trigger, but you need 75% across all apex in the org.
So, a simple test class where you create a RunAs User and then get that user account to do the insert of a test Oppty should give you coverage for the trigger. This however does assume that your code coverage for the called methods is satisfactory.

I would however first check whether the entire trigger can be deactivated and review the suggestions in the linked article above.

Regards
Andrew
 
This was selected as the best answer
Scott Janis 20Scott Janis 20
Thanks. I set up an environment with Visual Studio , Salesforce DX. I still could not deploy this as "Inactive' or Deleted. I was ableo to write some sripts for testing but this is the one I'm having the issue with. 
Here is te trigger : 


 trigger MDP_Tgr_Opportunity_Before on Opportunity (before insert, before update, before delete) {    
    
    system.debug('in the MDP_Tgr_Opportunity_Before trigger - MDP_Utils.updateOpportunityfromSalesTeamHandler : - '+MDP_Utils.updateOpportunityfromSalesTeamHandler);
    if(!MDP_Utils.updateOpportunityfromSalesTeamHandler){
        // for update
        
        if(trigger.isUpdate){ 
            system.debug('MDP_Utils.updateOpportunityfromSalesTeamHandler : - '+MDP_Utils.updateOpportunityfromSalesTeamHandler);
            MDP_Tgr_Opportunity_Handler.checkFieldValueChange(trigger.new, trigger.oldMap); 
            
            // tlm 05/21/2018 - set/verify the Sales Team flds
            //MDP_Tgr_Opportunity_Handler.FetchSalesTeams(trigger.new, trigger.oldMap);
            
            //Moved process builder logic to trigger
            MDP_Tgr_Opportunity_Handler.Set_Won_Lost_OpportunitiesUpdate(trigger.new, trigger.oldMap);
            MDP_Tgr_Opportunity_Handler.Set_Original_Amount_COS_AmountUpdate(trigger.new, trigger.oldMap);
            
            // tlm, 06/18/2018: jira SF2-797: Sums the OLI NEP values and updates Opportunity.Estimated_NEP__c
            MDP_Tgr_Opportunity_Handler.setNEPTotal(trigger.new);
            
            //Shridhar Hegde JIRA - Sf2-995
            MDP_Tgr_Opportunity_Handler.ProductInfoNeeded(trigger.new, trigger.oldMap);
            
            //Shridhar Hegde JIRA -SF2 -1061
            MDP_Tgr_Opportunity_Handler.setOpportunityNameOnUpdate(trigger.new, trigger.oldMap);
            
            // filter for check total Nep- 1053
            MDP_Tgr_Opportunity_Handler.checkTotalNEP(trigger.new,trigger.oldMap);      //1053_banani
            
            //Samuel johnson JIRA - SF2-1131
            //MDP_Tgr_Opportunity_Handler.DigitalPodUpdate(trigger.new, trigger.oldMap);
        }
        // for delete
        if(trigger.isDelete){
     
         if(!V2SA_Trg_Opportunity_Helper.isValidVPAccountOwner(Trigger.oldMap)) return;
   MDP_Tgr_Opportunity_Handler.DeleteDigitalDeliveryOpps(trigger.old);
        }
        // For Insert
        if(trigger.isInsert){
            MDP_Tgr_Opportunity_Handler.assginAdCampaign(trigger.new);
            MDP_Tgr_Opportunity_Handler.populateFieldsBeforeInsert(trigger.new);
            
            // tlm 05/21/2018 - set the Sales Team flds
            //MDP_Opportunity_Util.FetchSalesTeamsForInsertedOpps(trigger.new);
            
            //MDP_Tgr_Opportunity_Handler.checkTotalNEP(trigger.new,null);          // moved to the after trigger so we have an ID
            
            //Moved process builder logic to trigger
            MDP_Tgr_Opportunity_Handler.Set_Won_Lost_OpportunitiesInsert(trigger.new);
            MDP_Tgr_Opportunity_Handler.Set_Original_Amount_COS_AmountInsert(trigger.new);
            
            //Shridhar Hegde JIRA -SF2 -1061
            MDP_Tgr_Opportunity_Handler.setOpportunityNameOnInsert(trigger.new);//1053_banani
            
            //Samuel johnson JIRA - SF2-1131
            //MDP_Tgr_Opportunity_Handler.DigitalPodUpdate(trigger.new, null);
            
        }
    }
}
 
Scott Janis 20Scott Janis 20
And here is the class the trigger seems to be accessing. I just need to deply an inactive version of the trigger.