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

Sai_Charan_K
Kilo Sage

Hi @Jessica28 ,

I see that you are not passing the ritm sys_id to the script include. Can you try passing it so that the link happens and then it gets tied up to your task.

Please mark this answer as "helpful" and mark it as correct if this helped you in any way.

 

Thanks and Regards,

K. Sai Charan

Sr. Servicenow Developer 

Deloitte India 

Hello @Sai_Charan_K 

Thank you for pointing out my mistake.  I have corrected the code.  However, I was able to create a task at "Requested Item" level, but not at Catalog Task level.  Any suggestion?

 

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_ritmSysId', g_form.getUniqueValue());
        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
        });
    }
}

 

Bert_c1
Kilo Patron

On the sc_task table, the 'cat_item' field is a reference to the 'sc_cat_item' table, and the 'request_item' field is a reference to the 'sc_req_item' table.  Add logic in the script include to set the desired field and value.

Hi @Bert_c1 

I'm confused about where to add the logic in the code. Could you please help me with that? I want to ensure I'm setting the correct fields and values in the script, and I also want to learn by seeing how it's done. 

Thank you

 

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

        // 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
        }
    },

    type: 'EDMS_create_task'

});