- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2019 11:49 AM
Hi All,
I am trying to set the sequence numbering of a choice field by writing a BR in the sys_choice table. I want the latest created record to have the sequence number as 0 and just before latest created record to have as 1 and so on.
Basically 0 should be sequence set for new record and all older records should follow the sequence. Below is the script that I have written but it does not serve the purpose.
/*var sequenceCounter=0;
var gr = new GlideRecord('sys_choice');
gr.addQuery('name','table_name');
gr.addQuery('element','service_month');
gr.orderByDesc('sys_created_on');
gr.query();
while(gr.next()){
//gr.sequence=sequenceCounter;
sequenceCounter++;
current.sequence = sequenceCounter;
}*/
var seq = 0;
var gr = new GlideRecord('sys_choice');
gr.addQuery('name','table_name');
gr.addQuery('element','service_month');
gr.orderByDesc('sys_created_on');
gr.query();
while(gr.next()){
seq = gr.sequence -1;
seq++;
//gr.update();
}
current.sequence = seq;
Not sure how to get this working hence reaching out to you guys.
Thank You for you help and suggestions.
Regards,
Imran
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2019 12:07 PM
I believe the issue is coming in here:
var seq = 0;
var gr = new GlideRecord('sys_choice');
gr.addQuery('name','table_name');
gr.addQuery('element','service_month');
gr.orderByDesc('sys_created_on');
gr.query();
while(gr.next()){
gr.sequence = seq;
seq++;
gr.update();
}
So you wouldn't need to do the top part of your script at all and just run this...
I'm assuming this is running on an insert BR for this specific element and table name?
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2019 12:07 PM
I believe the issue is coming in here:
var seq = 0;
var gr = new GlideRecord('sys_choice');
gr.addQuery('name','table_name');
gr.addQuery('element','service_month');
gr.orderByDesc('sys_created_on');
gr.query();
while(gr.next()){
gr.sequence = seq;
seq++;
gr.update();
}
So you wouldn't need to do the top part of your script at all and just run this...
I'm assuming this is running on an insert BR for this specific element and table name?
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!