I amrying to pass comments and validate it if approver rejects then he should give some comments from before update trigger but everytime comments comes null therefore even i give some comments validation comes up..
public void fetchCommentsField(List<opportunity>lstOpp
,Map<id,opportunity> mapOldOpp,Map<id,Opportunity> mapNewMap {
Set<id> setOfOppId = new Set<id>();
for(Opportunity objOpportunity:lstOpp)
{
if(((objOpportunity.Rejected_Count__c!=mapOldOpp.get(objOpportunity.id).Rejected_Count__c)
|| (objOpportunity.Rejected_Count_for_Site_Progress__c
!=mapOldOpp.get(objOpportunity.id).Rejected_Count_for_Site_Progress__c) &&
mapOldOpp.get(objOpportunity.id).Approval_Process_for_Site_Checklist__c=='In-Progress'))
{
setOfOppId.add(objOpportunity.id);
}
}
system.debug('setOfOppId'+setOfOppId);
if(!setOfOppId.isEmpty())
{
Id idOpp ;
List<id> processInstanceIds = new List<id>();
for (Opportunity invs : [SELECT (SELECT ID
FROM ProcessInstances
ORDER BY CreatedDate DESC
LIMIT 1)
FROM Opportunity
WHERE ID IN :setOfOppId])
{
processInstanceIds.add(invs.ProcessInstances[0].Id);
idOpp = invs.id;
}
system.debug('processInstanceIds' +processInstanceIds);
List<Opportunity> oppRecord = new List<Opportunity>([Select id,Comments_From_Approver__c from
Opportunity where id=:idOpp]);
system.debug(oppRecord);
system.debug('mapNewMap' +mapNewMap);
// Now that we have the most recent process instances, we can check
// the most recent process steps for comments.
string commentStr='';
for (ProcessInstance pi : [SELECT TargetObjectId,(SELECT Id, comments FROM Steps ORDER BY CreatedDate
Desc LIMIT 1 ) FROM ProcessInstance WHERE Id IN :processInstanceIds
ORDER BY CreatedDate DESC])
{
system.debug('pi' +pi);
system.debug('pi.TargetObjectId' +pi.TargetObjectId);
system.debug('pi.Steps[0].Comments' +pi.Steps[0].Comments);
if ((pi.Steps[0].Comments == null ))
{
mapNewMap.get(pi.TargetObjectId).addError('Operation Cancelled: Please provide a rejection reason!');
}
else{
commentStr= pi.Steps[0].Comments;
system.debug('commentStr');
}
}
system.debug('oppRecord'+oppRecord);
oppRecord[0].Comments_From_Approver__c=commentStr;
system.debug('commentStr'+commentStr);
//update oppRecord;
system.debug('oppRecord' +oppRecord);
}
}