0

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

Please help! How can I achieve it? I tried formula field but unable to get. I tried through trigger but it is not working. Please check my code and let me know what I am doing wrong.

Please find below code:

// This trigger fires on newly inserted or reparented Tasks associated with conortunities,
// and stores the conty's current functional role value in the contact   functional role field
trigger updatefunctionalrole1 on Task(before insert, before update) {
    // Create a map between the contact ID and its functional role value
    Map < ID, String > confunrole1 = new Map < ID, String > ();
    List < Task > conTasks = new List < Task > ();
    // Loop through the triggered Tasks and add all of the contact IDs (for those associated with conortunities)
    for (Task t: trigger.new) {
        // Only Tasks associated with conortunities
        if (t.whoID != null && (String.valueOf(t.whoID)).startsWith('003')) {
            // And only newly inserted Tasks or those being reparented to an contact
            if (trigger.isInsert || (trigger.isUpdate && t.WhoID != trigger.oldMap.get(t.id).WhoID)) {
                confunrole1.put(t.whoID, '');
                conTasks.add(t);
            }
        }
    }
    // Query the conortunities and add their functional role to the map
    for (contact con: [SELECT Functional_Role__c FROM contact WHERE ID IN: confunrole1.keySet()]) {
        confunrole1.put(con.id, con.Functional_Role__c);
    }
    // Update the contact functional role field on the Task with the relevant value
    for (Task t: trigger.new) {
        t.functional_role__c = confunrole1.get(t.whoID);
    }
}

Kindly suggest what to do?

11
  • It's looking fine to me. What issue you are having with this??
    – Mr.Frodo
    Commented Jun 29, 2016 at 5:41
  • It is working correctly. Commented Jun 29, 2016 at 5:42
  • Don't spot an error with a quick glance.
    – drakored
    Commented Jun 29, 2016 at 5:44
  • Originally I said something about Process Builder, but realized you're grabbing something from a polymorphic field (still can be done in PB, but requires an invocable, the choice isn't as clear in that case).
    – drakored
    Commented Jun 29, 2016 at 5:45
  • 3
    What is your issue? Looks like it should work at a quick glance. Have you written test methods that properly set up data and assert values? Basically a question of "It is not working" will not get a very detailed response. Imagine if you received a call from someone who said "Help, its not working" what would you say to the caller? Also, I do not see the purpose of conTasks. And then there is the updating of tasks t.functional_role__c that were excluded from the query of the contacts (on update if contact did not change the field would be nulled)
    – Eric
    Commented Jun 29, 2016 at 5:49

1 Answer 1

1

If you have any WFR or processes that update the record after insert due to this:

for (Task t: trigger.new) {
        t.functional_role__c = confunrole1.get(t.whoID);
    }

the field will be nulled as the value does not exist in the map due to this

if (t.whoID != null && (String.valueOf(t.whoID)).startsWith('003')) {
            // And only newly inserted Tasks or those being reparented to an contact
            if (trigger.isInsert || (trigger.isUpdate && t.WhoID != trigger.oldMap.get(t.id).WhoID)) {
                confunrole1.put(t.whoID, '');
                conTasks.add(t);
            }
        }

as the WHOID did not change and thus the map will not contain the whoID

You will have to rethink your trigger to not run on update in the same context where an insert occurred so that the resultant update does not null the field. Or at a minimum check the map for the key being present

4
  • I am getting error: **updatefunctionalrole1: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updatefunctionalrole1: line 13, column 1**@Eric Commented Jul 1, 2016 at 5:13
  • You are right map does not contain the whoID. yes I have WFR to workon after update. So how can I solve this do I need to deactivate the rule. @Eric Commented Jul 1, 2016 at 5:21
  • Hi , I tried multiple way to figure out the issue but still trigger in not fire on insert. @Eric you are right Map is not contain whoID. I am new in development so could you please help me to figure out the issue. Commented Jul 17, 2016 at 12:39
  • If you are having trouble understanding how to troubleshoot and apply what has been said to you on how to resolve then I suggest you hire a consultant /developer firm to help. Comment out the second block of code I mentioned and try again
    – Eric
    Commented Jul 17, 2016 at 15:44

You must log in to answer this question.

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