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
jleehjleeh 

Simple Trigger and Method help needed

I need to take a trigger and move it into a class method. The trigger works correctly, but I need to move it into a method and call it from 2nd trigger that is already calling other methods. I'm doing this because I don't want multiple triggers on one object.

What should the method look like?

trigger dealUpdate on Deal_Update__c (before insert)
{
    set<string> bpidSet = new set<string>();

    for (Deal_Update__c spBPID : Trigger.new){
       if(spBPID.Partner_BPID__c!=null) bpidSet.add(spBPID.Partner_BPID__c);
    }

    if(bpidSet.size()>0){
       List<Account> accountList = [SELECT Id, SP_BPD__c FROM Account
                                    WHERE SP_BPD__c = :bpidSet AND IsPersonAccount=FALSE
                                    AND RecordType.DeveloperName='Partner_Account'];

       Map<string,id> accountmap = new Map<string,id>();

       for (Account a : accountList) {
          accountmap.put(a.SP_BPID__c,a.id);
       }

        for (Deal_Update__c spBPID : Trigger.new){

          if(accountmap != null) {
              spBPID.Partner_Account__c = accountmap.get(spBPID.Partner_BPID__c);
          }
        }
    }
}


 
Best Answer chosen by jleeh
Amit Chaudhary 8Amit Chaudhary 8
Update your code like below
trigger DealUpdate on Deal_Update__c (before update, before insert) {
    
	dealUpdateTriggerHandler handler = new dealUpdateTriggerHandler();
	
    if(Trigger.isInsert)
	{
        handler.dealUpdateAction(true, trigger.new, null);
        handler.OnBeforeInsert(trigger.new);
    }
    
    if(Trigger.isUpdate)
	{
        handler.dealUpdateAction(false, trigger.new, trigger.oldmap);
    }

}
I hope you created all above method in handler class
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below

Trigger
trigger dealUpdate on Deal_Update__c (before insert)
{
	dealUpdateTriggerHandler handler = new dealUpdateTriggerHandler();
	
	if(Trigger.isInsert)
	{
		handler.OnBeforeInsert(Trigger.New);
	}
	
}

Handler class
public with sharing class dealUpdateTriggerHandler 
{
	public void OnBeforeInsert(List<Deal_Update__c> newDealUpdate)
    {
	
		set<string> bpidSet = new set<string>();
		for (Deal_Update__c spBPID : newDealUpdate )
		{
		   if(spBPID.Partner_BPID__c!=null) 
			bpidSet.add(spBPID.Partner_BPID__c);
		}

		if(bpidSet.size()>0)
		{
		   List<Account> accountList = [SELECT Id, SP_BPD__c FROM Account
										WHERE SP_BPD__c = :bpidSet AND IsPersonAccount=FALSE
										AND RecordType.DeveloperName='Partner_Account'];

		   Map<string,id> accountmap = new Map<string,id>();

		   for (Account a : accountList) {
			  accountmap.put(a.SP_BPID__c,a.id);
		   }

			for (Deal_Update__c spBPID : newDealUpdate)
			{

				if(accountmap != null) 
				{
					spBPID.Partner_Account__c = accountmap.get(spBPID.Partner_BPID__c);
				}
			}
		}

    }

}

Please check below post for trigger framwork
1) http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Let us know if this will help you

Thanks
Amit Chaudhary
jleehjleeh
Hi Amit - Thanks for the feedback, but I'm receiving the follow error.
[OPERATION FAILED]: triggers/DealUpdate.trigger: Variable does not exist: new (Line: 5, Column: 9)

My trigger is below:

trigger DealUpdate on Deal_Update__c (before update, before insert) {
    
    if(Trigger.isInsert){
        new dealUpdateTriggerHandler().dealUpdateAction(true, trigger.new, null);
        new.dealUpdateTriggerHandler().OnBeforeInsert(trigger.new);
    }
    
    if(Trigger.isUpdate){
        new dealUpdateTriggerHandler().dealUpdateAction(false, trigger.new, trigger.oldmap);
    }

}
Amit Chaudhary 8Amit Chaudhary 8
Update your code like below
trigger DealUpdate on Deal_Update__c (before update, before insert) {
    
	dealUpdateTriggerHandler handler = new dealUpdateTriggerHandler();
	
    if(Trigger.isInsert)
	{
        handler.dealUpdateAction(true, trigger.new, null);
        handler.OnBeforeInsert(trigger.new);
    }
    
    if(Trigger.isUpdate)
	{
        handler.dealUpdateAction(false, trigger.new, trigger.oldmap);
    }

}
I hope you created all above method in handler class
 
This was selected as the best answer
jleehjleeh
I updated the trigger per your guidance. I'm still receiving an error on just the one line:

handler.OnBeforeInsert(trigger.new);

Error: Method does not exist or incorrect signature (List<Deal_Update__c> newDealUpdate)
jleehjleeh
Thanks Amit - Your original suggestion worked for the trigger, and it appears Maven's Mate was not working correctly (which I'm seeing a lot lately). Thanks again