Help with the below Business rule

Pooja Khatri
Tera Contributor

Hi All , 

 

I am using the below script in one my function in my Business rule , the entire script is written in the function 

 

function change(){
if(task == 'change_request'){
 var changeSysId = current.document_id ;
var assignedto = current.approver.sys_id.toString();
var CR = new GlideAggregate('change_request');
 CR.addEncodedQuery("type=Normal^sys_id=" + changeSysId + "^assigned_to=" + assignedto+"^ORrequested_by="+assignedto);
CR.addAggregate('COUNT');
CR.query();
var count = 0;
 if(CR.next()){
count = CR.getAggregate('COUNT');
 }
 if(count>0) {
 return false ;
}
else {
 return true ;
 }
  }
  else {
 return false ;
 }}
 
and I am callling the function in the below way 
 
var isNormal = change();
 
I want to now want to trigger an event == approve.insert 
 
I want to utilize function return to trigger or not trigger the approval.insert event , if the conditions in my function are true it should trigger event approve.insert
 
I wan to to build the logic for above statement in the below code : 
 
if (current.state.changes() && current.state == 'requested') {
    var event = "approve.insert";
    if (req)
        event = "req.approve.insert";
    else if (tsk)
        event = "tsk.approve.insert";

    gs.eventQueue(event, current, gs.getUserID(), gs.getUserName());
    var cmt = getComment(isFD, getApproverUserName(current.approver), "Approve");
    updateTask(current, cmt);
}
 
Can anyone help me to fix this logic ?
5 REPLIES 5

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @Pooja Khatri ,

                                         Try Below fix

if (current.state.changes() && current.state == 'requested') {
  // Get the event name to trigger.
  var event = "approve.insert";
  if (req) {
    event = "req.approve.insert";
  } else if (tsk) {
    event = "tsk.approve.insert";
  }

  // Get the function return value.
  var isNormal = change();

  // Trigger the event only if the function returns true.
  if (isNormal) {
    gs.eventQueue(event, current, gs.getUserID(), gs.getUserName());
  }

  // Update the task with the comment.
  var cmt = getComment(isFD, getApproverUserName(current.approver), "Approve");
  updateTask(current, cmt);
}

Kindly mark correct and helpful if applicable

 

Hi @Chetan Mahajan - with the above script will it only call event = approve.insert for is normal function and also will it not affect the other events and other conditions specified in the code ?

 

Hello Pooja,

                  The event name will vary depending on the type of record being approved (e.g., request, task, etc.) if those not found it will call event = approve.insert. 

@Chetan Mahajan I tried with the given fix , but its not working