function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RajanJasujaRajanJasuja 

How to make Approval comment required

Hi,

 

I have two questions related with Approvals.

 

1. Is their a way to make Approver comments required while approving or rejecting any record?

2.  How can we make any field required on record while some one is approving or rejecting the record?

 

 

Cheers!

Rajan

Project2Project2

Validate if

1.Status=Approved || Rejected, AND

2. ISCHANGED(Commnent)

 

Returns TRUE

RajanJasujaRajanJasuja

Hi,

 

Thanks for your reply.

 

But the issue is that our custom object don't have any comment field.
The comment I am talking about is the comment which need to be entered during the approval or rejection of an record and at that time the comment save in Approval Process tables(Their are three standard tables which manage approval process internally).
So we don't have comments field on our object.
Regards,
Rajan Jasuja

   

BeeddiskShahBeeddiskShah

You can write a trigger on the approval process. Approval step will make a 'field update' that will trigger a before update trigger.

Check for the comment, if field is empty, add a addError to the object. It will stop the approval process and add a error.

 

It works beautifully.

Christopher_Alun_LewisChristopher_Alun_Lewis

BeeddiskShah is right, this is the method I adopted to make this possible.

 

For a step by step guide on how to do this, check out my blog posts:

 

Making rejection comments mandatory (Simple version, only deals with rejection comments)

 

and

 

Making all approval comments mandatory (More complex, but covers all scenarios).

 

commerinesongcommerinesong

Navigate to the field updates menu (Setup --> App Setup --> Create --> Workflows & Approvals --> Field Updates) and create a new field update worfkflow action. Set the name of the new action to "Approval Status Pending" and unique name to "Approval_Status_Pending" and object to the object you added the custom field to. Then select the Approval Status field from the resulting drop down. In the new field value options, select "A specific value" then pick "Pending" status.

ItsMeSolItsMeSol

Hi,

 

Below is the error when i tried to tetx the approval process:

 

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger RequireRejectionComment caused an unexpected exception, contact your administrator: RequireRejectionComment: execution of BeforeUpdate caused by: System.ListException: List index out of bounds: 0: Trigger.RequireRejectionComment: line 42, column 1".

 

 

 

 

 

 

 

Christopher_Alun_LewisChristopher_Alun_Lewis

Hi Sol,

 

My first thought would be that this error message is being generated by the following line

 

      "if ((pi.Steps[0].Comments == null || pi.Steps[0].Comments.trim().length() == 0))"

The error message seems to suggest that the pi.Steps collection is empty. Just a few quick questions: 

 

1) Have you followed all of the other instructions in the blog post, set up the approval processes etc. ?

2) If so, are you changing the value of the Approval_Status__c as part of any other process outside the main approval?

 

 

ItsMeSolItsMeSol

Hi Christopher, I followed all the instructions in the blog. I make a picklist field with the picklist values, Approved, Rejected and Pending. I added the 3 fields update in the Approval process then copied the code changing the custom object I use. I think that's all the process in the Blog.

Sure@DreamSure@Dream

I am also getting the same error..can some one help please? 

Benjamin Pirih.ax1481Benjamin Pirih.ax1481

The problem here is pi.Steps[0] is null is Your Rejection Process going back a step? 

 

Have spent a few hours rewriting the code trying to make this work.

 

Currently I see major issue with this solution: if your Approval Step Rejection does not go to Final Rejection Step. ( i.e. you go back a step )

-- The processInstanceStep for that rejection is not inserted within the context of the current trigger.  So you can determine if a comment had been entered and hence you always fail validation when trying to reject an approval. 

 

Here is my updated method if you want to try this.

 

	public static void validateApproval(map<Id, Invoice__c> newInvoices,  map<Id, Invoice__c> oldInvoices)
	{
		map<Id, Invoice__c> rejectedInvoices = new map<Id, Invoice__c>();
		set<Id> rejectedInvoicesWithCommentsIds = new set<Id>();

		// Build a list of rejected approval processes
		for ( Invoice__c inv : newInvoices.values() )
		{
			Invoice__c oldInvoice = oldInvoices.get( inv.Id );

			system.debug('oldInvoice.Approval_Process_Status__c: ' + oldInvoice.Approval_Process_Status__c);
			system.debug('inv.Approval_Process_Status__c: ' + inv.Approval_Process_Status__c);

			if (( oldInvoice.Approval_Process_Status__c == 'Pending' || oldInvoice.Approval_Process_Status__c == 'Approved' )
				&& (inv.Approval_Process_Status__c == 'Rejected'))
			{
				rejectedInvoices.put(inv.Id, inv);
			}
		}

		if (!rejectedInvoices.isEmpty())
		{
			map<Id, ProcessInstance> latestInstance = new map<Id, ProcessInstance>();

			for (ProcessInstance pi : [	SELECT TargetObjectId, Status, CreatedDate,
	            	                 		(
	                                 		SELECT Id, ProcessInstanceId, StepStatus, Comments, CreatedDate
	                                 		FROM Steps
	                                 		WHERE StepStatus = 'Rejected'
	                                 		ORDER BY CreatedDate DESC
	                              			)
                               			FROM ProcessInstance
                               			WHERE TargetObjectId In
                                 		:rejectedInvoices.keySet()
                               			ORDER BY CreatedDate DESC
                              		])
    		{
    			system.debug('processInstance: ' + pi);
    			system.debug('steps.size: ' + pi.Steps.Size());

    			for ( ProcessInstanceStep pis : pi.Steps)
				{
					system.debug('pis: ' + pis);
				}

    			if (!latestInstance.containsKey(pi.TargetObjectId))
    			{
    				latestInstance.put(pi.TargetObjectId, pi);
    			}
    			else
    			{
    				ProcessInstance maxInstance = latestInstance.get(pi.TargetObjectId);
    				if ( maxInstance.CreatedDate < pi.CreatedDate  )
    				{
    					latestInstance.put(pi.TargetObjectId, pi);
    				}
    			}
    		}

			system.debug('latestInstance.values(): ' + latestInstance.values());


    		// only process the latest processInstance for any invoice
    		for ( ProcessInstance pi : latestInstance.values())
    		{
    			system.debug('pi: ' + pi);

    				ProcessInstanceStep latestPis = new ProcessInstanceStep();

					// only process the latest step of any processInstance
					for ( ProcessInstanceStep pis : pi.Steps)
					{
						system.debug( 'pis: ' + pis);
						system.debug( 'latestPis: ' + latestPis);
						system.debug( 'latestPis.comments: ' + latestPis.Comments);

						if ( latestPis.Id == null)
						{
							latestPis = pis;
						}
						else
						{
							if ( latestPis.CreatedDate < pis.CreatedDate)
							{
								latestPis = pis;
							}
						}
					}

					system.debug( 'Final latestPis: ' + latestPis );
					system.debug( 'Final latestPis.comments: ' + latestPis.Comments);

		      		if ( (latestPis.Comments == null) || (latestPis.Comments.trim().length() == 0) )
		      		{
		      			rejectedInvoices.get(pi.TargetObjectId).addError(
		          		'Operation Cancelled: Please provide a rejection reason! Select back button and enter a reject comment.');
		      		}
		      		else
		      		{
		      			Invoice__c completedInvoice = rejectedInvoices.get(pi.TargetObjectId);
		      			system.debug('reseting Approval_Process_Status__c for Invoice__c.Id: ' + completedInvoice.Id);
		      			completedInvoice.Approval_Process_Status__c = 'Pending';
		      		}

			}

		}

	}

 

 

jas pjas p

I've been getting this error message,

" There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Operation Cancelled: Please provide a rejection reason!". 

Choose another list to continue.

"

 

could i get your kind guidance and help?