Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Insert record on table with reference field

Hannah C
Giga Expert

Hello,

We have a table for freeware called u_freeware_software and we have a catalog item that users fill out to add new approved freeware to the list. Once submitted, I added a runscript on the workflow that will take the values from the RITM and add a record to the u_freeware_software table. All of the fields that are just text values are inserting correctly (name, short_description, version, and comments.

The manufacturer field is not populating. This field is a reference variable to the core_company table. How do you insert a new value for a reference field in a workflow script?

Here is the script I have:

var freeware_name = current.variables.freeware_title;
var description = current.variables.details;
var version = current.variables.version;
var manufacturer = current.variables.manufacturer;
var url = current.variables.url;

var gr = new GlideRecord('u_freeware_software');

gr.initialize();

gr.name = freeware_name;
gr.short_description = description;
gr.version = version;
gr.manufacturer.setDisplayValue(manufacturer);
gr.comments = url;

gr.insert();

 

I tried doing gr.manufacturer = manufacturer; but then I read on the community site that for reference variables you need to use setDisplayValue but that still didn't work.

1 ACCEPTED SOLUTION

Hello Hannah

If the core_company record don't exist you must insert first that record.

You can use something similar to this:

You must use the gliderecord to insert the core_company.
in the "insert statement" you can make this:

var grCoreCompany = new GlideRecord('core_company');
grCoreCompany.initialize();
grCoreCompany.field1 = valueField1;
var coreCompanyInserted = grCoreCompany.insert(); //to obtain the sys_id generated

Then you can use the coreCompanyInserted

gr.manufacturer = coreCompanyInserted;

 

Please, mark correct or useful if I helped you
Thanks
Ariel

View solution in original post

8 REPLIES 8

arielgritti
Mega Sage

Hello

Try using the sys_id.

gr.manufacturer = current.variables.manufacturer.sys_id;

 

Please, mark correct or useful if I helped you
Thanks
Ariel

Thank you Ariel. However that didn't work because I'm trying to insert and create the record. The manufacturer variable hasn't been created on the u_freeware_software table or the core_company table yet. Do we need to write a script to insert it in the core_company table first then insert on the u_freeware_software table?

Hello Hannah

If the core_company record don't exist you must insert first that record.

You can use something similar to this:

You must use the gliderecord to insert the core_company.
in the "insert statement" you can make this:

var grCoreCompany = new GlideRecord('core_company');
grCoreCompany.initialize();
grCoreCompany.field1 = valueField1;
var coreCompanyInserted = grCoreCompany.insert(); //to obtain the sys_id generated

Then you can use the coreCompanyInserted

gr.manufacturer = coreCompanyInserted;

 

Please, mark correct or useful if I helped you
Thanks
Ariel

Omg that is just what I needed! Thank you so so much!