creating an sc_task using catalog client script

miles9
Tera Expert

Hi all,

 

I am trying to create a catalog task from a catalog client script.  I am using an OnChange Type against a variable.  The way I would like it to work is, on the catalog task level a user will change a variable and that variable that is changed will trigger the creation of a new task.

1.  Is this possible?

2. The code that I am using does work in the script - background

3. It does not currently work in the catalog client script which is why I am thinking I have to do it differently.

4. If it has to be done differently would a script include work and what exactly would that look like?

 

catalog client script

if (g_form.getValue('variable_to_check') == 'true') {
var task1 = new GlideRecord("sc_task");
task1.initialize();
task1.request_item.setValue('sys_id_request_item');
task1.cat_item.setDisplayValue('catalog_item');
task1.assignment_group.setDisplayValue('assignment_group');
task1.short_description = 'short description';
task1.description = 'description';
task1.insertWithReferences();

 

Thank you

2 ACCEPTED SOLUTIONS

Danish Bhairag2
Tera Sage
Tera Sage

Hi @miles9 ,

 

In order to create a task on change of a field u would require a combination of both Client script as well as script include.

 

Reason for code working in background script n not in client script is because background script executes code on server side whereas when u execute any piece of code in client script it executes it on the client side. Also using GlideRecord in client side is not a good practice.

 

Thanks,

Danish

 

View solution in original post

Hi @miles9 ,

 

Please follow below steps:

 

### 1. Catalog Client Script (onChange):

 

This script will trigger when the `variable_to_check` field changes its value.

 

function onChangeVariableToCheck() {

    var newValue = g_form.getValue('variable_to_check');

 

    // Check if the variable_to_check field changed to 'true'

    if (newValue == 'true') {

        // Call the Script Include to create the catalog task

        var scriptInclude = new MyScriptInclude(); // Replace 'MyScriptInclude' with the actual name of your Script Include

        scriptInclude.createCatalogTask(g_form.getUniqueValue()); // Pass necessary parameters

    }

}

 

 

Replace `'variable_to_check'` with the actual field name you're monitoring for changes. Adjust the conditions and the logic inside the `onChangeVariableToCheck()` function as needed.

 

### 2. Script Include:

 

Create a Script Include that contains the function to create the catalog task.

 

 

var MyScriptInclude = Class.create();

MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    createCatalogTask: function(requestItemId) {

        var task1 = new GlideRecord("sc_task");

        task1.initialize();

        task1.request_item = requestItemId;

        // Set other task fields as needed

        task1.short_description = 'Short description';

        task1.description = 'Description';

        task1.insertWithReferences();

    },

 

    type: 'MyScriptInclude'

});

 

Replace `'Short description'` and `'Description'` with the actual values you want to assign to the newly created catalog task. Ensure you set the required fields and handle all necessary validations before creating the task.

 

### Notes:

- In the onChange script, `g_form.getUniqueValue()` passes the ID of the current record (in this case, the request item) to the Script Include for creating the related catalog task.

- The Script Include method `createCatalogTask()` initializes a new `sc_task` record, sets the request item field, and inserts the new task with reference.

 

Make sure to test these scripts thoroughly in your development or test instance before deploying to a production environment. Adjust the code according to your specific field names, conditions, and requirements.

View solution in original post

2 REPLIES 2

Danish Bhairag2
Tera Sage
Tera Sage

Hi @miles9 ,

 

In order to create a task on change of a field u would require a combination of both Client script as well as script include.

 

Reason for code working in background script n not in client script is because background script executes code on server side whereas when u execute any piece of code in client script it executes it on the client side. Also using GlideRecord in client side is not a good practice.

 

Thanks,

Danish

 

Hi @miles9 ,

 

Please follow below steps:

 

### 1. Catalog Client Script (onChange):

 

This script will trigger when the `variable_to_check` field changes its value.

 

function onChangeVariableToCheck() {

    var newValue = g_form.getValue('variable_to_check');

 

    // Check if the variable_to_check field changed to 'true'

    if (newValue == 'true') {

        // Call the Script Include to create the catalog task

        var scriptInclude = new MyScriptInclude(); // Replace 'MyScriptInclude' with the actual name of your Script Include

        scriptInclude.createCatalogTask(g_form.getUniqueValue()); // Pass necessary parameters

    }

}

 

 

Replace `'variable_to_check'` with the actual field name you're monitoring for changes. Adjust the conditions and the logic inside the `onChangeVariableToCheck()` function as needed.

 

### 2. Script Include:

 

Create a Script Include that contains the function to create the catalog task.

 

 

var MyScriptInclude = Class.create();

MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    createCatalogTask: function(requestItemId) {

        var task1 = new GlideRecord("sc_task");

        task1.initialize();

        task1.request_item = requestItemId;

        // Set other task fields as needed

        task1.short_description = 'Short description';

        task1.description = 'Description';

        task1.insertWithReferences();

    },

 

    type: 'MyScriptInclude'

});

 

Replace `'Short description'` and `'Description'` with the actual values you want to assign to the newly created catalog task. Ensure you set the required fields and handle all necessary validations before creating the task.

 

### Notes:

- In the onChange script, `g_form.getUniqueValue()` passes the ID of the current record (in this case, the request item) to the Script Include for creating the related catalog task.

- The Script Include method `createCatalogTask()` initializes a new `sc_task` record, sets the request item field, and inserts the new task with reference.

 

Make sure to test these scripts thoroughly in your development or test instance before deploying to a production environment. Adjust the code according to your specific field names, conditions, and requirements.