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
Deepika G PDeepika G P 

trigger should run 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.I am begineer in salesforce. 

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
I found this as duplicate post. Please check below post for your solution.
1) https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BaTcIAK
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';
						 }
					}
				}	
            }  
    }     
}



 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
I found this as duplicate post. Please check below post for your solution.
1) https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BaTcIAK
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';
						 }
					}
				}	
            }  
    }     
}



 
This was selected as the best answer
Deepika G PDeepika G P
I am getting error for this .Incorrect signature
Deepika G PDeepika G P
I corrected the signature but now the trigger is not working
Amit Chaudhary 8Amit Chaudhary 8
Please try below code AND  let us know if this will help you
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';
						 }
					}
				}	
            }  
    }     
}


 
Deepika G PDeepika G P
This code is working fine for me. Thank you . But just wanted to know the difference.
Deepika G PDeepika G P
@ Sujit,

I have converted whatid  to String using below code, I am able to save but trigger is not working.

 String strWhatId = tsk.whatId;
   if(strWhatId.startsWith('[a0v]'))   
Amit Chaudhary 8Amit Chaudhary 8
just try like below only


 String strWhatId = tsk.whatId;
   if(strWhatId.startsWith('a0v'))   

 
Deepika G PDeepika G P
Thanks Amit! It is working.