+ Start a Discussion
Rahul Sangwan7341Rahul Sangwan7341 

I am using bcc to salesforce, and i have to create a new task when mail comes with specified subject

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;
        }
    }

}


 
Best Answer chosen by Rahul Sangwan7341
Swayam  AroraSwayam Arora
Please update your code to:-
 
trigger bccTrigger on Task (after insert) {
	List<Task> tnew=trigger.new;
	List<Task> tk=new List<Task>();
	for(Task t:tnew){	
		if((t.Subject).equals('Email: GroupBy / Searchandiser Introduction')){
			Task tadd=new Task();
			tadd.Subject='Test2';
			tadd.WhoId=t.WhoId;
			tk.add(tadd);
		}
	}
	insert tk;
}

I have tested, it is working fine.