Calling Script include from Ui action workspace script FALLING

Prudhvi Raj Y
Tera Expert

Hi Team, 

 

I'm writing the script to call the script include from ui action workspace script but script include is not getting called, I'm trying to cancel the request and child tasks associated to the parent request. Below is the request I'm trying.

 UI Action:

function onClick(g_form) {
 var fields = [{
    type: 'textarea',
    name: 'cancel_reason',
    label: getMessage('Cancellation reason'),
    mandatory: true
  }];

  g_modal.showFields({
    title: getMessage('Cancel request'),
    fields: fields,
    instruction: getMessage("This action can't be undone."),
    size: 'md',
    confirmTitle: getMessage('Cancel request'),
    cancelTitle: getMessage("Don't cancel"),
    confirmStyle: 'destructive'
  }).then(function(fieldValues) {

    var reason = fieldValues.updatedFields[0].value;

    var ga = new GlideAjax('SCRIPT INCLUDE WITH SCOPED NAME'); // Script Include
    ga.addParam('sysparm_name', 'cancelRequest');
    ga.addParam('sysparm_gsrc_sys_id', g_form.getUniqueValue());
    ga.addParam('sysparm_reason', reason);
    ga.getXMLAnswer(function(answer) {
      // Refresh the form
      g_form.addInfoMessage(answer || 'Cancelled.');
      g_form.refresh(); // or g_form.save() depending on your UX preference
    });
  });
}
 
 
SCRIPT INCLUDE:
---------------------------------------------------------------------
cancelRequest: function(){
    var gsrcId = this.getParameter('sysparm_gsrc_sys_id');
    var reason = this.getParameter('sysparm_reason') || '';
    if (!gsrcId) return 'Missing GSRC sys_id';

    var gsrc=new GlideRecordSecure('CUSTOM TABLE NAME);
    if (!gsrc.get(gsrcId)) return 'GSRC not found';

    // 1) Cancel/close child catalog tasks
    var task = new GlideRecordSecure('CHILD TABLE NAME');
    task.addQuery('u_parent', gsrc.sys_id);
    task.addQuery('active', true);
    task.query();

    while (task.next()) {
      // Set your desired "cancelled/closed" values:
      task.state = 6;
      task.active = false;
      task.work_notes = 'Cancelled due to: ' + reason;
      task.update();
    }

    // 2) Cancel/close the GSRC itself
    gsrc.u_status = 'cancelled';
    gsrc.work_notes = 'Request cancelled. Reason: ' + reason;
    gsrc.update();

    return 'Request and active child tasks were cancelled.';

    },
3 REPLIES 3

satyasubraV3614
Tera Expert

Since you are using GlideRecordSecure(), ACLs are enforced. Please confirm that the user invoking the UI Action has the necessary roles to access the custom table. Insufficient permissions can prevent the Script Include from performing updates.

 

Please mark the answer correct & Helpful, if i could help you.

 

Thanks,

Sagar

Yes, users are having required access and I'm trying with admin account

Ankur Bawiskar
Tera Patron

@Prudhvi Raj Y 

script include and UI action are in same scope?

share screenshots of script include config and UI action config

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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