How to create Demand Tasks from UI action on Related List

AM24
Giga Guru

Hello,

 

I have a related list on a form, which has a button "Generate Tasks." This was created using a UI Action. Upon clicking that button, I want to generate several tasks on that related list. I would like that short description of that task to be set and that this record should have the proper parent.

 

Currently I have set the Onclick to generateTasks() and have the script

 

function generateTasks(){

  var gr = new GlideRecord('dmn_demand_task');

gr.initialize();

gr.short_description = 'description';

gr.parent = g_list.getSubmitValue('sysparm_collectionID');

gr.insert();

}

So far the parent field has not been populating. Using alert() function, I made sure that g_list.getSubmitValue('sysparm_collectionID') does return the correct sys id of the parent. However, the parent field on the demand task table is read only, which might be causing it to fail. I wasn't sure how to get it to work. Thank you.

1 ACCEPTED SOLUTION

AM24
Giga Guru

I eventually just had to call a script include from this UI Action, and had the code from before run on there. That fixed the issue.

View solution in original post

3 REPLIES 3

Marcus Walbridg
Tera Expert

In ServiceNow, if a field is read-only, you cannot directly set its value using the GlideRecord API. However, there are a couple of ways you can set the parent field value for the demand task record.

 

Option 1: Using the parent field's underlying reference field

function generateTasks() {
var gr = new GlideRecord('dmn_demand_task');
gr.initialize();
gr.short_description = 'description';

// Set the parent reference field value
gr.parent_ref = g_list.getSubmitValue('sysparm_collectionID');

gr.insert();
}

 

Option 2: Using the dot-walking technique

function generateTasks() {
var gr = new GlideRecord('dmn_demand_task');
gr.initialize();
gr.short_description = 'description';

// Set the parent field using dot-walking
gr.parent = g_list.getSubmitValue('sysparm_collectionID') + ''; // Convert to string

gr.insert();
}

 

Riya Verma
Kilo Sage

Hi @AM24 ,

 

Hope you are doing great.

 

The problem you're encountering is likely due to the parent field being read-only on the demand task table. To set the parent field successfully, you need to utilize the setValue()  method instead of directly assigning a value to the field. below is modified script:

function generateTasks() {
  var gr = new GlideRecord('dmn_demand_task');
  gr.initialize();
  gr.short_description = 'description';
  gr.setValue('parent', g_list.getSubmitValue('sysparm_collectionID'));
  gr.insert();
}

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

AM24
Giga Guru

I eventually just had to call a script include from this UI Action, and had the code from before run on there. That fixed the issue.