function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
raji devi 1raji devi 1 

Trigger to create task on contact object

I am not getting any error  on opportunity object and created task,but the same code is not working on contact
it giving error like this
exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Related To ID: id value of incorrect type: 0032800000NuHm0AAF: [WhatId]: T
Thanks in advance ,  Please give me solution for this 
trgger autocreatetask on Contact (after  insert , after update)  {
  
    list<task> tasklist = new list<task>();    
        for(contact con : trigger.new){
          task t = new task();
          t.whatid=con.id;
          t.Status = 'In Progress';
          t.Subject='call';
          t.Priority = 'High';
          t.ownerid= con.ownerid;

            tasklist.add(t);
        }
        
        insert tasklist ;     
    
    }
Best Answer chosen by raji devi 1
ManojjenaManojjena
Hi Raji,

Try with below code ,I don't think it is required for after update .
 
trgger AutoCreateTask on Contact (after  insert )  {
    list<task> tasklist = new list<task>();    
    for(contact con : trigger.new){
	  Task tsk = new Task();
	  tsk.whoId=con.id;
	  tsk.Status = 'In Progress';
	  tsk.Subject='call';
	  tsk.Priority = 'High';
	  tsk.ownerid= con.ownerid;
	  tasklist.add(tsk);
    }
	if(tasklist.isEmpty()){
	   try{
	   
	   }catch(DmlException de){
		 System.debug(de);
	   }
	}
}
Let me know if it helps!!
Thanks
Manoj
 

All Answers

Dutta SouravDutta Sourav
Hi Raji,

Replace "whatId" with "whoId" and Check.

Kind Regards,
Sourav.
nitesh gadkarinitesh gadkari
Hi Raji,

WhatId=WhatId represents nonhuman objects such as accounts, opportunities, campaigns, cases, or custom objects.
WhoId =The WhoId represents a human such as a lead or a contact

Please refer following link for more info

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_task.htm
 
ManojjenaManojjena
Hi Raji,

Try with below code ,I don't think it is required for after update .
 
trgger AutoCreateTask on Contact (after  insert )  {
    list<task> tasklist = new list<task>();    
    for(contact con : trigger.new){
	  Task tsk = new Task();
	  tsk.whoId=con.id;
	  tsk.Status = 'In Progress';
	  tsk.Subject='call';
	  tsk.Priority = 'High';
	  tsk.ownerid= con.ownerid;
	  tasklist.add(tsk);
    }
	if(tasklist.isEmpty()){
	   try{
	   
	   }catch(DmlException de){
		 System.debug(de);
	   }
	}
}
Let me know if it helps!!
Thanks
Manoj
 
This was selected as the best answer
raji devi 1raji devi 1
Hi ,
Thanks All,  Every thing is useful for me.
Regards,
Raji
 
raji devi 1raji devi 1
Hi , i bulid logic for after insert, it is working fine but unable build logic for update , give me sample code for update ..
exm : if we  are going to remove email field in contact automatically email task also remove.

trigger autocreatetask on Contact (after insert, after update) {  
      list<task> tasklist = new list<task>();
          if(trigger.isinsert && trigger.isafter){          
                for(Contact con : trigger.new){
                    //add logic for task if phone give task is created as call and  email given to cretated email task                     
                    task t = new task();
                        if(con.phone!=null){
                            t.whoid=con.id;         
                            t.ownerid= con.ownerid;
                            t.status = 'In Progress';
                            t.Priority = 'High';
                            t.subject= 'call';  
                        tasklist.add(t);
                        } 
                       if(con.email!=null){
                       task  t2 = new task();
                            t2.whoid=con.id;         
                            t2.ownerid= con.ownerid;
                            t2.status = 'In Progress';
                            t2.Priority = 'High';
                            t2.subject= 'email';                     
                        tasklist.add(t2);
                        }  
                        
                                        
                     }     
                    
                    
                  }            
            insert tasklist ;   
                 
                    
        
    }