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
tgk1tgk1 

Pull completed date value from task and update a field with the value on account

Hi everyone, I could really use some help with a trigger I'm trying to make.  I essentially need a field on the account object to be populated with the latest completed task date if it meets two conditions:

 

 

1.  The task has been marked as completed

2.  The task field "Spoke_with__c" equals Yes

 

I tried to take a stab at it but I couldn't get the code to compile.  Sample code to get me going would be greatly appreciated.

 

Here are the variables-

Task custom field  (picklist)- Spoke_With__c

Account custom field (date/time)-  Last_SW_Activity__c

 

 

Thanks!

 



sfdcfoxsfdcfox
trigger updateSWActivity on Account (before update) {
  for(Account a:[Select Id,(Select Id,ActivityDate FROM Tasks WHERE isclosed = true and Spoke_With__c = true ORDER BY ActivityDate DESC LIMIT 1) from Account WHERE ID in :trigger.new]) {
    if(a.activityhistories<>null&&a.activityhistories.size()==1) {
      trigger.newmap.get(a.id).last_sw_activity__c = a.activityhistories[0].ActivityDate;
    }
  }
}

Try this out, kick the tires, and see if it works.

tgk1tgk1

Thanks sfdcfox, I appreciate your help.  This didn't actually work out for me, it compiles but doesn't function.  Instead of using the "Spoke_With__c" filter we can use the filter of "starts with sw".   However I'm not sure what the operation for "starts with" is.  In other words  "and Subject starts with sw".

 

Thanks again for your help I appreciate it.

sfdcfoxsfdcfox

That would be something like:

 

FieldName LIKE "sw%"

The "%" is a "wildcard", meaning it matches any number of characters (even none at all), so this is how you would implement "starts with."

tgk1tgk1

Thanks for the help fox.  I tried tweaking the code but I still couldn't get this to work.  I ended up starting from scratch (trying to get some experience developing with apex since I'm new to it) and I think I'm pretty close.  If anyone can please take a look at my code and help me out I would appreciate it.

 

trigger LastSWDate on Task (before insert, before update) {
	
	//To do - If the subject of a completed task contains "SW", put the date of the completed task in the 
	//the "Last_SW_Activity__c" field on the account object

//Create a set of related account ID's
Set <ID> acctIDs = new Set <ID> ();


//For every task, add it's related to account ID to the set
	for (Task t: Trigger.new){
	  acctIDs.add(t.accountID);
//Create a map to match the task related to ID's with their corresponding account ID's
	  Map<ID, Account> acctMap = new Map<ID, Account> ([Select ID, Last_SW_Activity__c from Account where ID in :acctIDs]);
//Create the account object
      Account acctRec = acctMap.get(t.accountID);

//If the account ID isn't null, the subject line starts with "sw", and the task has been marked as completed    
	If (t.accountID =!null && t.subject.indexOf('sw') && t.Status == 'Completed')
//Check to see if the Last_SW_Activity__c field is current compared with the latest completed activity
  	  If (acctMap.get.(t.accountID).Last_SW_Activity__c < t.endDateTime || acctMap.get(t.accountID).Last_SW_Activity ==null){
//Update the Last_SW_Activity__c field on the account object with the task's end date  
  		  acctrec.Last_SW_Activity__c = t.endDatetime;
  	}
  }
}

 The error I'm getting states "Invalid field endDatetime for SObject Task".  It's refering to the last line of code.  Does anybody have any idea why it's giving me that error?