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
Salesforce2015Salesforce2015 

Combine existing simple triggers into one triggers

Hi Experts,
 
I've one trigger to work for Before actions for Delete & Update.
 
Below is my requirement.
 
Object: Request__c
Before Trigger.....
 
On Delete:
1. Query for related Recipient__c records.  If count > 0, return error (sobject.addError())
  "Please Cancel Request, or Remove Recipients before deleting"
2. Query for related Workflow_Instance__c records where Request_Status__c != 'Cancelled'.  If count > 0, return error:
  "Please Cancel Request before deleting"
 
On Update
3. Query for related Workflow_Instance__c records where Request_Status__c != 'Cancelled',  If count > 0, return error:
  "You cannot edit a submitted Request"
 
I written two triggers for delete action (i.e. for first two points) both triggers working fine. Now combine these two triggers as one trigger and add one more action for update (i.e. 3rd query)
 
Trigger1:
 
Trigger PreventDeletion on Request__c (before delete) {
   for (Request__c Pa : Trigger.Old){
        list<Recipient__c> ChList = [select id, Request__c from Recipient__c where Request__c =: pa.id];  //list to hold child matches the parent
        if(ChList.size() > 0){
            pa.adderror('Please Cancel Request, or Remove Recipients before deleting');
        }
   }
}
 
 
 
Trigger2:
 
Trigger PreventDel on Request__c (before delete) {
   for(Request__c Pa : Trigger.Old){
        list<Workflow_Instance__c> ChList = [select id, Request__c from Workflow_Instance__c where Request_Status__c != 'Cancelled'];  //list to hold child matches the parent
        if(ChList.size() > 0){
            pa.adderror('Please Cancel Request before deleting');
        }
   }
}
 
Help will be appreciated. Thanks in advance.

Thanks,
Manu
 
Amit Chaudhary 8Amit Chaudhary 8
Please check below blog how to write single Trigger
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Please let us know if this will help you
Salesforce2015Salesforce2015
Amit thanks for quick response, i alrady created successful triggers. Some one, need to combine it helps me alot.
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
Trigger PreventDeletion on Request__c (before delete) 
{
   for (Request__c Pa : Trigger.Old)
   {
        list<Recipient__c> ChList = [select id, Request__c from Recipient__c where Request__c =: pa.id];  //list to hold child matches the parent
   
		if(ChList.size() > 0){
            pa.adderror('Please Cancel Request, or Remove Recipients before deleting');
        }
		
		list<Workflow_Instance__c> ChList1 = [select id, Request__c from Workflow_Instance__c where Request_Status__c != 'Cancelled'];  
		
        if(ChList1.size() > 0)
		{
            pa.adderror('Please Cancel Request before deleting');
        }		
   }
}

 
Salesforce2015Salesforce2015
Thanks for the update.

I have total 3 quaries...

On Delete:
1. Query for related Recipient__c records.  If count > 0, return error (sobject.addError())
  "Please Cancel Request, or Remove Recipients before deleting"
2. Query for related Workflow_Instance__c records where Request_Status__c != 'Cancelled'.  If count > 0, return error:
  "Please Cancel Request before deleting"
On Update:
3. Query for related Workflow_Instance__c records where Request_Status__c != 'Cancelled',  If count > 0, return error:
  "You cannot edit a submitted Request"

Please modify below code.....

Trigger PreventDeletion1 on Request__c (before delete, before update)
{

 if( Trigger.isDelete ) {

   for (Request__c Pa : Trigger.Old)
   {

        list<Recipient__c> ChList1 = [select id, Request__c from Recipient__c where Request__c =: pa.id];
   
        if(ChList1.size() > 0){
            pa.adderror('Please Cancel Request, or Remove Recipients before deleting');
        }
        

        list<Workflow_Instance__c> ChList2 = [select id, Request__c from Workflow_Instance__c where Request_Status__c != 'Cancelled'];  
        
        if(ChList2.size() > 0)
        {
            pa.adderror('Please Cancel Request before deleting');
        }
   }
   
   
   elseif ( Trigger.isUpdate )       

   for (Request__c Pa : Trigger.Old)
   {

        list<Workflow_Instance__c> ChList3 = [select id, Request__c from Workflow_Instance__c where Request_Status__c != 'Cancelled'];  
        
        if(ChList3.size() > 0)
        {
            pa.adderror('You cannot edit a submitted Request');
        }       

   }
       
  }
}