+ Start a Discussion
AAIAAI 

Capture an approval comments to populate a lead custom field

Hi all,
I need to capture an approval comments to populate a lead custom field when the lead status = unqualified and the step status = rejected. I have an error compilation like "Error: Compile Error: Variable does not exist: Lead at line 14 column 109"
Here is my code. Any help?
trigger Unqualified on Lead (before update) {
//Get all approval process records from an approval process definition. ProcessDefinitionId = The ID of this approval process instance.TargetObjectId = ID of the object affected by this approval process instance.//
List<ProcessInstance> instances = [SELECT Id,TargetObjectId,(SELECT Id, StepStatus, Comments FROM Steps) FROM ProcessInstance Where ProcessDefinitionId  = '[ TargetObjectId]'];

//guarda la información de los leads. Declara e inicializa. List (nombre del objeto)//
List<Lead> Leads = New List <Lead>(); 

//Create a set of object Ids which has process instance//
    for(ProcessInstance pi:instances){
        LeadIds.add(pi.TargetobjectId);
    }
 
//Query for related records//
Map<Id,Lead> LeadMap = new Map<Id,Lead>([Select ReasonRejectedByCommercialPlanning__c from Lead Where Id in:Lead]);

//populate object's reason rejected field from approval comments
if (l.status == 'Unqualified')
    if (l.recordtypeId == '0121A000000QeSxQAK')
    for(ProcessInstance pi:instances){
       for (ProcessInstanceStep step : pi.Steps) {
         if(step.Status == 'Rejected') {
            LeadMap.get(pi.TargetObjectId).ReasonRejectedByCommercialPlanning__c = step.Comments;
         }
       }
    }

//Update your object//
update LeadMap.values();
Best Answer chosen by AAI
AAIAAI
Hi,
Here it´s my code. It works perfectly.

trigger Unqualified on Lead (before update ) {
System.debug ('**entro en el trigger before update'); // es un comentario para verlo en el log//

List<Lead> Leads = New List <Lead>();// guarda la información de los leads. Declara e inicializa. List (nombre del objeto) //
List<Id> Ids = New List <Id>();
for (Lead l: Trigger.new){
    Ids.add (l.Id);
}
//Se obtienen los Id de los procesos de aprobación y de los Leads//
List<ProcessInstance> instances = [select Id, TargetObjectId from ProcessInstance where TargetObjectId in :Ids];
Map<Id,Id> LeadProcessMap = new Map<Id,Id>();
Ids = New List <Id>();
for(ProcessInstance pi:instances){
        LeadProcessMap.put (pi.TargetObjectId,pi.Id); 
        Ids.add (pi.Id);// se guardan los Ids de los process instances para que los coja la segunda query//
    }//Con el for se pobla el mapa compuesto por el Id del Lead y del Process Instance//
System.Debug ('** mapa1: '+LeadProcessMap);
    
List<ProcessInstanceStep> instancesSteps = [select Comments,ProcessInstanceId from ProcessInstanceStep where ProcessInstanceId in :Ids];
Map<Id,String> LeadProcessStepMap = new Map<Id,String>(); // String porque hace referencia a comentarios//
for(ProcessInstanceStep pis:instancesSteps){
        LeadProcessStepMap.put (pis.ProcessInstanceId, pis.Comments); 
    }//Con el for se pobla el mapa compuesto por el Id del Lead y del Process Instance//

System.Debug ('** mapa2: '+LeadProcessStepMap);

 //recorre todos los leads//   
for (Lead l: Trigger.new){
    if (l.status == 'Unqualified')
        if (l.recordtypeId == '0121A000000QeSxQAK'){
        System.debug ('** razon2: '+LeadProcessStepMap.get(LeadProcessMap.get(l.Id)));
            l.ReasonRejectedByCommercialPlanning__c = LeadProcessStepMap.get(LeadProcessMap.get(l.Id));
        System.debug ('** razon: '+l.ReasonRejectedByCommercialPlanning__c);
        }


       
}

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Amaya,

Please find the code below and make necessary requirements as per your lead object.
//Get all approval process records from an approval process definition
List<ProcessInstance> instances = [SELECT Id,TargetObjectId,(SELECT Id, StepStatus, Comments FROM Steps) FROM ProcessInstance Where ProcessDefinitionId = '[Your process definition Id]'];


 Set<String> objectIds = new Set<String>();

//Create a set of object Ids which has process instance
    for(ProcessInstance pi:instances){
        objectIds.add(pi.TargetobjectId);
    }
 
//Query for related records
Map<Id,YOUR_OBJECT__C> yourObjectMap = new Map<Id,YOUR_OBJECT__C>([Select Your_Comments_Field__c from YOUR_OBJECT__C Where Id in:objectIds ]);

//populate object's comment field from approval comments
    for(ProcessInstance pi:instances){
       for (ProcessInstanceStep step : pi.Steps) {
         if(step.Status == 'Approved') {
            yourObjectMap.get(pi.TargetObjectId).Your_Comments_Field__c = step.Comments;
         }
       }
    }

//Update your object
update yourObjectMap.values();
Regards,
Nagendra.
 
AAIAAI
Thanks. It is the same which I used before. It gave me a compilation error as I wrote in my message.

Any idea about how to resolve the error?

Thanks in advance,
AAIAAI
Hi,
Here it´s my code. It works perfectly.

trigger Unqualified on Lead (before update ) {
System.debug ('**entro en el trigger before update'); // es un comentario para verlo en el log//

List<Lead> Leads = New List <Lead>();// guarda la información de los leads. Declara e inicializa. List (nombre del objeto) //
List<Id> Ids = New List <Id>();
for (Lead l: Trigger.new){
    Ids.add (l.Id);
}
//Se obtienen los Id de los procesos de aprobación y de los Leads//
List<ProcessInstance> instances = [select Id, TargetObjectId from ProcessInstance where TargetObjectId in :Ids];
Map<Id,Id> LeadProcessMap = new Map<Id,Id>();
Ids = New List <Id>();
for(ProcessInstance pi:instances){
        LeadProcessMap.put (pi.TargetObjectId,pi.Id); 
        Ids.add (pi.Id);// se guardan los Ids de los process instances para que los coja la segunda query//
    }//Con el for se pobla el mapa compuesto por el Id del Lead y del Process Instance//
System.Debug ('** mapa1: '+LeadProcessMap);
    
List<ProcessInstanceStep> instancesSteps = [select Comments,ProcessInstanceId from ProcessInstanceStep where ProcessInstanceId in :Ids];
Map<Id,String> LeadProcessStepMap = new Map<Id,String>(); // String porque hace referencia a comentarios//
for(ProcessInstanceStep pis:instancesSteps){
        LeadProcessStepMap.put (pis.ProcessInstanceId, pis.Comments); 
    }//Con el for se pobla el mapa compuesto por el Id del Lead y del Process Instance//

System.Debug ('** mapa2: '+LeadProcessStepMap);

 //recorre todos los leads//   
for (Lead l: Trigger.new){
    if (l.status == 'Unqualified')
        if (l.recordtypeId == '0121A000000QeSxQAK'){
        System.debug ('** razon2: '+LeadProcessStepMap.get(LeadProcessMap.get(l.Id)));
            l.ReasonRejectedByCommercialPlanning__c = LeadProcessStepMap.get(LeadProcessMap.get(l.Id));
        System.debug ('** razon: '+l.ReasonRejectedByCommercialPlanning__c);
        }


       
}
This was selected as the best answer
Todd KadasTodd Kadas
Tried to modify this code for an opportunity approval process I have but didn't work for me.  Ran debug log and found variable assignment list of size too large to display messages.  Not sure how to resolve