Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to return result from script include?

Meera_P
Tera Expert

Hello,

I'm learning script include on how to return a result to the caller. Please provide guidance.  Thank you

I would like to display an alert after a record has been created.

UI Action Script:

function requestApproval() {
    var approverSysId = g_form.getValue('assigned_to');
    var recordSysId = g_form.getUniqueValue();

    var ga = new GlideAjax('NewApprovalRecord');
    ga.addParam('sysparm_name', 'createApprover');
    ga.addParam('sysparm_approver', approverSysId); 
    ga.addParam('sysparm_sys_id', recordSysId); 
    ga.getXMLAnswer(callBackFunction);
}

function callBackFunction(result) {
    if (result == 'Yes') {
alert(result);
    }
}

Script Include:

var NewApprovalRecord = Class.create();
NewApprovalRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    createApprover: function() {

        var approverID = this.getParameter('sysparm_approver');
        var recordID = this.getParameter('sysparm_sys_id');

        var newApproval = new GlideRecord('sysapproval_approver');
        newApproval.initialize();
        newApproval.setValue('state', 'requested');
        newApproval.setValue('approver', approverID);
        newApproval.setValue('sysapproval', recordID);
        newApproval.insert();
    },
	

    type: 'NewApprovalRecord'
});

 

2 ACCEPTED SOLUTIONS

Kieran Anson
Kilo Patron

Hey!

Welcome to SN.

To return a response from a client-callable script include you can do the following (which is applicable when using getXMLAnswer)

createApprover: function() {

        var approverID = this.getParameter('sysparm_approver');
        var recordID = this.getParameter('sysparm_sys_id');

        var newApproval = new GlideRecord('sysapproval_approver');
        newApproval.initialize();
        newApproval.setValue('state', 'requested');
        newApproval.setValue('approver', approverID);
        newApproval.setValue('sysapproval', recordID);
        newApproval.insert();

		return newApproval.getUniqueValue();
    }

The javasciprt return operator defines what the function i returning, and the newApproval.getUniqueValue() returns a string value of the sys_id. You want to make sure you're returning a string(able) value, not an object.

 

So the following wouldn't work, as newApproval isa GlideObject

createApprover: function() {

        var approverID = this.getParameter('sysparm_approver');
        var recordID = this.getParameter('sysparm_sys_id');

        var newApproval = new GlideRecord('sysapproval_approver');
        newApproval.initialize();
        newApproval.setValue('state', 'requested');
        newApproval.setValue('approver', approverID);
        newApproval.setValue('sysapproval', recordID);
        newApproval.insert();

		return newApproval
    }

 

But this would

createApprover: function() {

        var approverID = this.getParameter('sysparm_approver');
        var recordID = this.getParameter('sysparm_sys_id');

        var newApproval = new GlideRecord('sysapproval_approver');
        newApproval.initialize();
        newApproval.setValue('state', 'requested');
        newApproval.setValue('approver', approverID);
        newApproval.setValue('sysapproval', recordID);
        newApproval.insert();

		return true
    }

View solution in original post

Hemanth M1
Giga Sage
Giga Sage

Hi @Meera_P ,

 

try this

var NewApprovalRecord = Class.create();
NewApprovalRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    createApprover: function() {

        var approverID = this.getParameter('sysparm_approver');
        var recordID = this.getParameter('sysparm_sys_id');

        var newApproval = new GlideRecord('sysapproval_approver');
        newApproval.initialize();
        newApproval.setValue('state', 'requested');
        newApproval.setValue('approver', approverID);
        newApproval.setValue('sysapproval', recordID);
        return  newApproval.insert(); //this would return sys_id of the approval record
    },
	

    type: 'NewApprovalRecord'
});
Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

View solution in original post

7 REPLIES 7

@Kieran Anson 

Thank you for correcting the code. I have a few more questions that I'd like to ask:

1. Why are we using JSON notation to store multiple values?
2. The JSON response I'm currently receiving looks like this:

 

Meera_P_0-1718818684356.png

 


3. How can I format the return to be suitable for use in the description field such as:

Approver Name:  ABC

Approver SysID:  123456

Approval Number:   CHG0032443

 

Here is the code:

    var ga = new GlideAjax('NewApprovalRecord');
    ga.addParam('sysparm_name', 'createApprover');
    ga.addParam('sysparm_approver', approverSysId); //Approver SysID
    ga.addParam('sysparm_sys_id', recordSysId); //changeSysID
    ga.getXMLAnswer(callBackFunction);
}

function callBackFunction(answer) {
    alert(answer);
    g_form.setValue('description', answer);
 }

  1. JSON allows us to easily stored more information in a format that can be programmatically accessed. 
  2. When accessing JSON via your callback, you first need to parse it. 
var ga = new GlideAjax('NewApprovalRecord');
    ga.addParam('sysparm_name', 'createApprover');
    ga.addParam('sysparm_approver', approverSysId); //Approver SysID
    ga.addParam('sysparm_sys_id', recordSysId); //changeSysID
    ga.getXMLAnswer(function(response){

		if(response == null){
			g_form.addErrorMessage("Invalid Response");
			return;
		}

		var responseJSON = JSON.parse(response);

		var composeDescription = '';
		//There's nicer ways to do this, but we'll keep it simple
		composeDescription += 'Approval Name: ' + responseJSON.approverName;
		composeDescription += 'Approval SysID: ' + responseJSON.approverSysId;
		composeDescription += 'Approval Number: ' + responseJSON.approvingRecordDisplayValue;

		g_form.setValue('short_descrption' , composeDescription);

	});

Hemanth M1
Giga Sage
Giga Sage

Hi @Meera_P ,

 

try this

var NewApprovalRecord = Class.create();
NewApprovalRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    createApprover: function() {

        var approverID = this.getParameter('sysparm_approver');
        var recordID = this.getParameter('sysparm_sys_id');

        var newApproval = new GlideRecord('sysapproval_approver');
        newApproval.initialize();
        newApproval.setValue('state', 'requested');
        newApproval.setValue('approver', approverID);
        newApproval.setValue('sysapproval', recordID);
        return  newApproval.insert(); //this would return sys_id of the approval record
    },
	

    type: 'NewApprovalRecord'
});
Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025