How to set approval status based on all child records in PM Approval Record using script

In the PM Approval record, we have to set the approval status as ‘Approved’ when all the child records approval status as ‘Approved’.

    let newRecord = scriptContext.newRecord;               

if (scriptContext.type == ‘xedit’ || scriptContext.type == ‘edit’) {                   

let approvalStatus = newRecord.getValue({ fieldId: ‘custrecord_jj_approval_status’ });                   

log.debug(“approval status”, approvalStatus);                   

let recordID = newRecord.getValue({ fieldId: ‘id’ });                   

log.debug(“record id”, recordID)                   

if (checkForParameter(approvalStatus) && checkForParameter(recordID)) {                       

//get the parent record internal id                       

let parentID = parentRecordID(recordID);                       

//get the all child records approval status                       

let statusResult = parentRecordSearch(parentID);                       

log.debug(“status result”, statusResult);                       

if (statusResult == true) {                           

//set the approval status as ‘Approved’.                           

record.submitFields({                               

type: ‘customrecord_jj_pm_approval’,                               

id: parentID,                               

values: {                                   

custrecord_jj_approval_status_pm: approvedStatus                               

}                           

});                       

}                       

else {                           

//set the approval status as ‘Pending Approval’                         

  record.submitFields({                             

  type: ‘customrecord_jj_pm_approval’,                               

id: parentID,                               

values: {                                   

custrecord_jj_approval_status_pm: pendingStatus                           

    }                           

});                       

}                   

}               

}

Leave a comment

Your email address will not be published. Required fields are marked *