2

I have created a custom field 'Account__c' on activity.Now what i am trying to achieve is whenever there is an activity on account , i want my custom field "Account__c" to get auto populated with the account name.

Can you please help or guide me in how can i achieve this.I am a little new to activities and configuration stuff

Thanks in advance Samir

2

You can write a trigger on Task and event for this(before insert scenario) and check if it is related to an account, populate your custom field with the account name by fetching account name corresponding to that Account Id.Below is the code i wrote on task trigger:

trigger TaskTrigger on Task (before insert) {

List<id> lstAccountIds = new List<Id>();
String str;
for(Task obj: trigger.new){

str=obj.whatid;
if(str!=null && str.startswith('001')){

lstAccountIds.add(obj.whatid);



}

}


map<id,String> mapAccounts  = new map<Id,String>();

for(Account objAcnt:[select id,name from Account where id in:lstAccountIds])
{

mapAccounts.put(objAcnt.id,objAcnt.name);   


}   


for(task objTask:trigger.new){
str=objtask.whatid;

if(str!=null && str.startswith('001')){

objTask.Account__c = mapAccounts.get(objTask.whatid);   


}   


}

}

P.S. Use helper classes for writing triggers.Above code is just for the logic building .

  • Please mark this as the Solution so that other may benefit from it :) – Abhijeet Aug 4 '16 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.