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
econ242econ242 

Lead Conversion Error after Trigger Field Update

Hi...being new to triggers and APEX, I'm in need of some help on an error I received...

I currently have a trigger that automatically updates a Lead Status and Lead Custon Field (Stage_C) based on the completion of an Activity. Below is my code for the tigger...which, works perfect:

trigger changeLeadStatustask2 on Task (before insert, before update) {
    List<Lead> LeadToUpdate = new List<Lead>();
        for (Task tsk: Trigger.new){
            if(tsk.Status=='Completed' && tsk.Subject=='Call to Qualify (Successful)'){
                Lead ld = new Lead(Id=tsk.whoid);
                   ld.Status = 'Qualified';             
                     ld.Stage__c = 'Qualified - Convert to QP';
                LeadToUpdate.add(ld);           
            }
           
        }       
        update LeadToUpdate;
    }

However...when I attempt to convert this Lead into an Account, I get the below error/exception:

Error: System.DmlException: Update failed. First exception on row 0 with id 00TR000000EdoDYMAZ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, changeLeadStatustask2: execution of BeforeUpdate caused by: System.TypeException: Invalid id value for this SObject type: 003R000000ojtF5IAI Trigger.changeLeadStatustask2: line 5, column 1: [] (System Code)

Any help would be most appreciated!!! Thank you!!!

Best Answer chosen by econ242
Pavan Kumar KajaPavan Kumar Kaja
Hi,

Try below code.

The change i have made is
1. Checking for Whoid is not equal to null , lead
2. Error handling try, catch

I have tested the below code is working. let m eknow if you have any questions.
trigger changeLeadStatustask2 on Task (before insert, before update) {
    List<Lead> LeadToUpdate = new List<Lead>();
        for (Task tsk: Trigger.new){
            if(tsk.Status=='Completed' && tsk.Subject=='Call to Qualify (Successful)' &&  tsk.WhoId != null && String.valueOf(tsk.WhoId).startsWith(Schema.SObjectType.Lead.getKeyPrefix())){
                Lead ld = new Lead(Id=tsk.whoid);
                   ld.Status = 'Qualified';             
                   ld.Stage__c = 'Qualified - Convert to QP';
                LeadToUpdate.add(ld);           
            }
           
        }       
		if(!LeadToUpdate.isEmpty()){
			try{
				update LeadToUpdate;
			}
			catch(DMLException e){
				for(Task tsk: Trigger.new){
					tsk.addError(e.getDmlMessage(0));
				}
			}
		}
}


All Answers

Pavan Kumar KajaPavan Kumar Kaja
Hi,

Try below code.

The change i have made is
1. Checking for Whoid is not equal to null , lead
2. Error handling try, catch

I have tested the below code is working. let m eknow if you have any questions.
trigger changeLeadStatustask2 on Task (before insert, before update) {
    List<Lead> LeadToUpdate = new List<Lead>();
        for (Task tsk: Trigger.new){
            if(tsk.Status=='Completed' && tsk.Subject=='Call to Qualify (Successful)' &&  tsk.WhoId != null && String.valueOf(tsk.WhoId).startsWith(Schema.SObjectType.Lead.getKeyPrefix())){
                Lead ld = new Lead(Id=tsk.whoid);
                   ld.Status = 'Qualified';             
                   ld.Stage__c = 'Qualified - Convert to QP';
                LeadToUpdate.add(ld);           
            }
           
        }       
		if(!LeadToUpdate.isEmpty()){
			try{
				update LeadToUpdate;
			}
			catch(DMLException e){
				for(Task tsk: Trigger.new){
					tsk.addError(e.getDmlMessage(0));
				}
			}
		}
}


This was selected as the best answer
econ242econ242
Thank you Ashi!!!

Yes...worked perfectly!!! I have a few Triggers built for the same purpose but with different Statuses and Subjects...I modified them all with your code and was able to convert.

Are you able to easily explain what was preventing the conversion? I still need to read up on try, catch exceptions...just a lot to wrap my head around...lol!!!

Thanks again...I really appreciate your help!!!  :)

E