1

enter image description hereI am using bcc to salesforce, and i have to create a new task when mail comes with specified subject. I had written a trigger on Task. In case when i am sending bcc to lead its working fine but in case of Contact whoId is always coming as null, i tried with both after insert and after update.

This is trigger I had written:

trigger bccTrigger on Task (after insert, after update) {

    //TaskTriggerHelper.createNewTask(Trigger.new);
    List<Task> tnew=trigger.new;
    List<Task> tc=[Select Id,Subject,Dummy_Number__c,WhoId from Task where id in:tnew];
    List<Task> tk=new List<Task>();
    If(Trigger.isInsert){
        if(Trigger.isAfter){
        for(Task t:tc){
        system.debug('............Insert.......'+t.WhoId+'.....'+t.Id);
            if((t.Subject).equals('Email: GroupBy / Searchandiser Introduction')){
                t.Dummy_Number__c=t.Dummy_Number__c+1;
                }
            }
            update tc;
        }
    }

    if(Trigger.isUpdate){
        if(Trigger.isAfter){
            for(Task t:tc){
            system.debug('............Update.......'+t.WhoId+'.....'+t.Id);
            if((t.Subject).equals('Email: GroupBy / Searchandiser Introduction')){
                Task tadd=new Task();
                tadd.Subject='Test1';
                tadd.WhoId=t.WhoId;
                tk.add(tadd);
                }
            }
            insert tk;
        }
    }

}
2
  • I'm a bit confused by your question. Are you (1) sending the email as part of a Task or (2) is the email coming from a customer and you're creating a Task to follow-up? (3) Are you perhaps sending the email from a non SF email address to a customer with a BCC to a SF email address and trying to use a trigger to create a Task from the email? If 1 or 2, do you have an inbound email handler? If not, what does the BCC have to do with it?
    – crmprogdev
    Jan 22, 2015 at 14:40
  • @crmprogdev I am using standard salesforce functionality BCC to Salesforce and i have integrated gmail in my instance. So when i am clicking the gmail button on email field in Contact that will open my gmail with Email to : Contact Email and in bcc: Salesforce generated email address. Now when mail is send a task is created, now i am trying to create new task on that event. Issue is task is created but whoId is not assigned as it is coming as null in trigger Jan 22, 2015 at 15:34

1 Answer 1

0

I rewrote your trigger for you with the thought that your query is unnecessary. Off hand, it appears to me as though all of what you're looking for should be contained in trigger.new. It appears to me that the Who.Id, Subject and other fields should all be contained in Trigger.new, so I see no need to query for them unless you know something that I don't.

Also, the first thing that immediately struck me is that you were running your query using a list<Task> to get the Id's in your Where clause, not a list<Id> based only on the Id's in Trigger.new. That may be the entire source of the problem you're having. I see no debug statement to check the length of the list your query returned. It may not have been returning anything at all.

Update

Here's my edited code for you that has no queries:

trigger bccTrigger on Task (after insert, after update) {

    //TaskTriggerHelper.createNewTask(Trigger.new);
    // List<Task> tnew=trigger.new;
    List<Id> tId = Trigger.NewMap.keyset();
    List<Id> ErrLst = new List<Id>;

    // List<Task> tc=[Select Id,Subject,Dummy_Number__c,WhoId from Task where id in:tnew];
    List<Task> tc;

    For(Id i: tId){
        Task t1 = new Task(Id = i, Subject = Trigger.NewMap.get(i).Subject, Dummy_Number__c = Trigger.NewMap.get(i).Dummy_Number__c, WhoId = Trigger.NewMap.get(i).WhoId);
        system.debug('t1.WhoId = '+t1.WhoId+ 't1.Id = ' +t.Id);
        if(t1.WhoId == null){
           Errlst.add(t1.Id); // will now have a list of all tasks w/o WhoIds
        {
        tc.add(t1);
    }       

    List<Task> tk1=new List<Task>();
    List<Task> tk2=new List<Task>();
    If(Trigger.isAfter && Trigger.isInsert) {
        for(Task t:tc){

            if((t.Subject).equals('Email: GroupBy / Searchandiser Introduction')){
               t.Dummy_Number__c=t.Dummy_Number__c+1;               
                // was one of the above supposed to be tc.Dummy_number__c ??
            }
            tk1.add(t); 
            // all changes to var t would be lost if not saved to a new list
        }
        update tk1;
    }

    if(Trigger.isAfter && Trigger.isUpdate){
        for(Task t:tc){
            if((t.Subject).equals('Email: GroupBy / Searchandiser Introduction')){
                Task tadd=new Task();
                tadd.Subject='Test1';
                if(t.WhoId != null) tadd.WhoId = t.WhoId;
            }
            tk2.add(tadd);
        }
        insert tk2;
    }
}

Note: The above code has not been tested. You'll need to test and debug to verify it works to to meet your needs.

7
  • i tried this earlier but its not working........i tried with debug log i am getting all values of task only who id is coming as null everytime in case of contact i am getting correctly in case of lead. Jan 27, 2015 at 4:32
  • If debug statements are telling you the WhoId isn't in trigger.new, it sounds as though you need to take a close look at the source emails being sent to SF to make certain the WhoId is being included in the same manner it was in the lead emails. If the data isn't there, its not being picked up by the email handler and you need to figure out why.
    – crmprogdev
    Jan 27, 2015 at 14:19
  • From the docs: "If Salesforce can’t match any email addresses for recipients that appear in the To and Cc fields of emails you add to Salesforce, the My Unresolved Items page displays one row for each email." May want to test for this if WhoId is null or empty. Also suggest you look at some of the config options mentioned in the link.
    – crmprogdev
    Jan 27, 2015 at 16:33
  • i checked all the things and when i am creating task via bcc to salesforce.........i am able to see whoId in salesforce record and also if i seeing from query editor........i don't know why it is coming as null in trigger time. Jan 29, 2015 at 4:17
  • Then the problem MUST be in your code. I've revised code above to add system.debug and create an ErrLst of Task IDs when contents of trigger.new is assembled into the list used for all sections of your code. Suggest you try it to properly debug your code. If any problems, may need to insert a try-catch for null pointer exceptions when testing for WhoId==null, but I don't expect you to need to. Can add a For loop to output contents of ErrLst if it's not empty or send to a utility class to email results to yourself.
    – crmprogdev
    Jan 29, 2015 at 16:59

You must log in to answer this question.

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