0

I have a requirement. Contact related to Task, I need to pull his functional role ( custom field) on Activity page layout. So whenever I create or edit an activity and relate with ant contact so his/her functional role should populate automatic.

Please help! How can I achieve it? I tried formula field but unable to get. I tried through trigger but I tried through trigger but it is not working neither on new task nor while I update the task.

Please find below code:

trigger updatefunctionalroletxt on Task (before insert, before update) {

Map<ID,String> confunrole = new Map<ID,String>();
List<Task> conTasks = new List<Task>();


for (Task e : trigger.new) {

    if (e.whoID!= null && (String.valueOf(e.whoID)).startsWith('003'))  {

        if (trigger.isInsert || (trigger.isUpdate && e.WhoID != trigger.oldMap.get(e.id).WhoID)) {
            confunrole.put(e.whoID,'');
            conTasks.add(e);
        }
    }
}

for (contact con : [SELECT Functional_Role__c FROM contact WHERE ID IN :confunrole.keySet()]) {
    confunrole.put(con.id,con.Functional_Role__c);
}
// Update the contact functional role field on the Task with the relevant value
for (Task e : trigger.new) {
    e.functional_role__c = confunrole.get(e.whoID);
}
}

Kindly help to write a correct trigger.

1
  • Your trigger context is before you need to change it to after, because you won't be able to get record Id in after insert
    – d_k
    Commented Jun 28, 2016 at 17:43

1 Answer 1

0

Your trigger seems to be fine! You can not do that while you are updating a record (aprat from dependent picklist) I suggest you use a VF page for your requirement.

Here is a similar answer

11
  • Hi, This trigger is not working event with new records. Commented Jun 27, 2016 at 12:25
  • Getting error : Execute Anonymous Error Line: 1, Column: 0 required (...)+ loop did not match anything at input 'trigger' Commented Jun 27, 2016 at 13:05
  • Can paste your code as in what you are trying to Insert or Update? @Russelbaker1
    – d_k
    Commented Jun 27, 2016 at 14:02
  • Code already available in above question. @d_k Commented Jun 28, 2016 at 5:14
  • No I meant what are you trying to do in Execute anonymous that's what I want to see
    – d_k
    Commented Jun 28, 2016 at 7:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .