Set Sysid to reference field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2023 12:33 AM - edited ‎12-08-2023 12:34 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2023 12:40 AM
Hi @Mark Wood ,
If you want to set a reference field using a script, you need to provide the sys_id of the record you want to reference. If your reference field is based on the "Scheduled Name" field, you'll need to find the record with that name and set the sys_id.
Here is an example of how you can do this using GlideRecord:
(function executeRule(current, previous /*null when async*/) {
// Assuming 'scheduled_name' is the field you want to use as a display field for the reference field
var scheduledName = current.scheduled_name;
// Query the table to find the record with the given scheduled name
var gr = new GlideRecord('your_table_name');
gr.addQuery('scheduled_name', scheduledName);
gr.query();
if (gr.next()) {
// Set the reference field using the sys_id of the record with the specified scheduled name
current.reference_field = gr.sys_id;
} else {
gs.error("Record with scheduled name '" + scheduledName + "' not found.");
}
})(current, previous);
Replace 'your_table_name' with the actual name of your table and 'reference_field' with the actual name of your reference field.
Thanks,
Ratnakar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2023 12:42 AM
Hello @Ratnakar7 ,
In the same way, I am trying to set sys_id for the reference field but instead of showing the display name, it's showing that sys_id. I hope you understand the issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2023 12:45 AM
Hi check below code
// Assuming 'target_table' is the target table of the reference field
var targetTable = 'target_table';
var scheduledName = current.scheduled_name_field; // Replace with the actual field name
var targetRecord = new GlideRecord(targetTable);
if (targetRecord.get('scheduled_name_field', scheduledName)) {
current.reference_field = targetRecord.sys_id;
} else {
gs.error('Record with scheduled name ' + scheduledName + ' not found in ' + targetTable);
}