- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2023 08:28 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2023 10:00 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2023 08:37 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2023 08:51 AM
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();
}
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2023 10:00 AM
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.