Update Account Comment from Task Insertion Using Trigger

Requirement: Write a trigger to update account comment field from Task comment value on insertion of Task under Account

Solution :


trigger updateAccountCommentFromTask on Task (before insert)
{
String account_prefix = Schema.SObjectType.Account.getKeyPrefix();

Map<Id,Account> accountMap = new Map<Id,Account>();
Set<id> setofWhatID = new Set<id>();
for (Task objTask: Trigger.new) 
{
setofWhatID.add(objTask.WhatId);
}
Map<id,Account> MapAccIDToAccount = new Map<id,Account>([Select Id, Account_Status_Sales__c from Account Where Id in :setofWhatID]);

for (Task objTask: Trigger.new)
if(objTask.Subject != null && objTask.WhatId !=null && ((String)objTask.WhatId).startsWith(account_prefix))
{
Account a = MapAccIDToAccount.get(objTask.WhatId);
a.Comment__C = objTask.Comment__c;
accountMap.put(a.id,a);
}
try
{
update accountMap.values();
}
catch (system.Dmlexception e) 
{
system.debug (e);
}
 }

                                                                   Thanks,
                                                              Sumit Shukla



Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page