Set Sysid to reference field

Mark Wood
Tera Contributor

Hello Experts,

I have written a business rule script that will set a scheduled name for for reference field. unfortunately, I am not able to value for reference it's showing sys_id.but my display field is the Scheduled name for that reference field.

Thank You.

3 REPLIES 3

Ratnakar7
Mega Sage
Mega Sage

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

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.

Harish Bainsla
Kilo Patron
Kilo Patron

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);
}