You need to sign in to do that
Don't have an account?
AAI
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();
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();
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
Please find the code below and make necessary requirements as per your lead object. Regards,
Nagendra.
Any idea about how to resolve the error?
Thanks in advance,
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);
}
}
}
I want to do same and update comment's approval process in custom object. I have refer the above code but the comment is not getting update in custom object. please help.