Can anyone advise on the correct way to insert custom values

charles898c
Mega Contributor

Hello,

I am working with a custom scoped app in ServiceNow and need to add custom values to a choice field (priority_level) during the app installation process. I’ve written a fix script to do this, but the values are not being inserted into the choice field as expected.

Can anyone advise on the correct way to insert custom values into a choice field during app installation? I’d appreciate any guidance or examples of how to achieve this.

Thanks in advance!

 

2 REPLIES 2

AshishKM
Kilo Patron
Kilo Patron

Hi @charles898c , 

 

If you have those choice values on some Dev instance, then you can export all valid records in XML format and import on target instance. Don't create new choice ( using fixscript )  on target instance otherwise the reference of choice values will be change on target instance and Select box will not show the same.

 

-Thanks,

AshishKM

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

MackI
Kilo Sage

Hi @charles898c 

 

Understanding the Challenges

Timing: During app installation, the choice list table (sys_choice) and your custom application's table might not be fully available when your fix script runs.
Transactions: App installation uses transactions. Your script might be running within a transaction that hasn't yet committed the necessary table changes.
Caching: ServiceNow caches choice lists. Adding choices directly to the database might not be reflected immediately in the UI.
Recommended Approach

sys_choice Table: You need to insert records directly into the sys_choice table. This table stores all choice list values across the platform.
onInstall Function in Fix Script: Use the onInstall function in your Fix Script. This ensures your script runs specifically during the installation process of your application version.
gs.getTransactionID(): Capture the current transaction ID. This is crucial for ensuring that your inserts are associated with the correct application installation.
Manual Inserts (GlideRecord): Use GlideRecord to directly insert records into sys_choice, setting the appropriate fields like table, element, value, label, dependent_value, sequence, and transaction_id.
gs.flushChoiceListCache() (Optional but Recommended): While not always mandatory, flushing the choice list cache can help ensure that your new choices are immediately available in the UI.

Note: This is a global function and may impact performance during installation, use with caution. In most modern versions of ServiceNow, this is not needed.

 

If you like this opinion and your problem is resolved after reviewing and applying it. Please kindly mark this your best answer‌🌠‌ OR  mark it  Helpful ‌‌ if you think that you get some insight from this content relevant to your problem and help me to contribute more to this community

MackI | ServiceNow Developer | 2 *Mainline Certification | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia