+ Start a Discussion
Deepika G PDeepika G P 

Want to run trigger for only one object

I have an Custom Object 'Customer participation' and Task is created for this object, now when I send an email from the related list with the subject as executive ,a field on task object should be inserted with an value. I have created a trigger but the trigger is working on all the objects. but i want it to work only on the emails of 'Customer participation'. Could anyone help me on this please.

trigger updatetaskoriginfield on Task (before insert, before update)
 {
     if(trigger.isBefore)
     {
            for(Task tsk: trigger.new)
            {
                 String test=tsk.subject;
                 set<string> a=new Set<string>{'executive','Executive'};
                 set<string> b=new Set<string>{'award','Award'};
                 List<String> lsttest=test.split(' ');
                 for(String s: lsttest)
                 {
                     if(a.contains(s))
                     {
                           tsk.Task_Origin__c='Executive Summary';
                     }
                     else if(b.contains(s))
                     {
                           tsk.Task_Origin__c='Award Notification';
                     }
                }
             }  
     }     
}
Best Answer chosen by Deepika G P
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
trigger updatetaskoriginfield on Task (before insert, before update)
 {
    if(trigger.isBefore)
    {
		String keyPrefix = Customer_participation__c.sObjectType.getDescribe().getKeyPrefix();

        for(Task tsk: trigger.new )
        {
            String strWhatId = tsk.whatId;
            if( strWhatId != null && strWhatId !='' && strWhatId.startsWith(keyPrefix) )
            {
				String test=tsk.subject;
				set<string> a=new Set<string>{'executive','Executive'};
				set<string> b=new Set<string>{'award','Award'};
				List<String> lsttest=test.split(' ');
				for(String s: lsttest)
				{
					 if(a.contains(s))
					 {
						   tsk.Task_Origin__c='Executive Summary';
					 }
					 else if(b.contains(s))
					 {
						   tsk.Task_Origin__c='Award Notification';
					 }
				}
            }  
		}  
    }   
}
Test class like below
@istest
Public class UpdateProjectScheduleFieldsTrigger
{
    public static testmethod void errorMsg()
	{
		Customer_participation__c custPart = new Customer_participation__c();
		custPart.name='demo';
		// add all required field
		insert custPart;
			
        task t= new task();
		t.whatId = custPart.id;
        t.ownerID=userinfo.getuserid();
        t.Subject='Run Test Trigger';
        t.Status='Not Started';
        t.Priority='Normal';
         
		insert t;
		update t;
    }

}
Let us know if this will help you


 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
On WhatId you can check record is related with Customer participation or not. That what you can check with key prefix. For more information please check below post
1) http://amitsalesforce.blogspot.in/2015/12/standard-field-record-id-prefix-decoder.html

Please update your code like below
trigger updatetaskoriginfield on Task (before insert, before update)
 {
    if(trigger.isBefore)
    {
		// please add your object Name here for Customer_participation__c
		String keyPrefix = Customer_participation__c.sObjectType.getDescribe().getKeyPrefix(); 
		System.debug('PREFIX--' + keyPrefix );

			for(Task tsk: trigger.new)
            {
				String strWhatId = tsk.whatId;
			
				if( strWhatId.startsWith(keyPrefix) ) // Chck record is related with Customer_participation__c only
				{
					 String test=tsk.subject;
					 set<string> a=new Set<string>{'executive','Executive'};
					 set<string> b=new Set<string>{'award','Award'};
					 List<String> lsttest=test.split(' ');
					 for(String s: lsttest)
					 {
						 if(a.contains(s))
						 {
							   tsk.Task_Origin__c='Executive Summary';
						 }
						 else if(b.contains(s))
						 {
							   tsk.Task_Origin__c='Award Notification';
						 }
					}
				}	
            }  
    }     
}

Let us know if this will help you

Thanks
Amit Chaudhary
Deepika G PDeepika G P
Thanks You Amit!
 
shephalishephali
Hi Deepika,
          I want to know that how have you created task for custom object? I have seen task object active for standard object like contact, opportunity, cases, account. but in case of custom object i dont have any idea for the same. Your answer will be appreciating.

Thanks 
Shephali
Deepika G PDeepika G P
Trigger can create a task for any custom object
Amit Chaudhary 8Amit Chaudhary 8
Hi Deepika / Shephali,

You can create the task on any custom object but "Track Activities" should be checked on custom object
Please check below post for more information
1) https://help.salesforce.com/apex/HTViewHelpDoc?id=tracking_activities_for_custom_objects.htm
2) https://help.salesforce.com/apex/HTViewSolution?id=000004351&language=en_US


In order for a Custom Object to show in the "Related To" Lookup list for an Event or Task, the "Allow Activities" (Track Activities) check-box must be checked in the Custom Object Definition. To do this:
   1. Go to Setup | Create | Objects.
   2. Click Edit to modify an existing custom object.
   3. Check the "Allow Activities" check-box under Optional Features.

Let us know if this will help you
shephalishephali
Thanks Amit for this awesome thing. Small changes makes bigger effects.  
Thanks a lot :)
Deepika G PDeepika G P
Hi all,

Below trigger is working fine as per the requirement but when i run the test class , below error is thrown ,
Can anyone help me on this please?
UpdateProjectScheduleFieldsTriggererrorMsgSystem.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updatetaskoriginfield: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updatetaskoriginfield: line 15, column 1: [] 
Stack Trace: Class.UpdateProjectScheduleFieldsTrigger.errorMsg: line 14, column 1

trigger updatetaskoriginfield on Task (before insert, before update)
 {
     if(trigger.isBefore)
     {
       
       // please add your object Name here for Customer_participation__c

       String keyPrefix = Customer_participation__c.sObjectType.getDescribe().getKeyPrefix();

     //  System.debug('PREFIX--' + keyPrefix );
           for(Task tsk: trigger.new )
            {
             String strWhatId = tsk.whatId;
             //  if(strWhatId.startsWith('a0v'))   
                if( strWhatId.startsWith(keyPrefix) ) // Chck record is related with Customer_participation__c only
                {
                 String test=tsk.subject;
                 set<string> a=new Set<string>{'executive','Executive'};
                 set<string> b=new Set<string>{'award','Award'};
                 List<String> lsttest=test.split(' ');
                 for(String s: lsttest)
                 {
                     if(a.contains(s))
                     {
                           tsk.Task_Origin__c='Executive Summary';
                     }
                     else if(b.contains(s))
                     {
                           tsk.Task_Origin__c='Award Notification';
                     }
                }
             }  
     }  
     }   
}
 
Deepika G PDeepika G P

Appreciate your help on this!
 
Deepika G PDeepika G P
Hi Amit,

Below is my test class 

@istest
Public class UpdateProjectScheduleFieldsTrigger{



    public static testmethod void errorMsg(){
    
        task t= new task();
        t.ownerID=userinfo.getuserid();
        t.Subject='Run Test Trigger';
        t.Status='Not Started';
        t.Priority='Normal';
         
         insert t;
         update t;
    }


}
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
trigger updatetaskoriginfield on Task (before insert, before update)
 {
    if(trigger.isBefore)
    {
		String keyPrefix = Customer_participation__c.sObjectType.getDescribe().getKeyPrefix();

        for(Task tsk: trigger.new )
        {
            String strWhatId = tsk.whatId;
            if( strWhatId != null && strWhatId !='' && strWhatId.startsWith(keyPrefix) )
            {
				String test=tsk.subject;
				set<string> a=new Set<string>{'executive','Executive'};
				set<string> b=new Set<string>{'award','Award'};
				List<String> lsttest=test.split(' ');
				for(String s: lsttest)
				{
					 if(a.contains(s))
					 {
						   tsk.Task_Origin__c='Executive Summary';
					 }
					 else if(b.contains(s))
					 {
						   tsk.Task_Origin__c='Award Notification';
					 }
				}
            }  
		}  
    }   
}
Test class like below
@istest
Public class UpdateProjectScheduleFieldsTrigger
{
    public static testmethod void errorMsg()
	{
		Customer_participation__c custPart = new Customer_participation__c();
		custPart.name='demo';
		// add all required field
		insert custPart;
			
        task t= new task();
		t.whatId = custPart.id;
        t.ownerID=userinfo.getuserid();
        t.Subject='Run Test Trigger';
        t.Status='Not Started';
        t.Priority='Normal';
         
		insert t;
		update t;
    }

}
Let us know if this will help you


 
This was selected as the best answer
Deepika G PDeepika G P
Thanks a lot Amit!

The test class is already in production, so if i change it , will it effect anyother things in production?

 
Amit Chaudhary 8Amit Chaudhary 8
Just deploy above trigger and Test class in production
Deepika G PDeepika G P
Thanks Amit!