+ Start a Discussion
shweta raghav 13shweta raghav 13 

Trigger to update ownerID field from the text field.

getting error:Apex trigger TaskCreate caused an unexpected exception, contact your administrator: TaskCreate: execution of AfterInsert caused by: System.StringException: Invalid id: ABC: Trigger.TaskCreate: line 15, column 1

trigger TaskCreate on Task (after insert, after update){
    if(trigger.isAfter){
        if(trigger.isInsert || trigger.isUpdate){
            List<Task>Tasklist=new List<Task>();
            for(Task ts : trigger.new){
                if(ts.Status == 'Completed' ){
                    system.debug('ts.Status------'+ts.Status);
                    system.debug('tss------'+ts);
                    Task td = new task();
                    td.WhoId=ts.WhoId;
                    td.Status='Not Started';
                    td.Subject=ts.Next_Step__c;
                    td.ActivityDate=ts.Next_Step_Due_Date__c;
                    td.Inside_Sales_Reps__c=ts.Inside_Sales_Reps__c;      
                    td.OwnerId=ts.Next_Step_Owner__c;            
                    Tasklist.add(td);
                    system.debug('Tasklist------'+Tasklist);
                } 
            }if(Tasklist.size()>0){
                try{
                    insert Tasklist;
                }
                catch(exception e){
                    system.debug('---e---'+e);
                }
            }            
        }
    }    
}
HARSHIL U PARIKHHARSHIL U PARIKH
HI Shweta,

I would say you need to make a use of Id.ValueOf() function since it is not recgonizing String as an ID.

Try using the following trigger,
 
Trigger TaskCreate on Task (after insert, after update)
{
    If(Trigger.IsAfter)
    {
        If(trigger.isInsert || Trigger.isUpdate)
        {
            List<Task>Tasklist=new List<Task>();
            
            for(Task ts : trigger.new)
            {
                if(ts.Status == 'Completed' )
                {
                    system.debug('ts.Status------'+ts.Status);
                    system.debug('tss------'+ts);
                    
                    Task td = new task();
                    
                    td.WhoId=ts.WhoId;
                    td.Status='Not Started';
                    td.Subject=ts.Next_Step__c;
                    td.ActivityDate=ts.Next_Step_Due_Date__c;
                    td.Inside_Sales_Reps__c=ts.Inside_Sales_Reps__c;      
                    td.OwnerId = Id.ValueOf(ts.Next_Step_Owner__c);            
                    Tasklist.add(td);
                    system.debug('Tasklist------'+Tasklist);
                } 
            }if(Tasklist.size()>0){
                try{
                    insert Tasklist;
                }
                catch(exception e){
                    system.debug('---e---'+e);
                }
            }            
        }
    }    
}

Hope this helps and if it solves the question then please mark it as best answer!
PawanKumarPawanKumar
Hi Shwetha,
if  field Next_Step_Owner__c is storing user name then you will have to change your OwnerId setting as below.

td.OwnerId=[Select Id from User where Name =:ts.Next_Step_Owner__c].Id;

in the above query, i am assuming you are storing name(Not firstName or Last Name)
If you store only first name then change above query criteria to firstName/LastName.

Regards,
Pawan Kumar
HARSHIL U PARIKHHARSHIL U PARIKH
I would agree with the Pawan Kumar here though... by looking the field API name it looks like it is storing a name and not the ID.

I hope you find the below trigger helpful,
 
Trigger TaskCreate on Task (after insert, after update)
{
    If(Trigger.IsAfter)
    {
        If(trigger.isInsert || Trigger.isUpdate)
        {
            List<Task>Tasklist=new List<Task>();
            
            for(Task ts : trigger.new)
            {
                if(ts.Status == 'Completed' )
                {
                    system.debug('ts.Status------'+ts.Status);
                    system.debug('tss------'+ts);
                    
                    Task td = new task();
                    
                    td.WhoId=ts.WhoId;
                    td.Status='Not Started';
                    td.Subject=ts.Next_Step__c;
                    td.ActivityDate=ts.Next_Step_Due_Date__c;
                    td.Inside_Sales_Reps__c=ts.Inside_Sales_Reps__c; 
                   
                    td.OwnerId=[Select Id from User where Name =:ts.Next_Step_Owner__c].Id;        
                                             
                               
                    Tasklist.add(td);
                    system.debug('Tasklist------'+Tasklist);
                } 
            }if(Tasklist.size()>0){
                try{
                    insert Tasklist;
                }
                catch(exception e){
                    system.debug('---e---'+e);
                }
            }            
        }
    }    
}

 
shweta raghav 13shweta raghav 13
i have tried above code but it gives error :Apex trigger TaskCreate caused an unexpected exception, contact your administrator: TaskCreate: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.TaskCreate: line 23, column 1