Sort and set the sequence of a sys_choice field in descending order

Imran28
Kilo Explorer

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;

find_real_file.pngNot sure how to get this working hence reaching out to you guys.

Thank You for you help and suggestions.

Regards,
Imran

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

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!

View solution in original post

1 REPLY 1

Allen Andreas
Administrator
Administrator

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!