petercawdron
Kilo Guru

There may be times where you want to know what the default value of a field is going to be, or if a field in a record is the default (ie, it wasn't specifically added by the user). Here's how you can do that in a script

///////////////////////////////////
function getThisDefault(whichGlideRecord, whichField){return (gs.nil(whichGlideRecord[whichField].getED().getDefault()) ? '' :whichGlideRecord[whichField].getED().getDefault())}
///////////////////////////////////

var gr = new GlideRecord('sc_task')
gr.initialize()

gs.info('The default value for made_sla is \t'  + getThisDefault(gr, 'made_sla'));
gs.info('The default value for state is \t'     + getThisDefault(gr, 'state'));
gs.info('The default value for opened_by is \t' + getThisDefault(gr, 'opened_by'));

var thisNewRecord = gr.insert();

gs.info('-----------------------')

var getNewRecord = new GlideRecord('sc_task')
getNewRecord.get(thisNewRecord)

gs.info('made_sla \t' + getNewRecord.made_sla.getValue())
gs.info('state \t'    + getNewRecord.state.getValue())
gs.info('opened_by \t'+ getNewRecord.opened_by.getValue())

The thing to note, however is that your defaults won't always match the values stored, for example, Boolean and dynamic values will appear as defaults one way before becoming actual database values...

find_real_file.png

Happy coding

Comments
Community Alums
Not applicable

Hi @petercawdron,

I think you should now be able to achieve this before actually creating a record by using newRecord() instead of initialize(). @Göran WitchDoctor Lundqvist has a great video on the different use cases for the two - https://community.servicenow.com/community?id=community_video&sys_id=d7e08749db947b44fece0b55ca9619c...

Should be useful if you want to avoid creating a record and then having to update it based on an initial value - in our use case we're trying to increase the priority by a certain number of points based on that initial value which won't always be the same.

Basically, I got to your article because I only want to find out the default of a particular field and avoid for the unique number generator engine to run unnecessarily.

Was wondering if you found something around this topic during your research?

Thank you

Community Alums
Not applicable

Hi @petercawdron,

I've see SN use newRecord instead to achieve something similar.

if (!priority) {
    var glideRecord = new GlideRecord("sn_hr_core_case");
    if (glideRecord.canCreate()) {
        glideRecord.newRecord();
        priority = glideRecord.getValue('priority');
    }
}

 Any particular use case for your solution instead? Or do you know of any downsides to SN's approach?

Thank you

Community Alums
Not applicable

Another solution used by ServiceNOW that I've just came across.

new GlideRecord("kb_knowledge").getElement("valid_to").getED().getDefault();
Version history
Last update:
‎03-10-2020 06:06 PM
Updated by: