When I am calling future method from a trigger then I am getting error USER_DEBUG caused by: System.AsyncException: Future method cannot be called from a future or batch method: ExampleFuture.UpdateLeadFuture(List).
The task of trigger is to check if phone number on Lead object is updated then call the future method to update the description on lead object. Why I am getting this error and why the description is not getting updated?
trigger TriggerToCallFuture on Lead (before update) {
List<Id> lstupdate = new List<Id>();
if(trigger.isExecuting && trigger.isBefore && trigger.isUpdate)
{
for(Lead le : trigger.new)
{
if(le.Phone != trigger.oldmap.get(le.Id).Phone)
{
lstupdate.add(le.Id);
}
}
ExampleFuture.UpdateLeadFuture(lstupdate);
}
}
//Update lead's description in future method, when phone number is updated
public class ExampleFuture {
@future
public static void UpdateLeadFuture(List<Id> lstRecord)
{
try{
List<Lead> lstLead = [Select Id,Description from Lead where Id=:lstRecord];
List<Lead> lstUpd = new List<Lead>();
if(lstLead.size()>0)
{
for(Lead lst : lstLead)
{
lst.description = 'Updated by future method';
lstUpd.add(lst);
}
}
if(lstUpd.size()>0)
update lstUpd;
}
catch(Exception e)
{
System.debug(e.getMessage());
}
}
}