1

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);
                   }


            }
3

1 Answer 1

0

I didn't really understand the flow of your question but I understand that you want to add some comments in opportunity in before trigger.

You don't have to fetch the Opportunity record by SOQL again in trigger, you should already have it in mapNewMap.

So remove the following lines:

List<Opportunity> oppRecord = new List<Opportunity>([Select id,Comments_From_Approver__c from
    Opportunity where id=:idOpp]); 
                      system.debug(oppRecord);

Next, after you figure out which comment to add, replace the following:

system.debug('oppRecord'+oppRecord);
                     oppRecord[0].Comments_From_Approver__c=commentStr;

with

Opportunity oppRec = mapNewMap.get(idOpp);
oppRec.Comments_From_Approver__c=commentStr;
mapNewMap.put(idOpp, oppRec);

Since its a before trigger and you are making changes into the trigger variable, you dont have to manually call update DML here. I hope this will work or at least help you somehow.

UPDATE:

I did a POC with your code. When the trigger is called from a field update after approval or rejection, you won't get the ProcessInstanceStep record in the trigger. Reason being - the ProcessInstanceStep record gets inserted after the field update is done i.e triggers are run.

Now to achieve this one option could be to have a Batch called after a minute of the trigger. The ProcessInstanceStep record would get inserted by then so the batch will have the comments.

8
  • Thanks ,but i am not able to get approver comments in my processinstance query when appprover rejects it with some comments ,every time i am getting the custom error . comments in step is always null..
    – Raju Sen
    Commented Sep 26, 2016 at 10:14
  • Okay, I think the reason is that you are accessing the first item of Steps list. The first one is step while submitting the record for approval, next one is the approved or rejected comments. So try accessing the Steps[1] in commentStr= pi.Steps[0].Comments; this line. Also add LIMIT 2 in the query. Commented Sep 26, 2016 at 12:03
  • First step in descending order so it should give me the latest step
    – Raju Sen
    Commented Sep 26, 2016 at 17:42
  • 1
    Right, stupid me! When is this Account trigger called? I mean approving or rejecting a record calls related object's trigger? Or you have some approval actions on the Approval process? Commented Sep 27, 2016 at 7:01
  • 1
    Can you move the field update to Final approval action of the approval process? That may solve the issue of not having the comments in trigger. Commented Sep 29, 2016 at 7:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .