Workflow to get a manager approval when a INC got created , Add a RunScript to update INC notes

somadileep840
Giga Contributor

Hi All,
I had an Assignment and required some help with code here .
Task:
Create a Workflow to get manager approval when incident got created  and Add Runscript to Update Incident notes based on Approval state

var incGR =new GlideRecord('incident');

incGR.get(current.sys_id);

var appGR = new GlideRecord('sysapproval_approver');
appGR.addQuery('sysapproval',current.sysapproval) ;

appGR.query();

if(appGR.next()){
    var appState = appGR.state.toString();
    var approver = appGR.approver.getDisplayValue();
    //gs.log("1st stage log");
    gs.info("incident update script :processing approval record"+appGR.sys_id+"for incident "+incGR.sys_id);

var comment ="" ;
    if(appState == 'approved'){
      comment="Approved by "+ approver;
       gs.log("2nd stage log);
               
    }
        else if(appState == 'rejected'){
        comment ="Rejected by "+approver;
        gs.log("3rd stage");        
        }
        else{
       comment="Approval state : "+appState+" by " +approver;
            
        }
   
       if(comment!=""){}
        incGR.work_notes=comment;
        incGR.comments_and_work_notes=comment;
         gs.log("4th stage");
        incGR.update();
      
             }else{
            gs.logError("Approval record not found for sys approval "+current.sysapproval);
                    incGR.work_notes="Error  : Could not retrieve information";
              incGR.update();
        }
}

I was stuck here ,its not getting into loop. Only Log1 and Log 4 showing and no comments were added to Incident .Kindly help me .
Thanks in Advance


2 REPLIES 2

Viraj Hudlikar
Giga Sage

Hello @somadileep840 

There's a missing closing quote in the gs.log("2nd stage log); line. It should be gs.log("2nd stage log");

The condition if(comment!=""){} is empty and doesn't do anything. You should remove the curly braces {}.

Ensure that current.sysapproval has a valid value. If it's null or empty, the query won't return any records.

 

var incGR = new GlideRecord('incident');
incGR.get(current.sys_id);

var appGR = new GlideRecord('sysapproval_approver');
appGR.addQuery('sysapproval', current.sysapproval);
appGR.query();

if (appGR.next()) {
    var appState = appGR.state.toString();
    var approver = appGR.approver.getDisplayValue();
    gs.info("incident update script: processing approval record " + appGR.sys_id + " for incident " + incGR.sys_id);

    var comment = "";
    if (appState == 'approved') {
        comment = "Approved by " + approver;
        gs.log("2nd stage log");
    } else if (appState == 'rejected') {
        comment = "Rejected by " + approver;
        gs.log("3rd stage log");
    } else {
        comment = "Approval state: " + appState + " by " + approver;
    }

    if (comment != "") {
        incGR.work_notes = comment;
        incGR.comments_and_work_notes = comment;
        gs.log("4th stage log");
        incGR.update();
    }
} else {
    gs.logError("Approval record not found for sys approval " + current.sysapproval);
    incGR.work_notes = "Error: Could not retrieve information";
    incGR.update();
}

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Ankur Bawiskar
Tera Patron
Tera Patron

@somadileep840 

if your workflow is on incident table then why to query again and cause performance impact

try this

var appGR = new GlideRecord('sysapproval_approver');
appGR.addQuery('sysapproval', current.sys_id);
appGR.query();

if (appGR.next()) {
    var appState = appGR.state.toString();
    var approver = appGR.approver.getDisplayValue();
    gs.info("incident update script: processing approval record " + appGR.sys_id + " for incident " + incGR.sys_id);

    var comment = "";
    if (appState == 'approved') {
        comment = "Approved by " + approver;
        gs.info("2nd stage log");
    } else if (appState == 'rejected') {
        comment = "Rejected by " + approver;
        gs.info("3rd stage log");
    } else {
        comment = "Approval state: " + appState + " by " + approver;
    }

    if (comment != "") {
        current.work_notes = comment;
        current.comments_and_work_notes = comment;
        gs.info("4th stage log");
        current.update();
    }
} else {
    gs.info("Approval record not found for sysapproval " + current.number);
    current.work_notes = "Error: Could not retrieve information";
    current.update();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader