Need to create new field with default value

navdeep_singh
Giga Guru

I want to create a new field (integer) with a value"0" on Incident table.   I want this value to be applied on new as well as existing records.

Will the "Default Value" work? if not please suggest any other option.

Thank you,

Navdeep

1 ACCEPTED SOLUTION

And they weren't updated:


find_real_file.png



Yes, we did it differently.



I went through the dictionary



1. went to System Definition -> Dictionary. Clicked on new


2. Choose the incident table, Integer type, and added 0 as default value:


find_real_file.png


3. Clicked on Submit


4. Run the script to confirm:


find_real_file.png


find_real_file.png



Telmo


View solution in original post

14 REPLIES 14

Chuck Tomasi
Tera Patron

Default value will only populate new records. If you want to update the existing records, you need to update them manually. Something like this will work without triggering business rules or updating other fields.



var inc = new GlideRecord('incident');


inc.query();



while (inc.next()) {


        inc.setWorkflow(false);


        inc.autoSysFields(false);


        inc.u_count_field = 0; // Change to YOUR field name


        inc.update();


}


I just tried Chuck, And it worked for me. The default value is populated in the existing records as well.


-Anurag

telmo
Tera Expert

Hi Navdeep,



Just created a new field, with 0 as default value, on incident table and the existing records got the 0 as value:


find_real_file.png




Regards,


Telmo


Hi Telmo,



Be careful... The list will display a 0, but that's not really stored in the database until the record is created/updated.



I just ran this simple little script to verify and got the following output on number and u_count, where u_count has a default value of 0.



var inc = new GlideRecord('incident');


inc.query();


while (inc.next()) {


  gs.print(inc.getValue('number') + ' - ' + inc.getValue('u_count'));


}


find_real_file.png