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
HotCopper_SalesHotCopper_Sales 

Trigger on Task - Establish if task is related to Opportunity

I would to create a trigger. I am very new and am having trouble. 

Could somebody please tell me if this is correct?
 
trigger TaskUpdateTrigger on Task (before insert) {
    For (Task t: Trigger.new){
        if(t.what.type == 'Opportunity'){
            System.debug(t.Subject+ 'is related to Opportunity');
        }
    } 
    
}

 
Best Answer chosen by HotCopper_Sales
ManojSankaranManojSankaran
Hi Ben,

Currently this has been posted in Idea and will soon be impletemented. Below is the workaround for now

t.whatId.substring(0,3) == '006' => for opportunity

t.whatId.substring(0,3) == '001' => for account

Mark it as answer if it solves the issue.

Thanks
Manoj S




 

All Answers

ManojSankaranManojSankaran
Hi Ben,

Currently this has been posted in Idea and will soon be impletemented. Below is the workaround for now

t.whatId.substring(0,3) == '006' => for opportunity

t.whatId.substring(0,3) == '001' => for account

Mark it as answer if it solves the issue.

Thanks
Manoj S




 
This was selected as the best answer
HotCopper_SalesHotCopper_Sales
Thank you!
Only thing i would comment is that i need to type cast the Id to string first.
 
String relatedID = String.valueOf(t.whatId); //type cast Id type to String
        	
        	if(relatedID.substring(0,3)=='006'){
            	      System.debug('TEST: '+t.Subject+ ' is related to Opportunity: ' +t.whatId);
                 }