You need to sign in to do that
Don't have an account?

Help with trigger
I have following trigger which creates a log in the custom object whenever a task is modified.
There are two problems the trigger is creating a log record even when the new task is created. It should only created the record when the task is modified.
Also it is creating a log for all the objects. I only want the log to be captured for Accounts/Contacts i.e whatid should only be account/contact record.
Can someone please help in refining the trigger.
Thanks in advance.
trigger CommentLog on Task (before update) { List<CommentLog__c> newcomments = new List<CommentLog__c> (); Map<Id, Task> mTask = new Map<Id, Task> (); //build the task map first for(Task t: [select Id, owner.name,LastModifiedBy.name,what.name from Task where Id in : Trigger.old]) { mTask.put(t.id,t); } //build comment log for(Task t: trigger.old) { CommentLog__c c = new CommentLog__c(); Task tt = mTask.get(t.Id); c.Previous_Comment__c=t.Description; c.CommentDate__c=t.LastModifiedDate; c.Name= (tt!=null?tt.LastModifiedBy.name:null); c.Task_owner__c = (tt!=null? tt.owner.name:null); c.type__c='Task'; c.Taskid__c=t.id; c.Link_to_Task__c=URL.getSalesforceBaseUrl().toExternalForm() + '/' + t.id; c.whatid__c=URL.getSalesforceBaseUrl().toExternalForm() + '/' +t.whatid; c.Account_Contact_Name__c=(tt!=null?tt.what.name:null); c.Account_Contact_ID__c = t.whatid; newcomments.add(c); } //insert comment log list insert newcomments; }
Hi,
You want to create log wnhe the account and contact task s are modified? Can you clarify requirement.
WhatId = accountId/opportunityID
whoId = leadId/ContactID
You are absolutely correct.
Thanks