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.

Issue with Client Script and Script Include.

Jessica28
Tera Guru

Hello,

We have a multiple-choice variable at the Task level. When the "Yes" option is selected, we want to create a task under the same RITM.

 

Jessica28_0-1727883913440.png

 

We are using a Client Script and Script Include for this, but while it returns "Task created successfully," no record is actually created. Could you please assist us? Thank you!

 

 Client Script:

Jessica28_1-1727884433500.png

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var getLicenseAvailable = g_form.getValue('license_available');
    if (getLicenseAvailable === 'yes') {
        alert(getLicenseAvailable);
        
        // Create a GlideAjax object
        var ga = new GlideAjax('EDMS_create_task'); // Name of the Script Include
        ga.addParam('sysparm_name', 'createTask');
        ga.addParam('sysparm_short_description', "Additional task for checkbox selection:");
        ga.addParam('sysparm_description', "Task description goes here");
        
        ga.getXMLAnswer(function(response) {
            var answer = response; 
            alert(answer); // Display the response
        });
    }
}

 

Script Include:

var EDMS_create_task = Class.create();
EDMS_create_task.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    createTask: function() {
        var task = new GlideRecord('sc_task');
        task.initialize();
		task.request_item = this.getParameter('sysparm_ritmSysId');
        task.short_description = this.getParameter('sysparm_short_description');
        task.description = this.getParameter('sysparm_description');
        var taskSysId = task.insert(); // Create the new task
        return taskSysId ? "Task created successfully" : "Error creating task"; // Return success or error message
    },

    type: 'EDMS_create_task'
});
1 ACCEPTED SOLUTION

Hello Jessica,

 

You will need too update the Client script to pass in which table you want to link your record too. the add logic in the script include as follows:

 

 

 

    createTask: function() {
        var task = new GlideRecord('sc_task');
        task.initialize();
		var tableToLink = this.getParameter('sysparm_table');
		if (tableToLink = 'Requested item')
			task.request_item = this.getParameter('sysparm_ritmSysId');
		if (tableToLink == 'Catalog task');
			task.cat_item = this.getParameter('sysparm_ritmSysId');
        task.short_description = this.getParameter('sysparm_short_description');
        task.description = this.getParameter('sysparm_description');

        var taskSysId = task.insert(); // Create the new task

        // Check if the task was created successfully
        if (taskSysId) {
            // Retrieve the newly created task to get the task number
            task.get(taskSysId);
            return task.number; // Return the task number
        } else {
            return "Error creating task"; // Return an error message
        }
    },

 

 

 

And in the exisitng client script, change to:

 

 

 

        // Create a GlideAjax object
        var ga = new GlideAjax('EDMS_create_task'); // Name of the Script Include
        ga.addParam('sysparm_name', 'createTask');
        ga.addParam('sysparm_short_description', "Additional task for checkbox selection:");
        ga.addParam('sysparm_description', "Task description goes here");
        ga.addParam('sysparm_table', 'Requested item');  

 

 

 

In the new client script for linking a catalog item, use:

 

 

 

        // Create a GlideAjax object
        var ga = new GlideAjax('EDMS_create_task'); // Name of the Script Include
        ga.addParam('sysparm_name', 'createTask');
        ga.addParam('sysparm_short_description', "Additional task for checkbox selection:");
        ga.addParam('sysparm_description', "Task description goes here");
        ga.addParam('sysparm_table', 'Catalog task');

 

 

 

I'm not sure if you want 2 client scripts or if you can add logic to the existing client script to determine which table you want to link to 'sc_req_item' or 'sc_cat_item'. More details on how you decide is needed. If you want both, then update the client script to call the script include twice, with the code snippet above. 

View solution in original post

9 REPLIES 9

Hello Jessica,

 

You will need too update the Client script to pass in which table you want to link your record too. the add logic in the script include as follows:

 

 

 

    createTask: function() {
        var task = new GlideRecord('sc_task');
        task.initialize();
		var tableToLink = this.getParameter('sysparm_table');
		if (tableToLink = 'Requested item')
			task.request_item = this.getParameter('sysparm_ritmSysId');
		if (tableToLink == 'Catalog task');
			task.cat_item = this.getParameter('sysparm_ritmSysId');
        task.short_description = this.getParameter('sysparm_short_description');
        task.description = this.getParameter('sysparm_description');

        var taskSysId = task.insert(); // Create the new task

        // Check if the task was created successfully
        if (taskSysId) {
            // Retrieve the newly created task to get the task number
            task.get(taskSysId);
            return task.number; // Return the task number
        } else {
            return "Error creating task"; // Return an error message
        }
    },

 

 

 

And in the exisitng client script, change to:

 

 

 

        // Create a GlideAjax object
        var ga = new GlideAjax('EDMS_create_task'); // Name of the Script Include
        ga.addParam('sysparm_name', 'createTask');
        ga.addParam('sysparm_short_description', "Additional task for checkbox selection:");
        ga.addParam('sysparm_description', "Task description goes here");
        ga.addParam('sysparm_table', 'Requested item');  

 

 

 

In the new client script for linking a catalog item, use:

 

 

 

        // Create a GlideAjax object
        var ga = new GlideAjax('EDMS_create_task'); // Name of the Script Include
        ga.addParam('sysparm_name', 'createTask');
        ga.addParam('sysparm_short_description', "Additional task for checkbox selection:");
        ga.addParam('sysparm_description', "Task description goes here");
        ga.addParam('sysparm_table', 'Catalog task');

 

 

 

I'm not sure if you want 2 client scripts or if you can add logic to the existing client script to determine which table you want to link to 'sc_req_item' or 'sc_cat_item'. More details on how you decide is needed. If you want both, then update the client script to call the script include twice, with the code snippet above. 

Hi @Bert_c1 

Thank you for your time and effort in assisting me. I will try both options to gain a better understanding of how they work.

 

I have one final question: after creating the new task at the "Catalog Task" level, I would like it to redirect to the Requested Item page.

 

I attempted to use "action.setRedirectURL(current)," but it didn’t work. Do you have any suggestions?

Hi Jessica,

 

It occurred to me that you may want one task with links to both the 'requested_item' and 'cat_item' fields on 'sc_task'. If so, then add one line to your updated client script:

ga.addParam('sysparm_cat_item', g_form.getValue("cat_tiem"));

and just add one line to your script include

		task.request_item = this.getParameter('sysparm_ritmSysId');
		task.cat_item = this.getParameter('sysparm_cat_item');		// new line

and not the changed I proposed earlier.

 

As far as redirecting to the sc_req_tiem record, add the following:

	var reqItem = new GlideRecord('sc_req_item);
	if (reqItem.get(this.getParameter('sysparm_ritmSysId'));
		action.setRedirectURL(reqItem);

in the script include, before returning to redirect to the new record. 

Good morning @Bert_c1 

You are amazing. Thank you

On the redirect, a test using that in a script include failed.  I'm not sure how you would re-direct, create a new post here on that.