1

Pardot creates some Tasks for agents to contact a potential customer but the marketing team is saying they cannot set the Related To field to be the Account for the Contact. So I tried setting the Account via a trigger but not getting it to work.

I tried:

trigger TestTaskTrigger on Task (before insert) {
  for (Task t : trigger.new){ 
    String c = t.Who.Name;
    Id a = [select AccountId from contact where name = :c];
    t.What = a;
 } 
}

This just generates an error of Illegal assignment from List to Id

Looking for suggestions.

3
  • Shouldn't it be t.WhatId? You also need to move that query outside the loop
    – EricSSH
    Apr 1, 2016 at 21:16
  • Also this sounds like a Pardot issue with the connector and you shouldn't have to do all of this work
    – EricSSH
    Apr 1, 2016 at 21:51
  • I have not looked at Pardot I was advised by the people doing that side that Pardot can not set the Account on the Task. That could be possible and I could have them investigate that further if you think it should be able to do that
    – Jason
    Apr 1, 2016 at 21:53

1 Answer 1

1

Revised Answer:

Heres what I would do if I were you

Trigger

trigger TestTaskTrigger on Task (before insert) {
       if(trigger.isInsert && trigger.isBefore){
            Task_Methods.setWhat(trigger.new);
        }
}

Class

public class Task_Methods{
    public static void setWhat(List<Task> newList){
        List<Task> tList = new List<Task>();
        Map<Id,Contact> contactMap = new Map<Id,Contact>([SELECT Id, AccountId FROM Contact WHERE AccountId != null]);

        for(Task t : newList){
            if(contactMap.get(t.WhoId) != null){
                 t.WhatId = contactMap.get(t.WhoId).AccountId;
            }
        }
    }
}
7
  • tried this and get List has no rows for assignment to SObject when trying to create a new task with a contact on it of course
    – Jason
    Apr 1, 2016 at 21:26
  • You're not going to get any results for the t.who.name within your trigger. I would recommend sending trigger.new to a helper class and then querying the who.name value and setting the WhatId that way. You're also going to need to determine if the who is a Lead or a Contact. I believe there is a WhoType field you can use for that. Apr 1, 2016 at 21:28
  • I will look at creating a solution using these suggestions. TY
    – Jason
    Apr 1, 2016 at 21:39
  • Wow this revised answer looks excellent. I will try that out
    – Jason
    Apr 1, 2016 at 21:43
  • Error: No such column 'AccountId' on entity 'Name'
    – Jason
    Apr 1, 2016 at 21:45

You must log in to answer this question.

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