Untitled

 avatar
unknown
plain_text
20 days ago
1.6 kB
4
Indexable
public class INV_LaserRequestApprovalHelper {
    public static void makeCommentMandatory(List<Laser_Request__c> listLaserRequest, Map<Id, Laser_Request__c> oldMapLaserRequest) {
        List<Id> laserRequestIds = new List<Id>();

        for (Laser_Request__c lr : listLaserRequest) {
            if (lr.Status__c == 'Rejected' && lr.Status__c != oldMapLaserRequest.get(lr.Id).Status__c) {
                laserRequestIds.add(lr.Id);
            }
        }

        if (!laserRequestIds.isEmpty()) {
            List<ProcessInstance> processInstances = [
                SELECT Id, TargetObjectId, 
                       (SELECT Id, Comments FROM Steps ORDER BY CreatedDate DESC LIMIT 1) 
                FROM ProcessInstance 
                WHERE TargetObjectId IN :laserRequestIds
            ];


            Map<Id, Boolean> approvalCommentMap = new Map<Id, Boolean>();

            for (ProcessInstance pi : processInstances) {
                Boolean hasComment = false;
                for (ProcessInstanceStep pis : pi.Steps) {
                    if (pis.Comments != null && pis.Comments.trim() != '') {
                        hasComment = true;
                        break;
                    }
                }
                approvalCommentMap.put(pi.TargetObjectId, hasComment);
            }

            for (Laser_Request__c lr : listLaserRequest) {
                if (laserRequestIds.contains(lr.Id) && (!approvalCommentMap.containsKey(lr.Id) || !approvalCommentMap.get(lr.Id))) {
                    lr.addError('Comment is mandatory when rejecting.');
                }
            }
        }
    }
}
Editor is loading...
Leave a Comment