Query in server Scripting

devservicenow k
Tera Contributor

Under My open Incidents in SP portals i need to sort the state fields value. where i need the On hold state field should come at the top . i have a custom widgets for this but i need the state on hold values should be seen at the top  followed by other values . 

 

 

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Hi @devservicenow k ,

To include on hold reason, try this script.

//we get only a max number of elements to avoid to have a big list of it
var maxNumberOfItemsInTheList = parseInt(options.max_number_of_elements_shown_on_the_list);
//set 10 if maxnumber is undefined, empty or negative value
maxNumberOfItemsInTheList = maxNumberOfItemsInTheList > 0 ? maxNumberOfItemsInTheList : 10;
var initRow = 0;
var lastRow = maxNumberOfItemsInTheList;
var currentPage = 0; //0 is the first page
if (input) {
    // update pagination
    currentPage = input.pagination.currentPage;
    initRow = (currentPage * maxNumberOfItemsInTheList);
    lastRow = initRow + maxNumberOfItemsInTheList;

}

var displayField = "short_description";

var secondaryfields = "number,sys_updated_on,state";
secondaryfields = secondaryfields.split(",");


var gr = new GlideRecordSecure('incident');
gr.chooseWindow(initRow, lastRow);
gr.addEncodedQuery('active=true^caller_idDYNAMIC90d1921e5f510100a9ad2572f2b477fe^ORu_affected_userDYNAMIC90d1921e5f510100a9ad2572f2b477fe');
gr.orderByDesc("state");
gr.query();
var rowCount = gr.getRowCount();
var approvals_on_hold = [];
var approvals = [];
var ids = [];
var source_tables = [];

while (gr.next()) {

    var j = {};
    j.sys_id = gr.getUniqueValue();
    j.table = gr.getRecordClassName();

    j.display_field = getField(gr, displayField);

    j.secondary_fields = [];
    secondaryfields.forEach(function(f) {
        j.secondary_fields.push(getField(gr, f));
    });

    j.state = gr.getValue("state");
    j.stateLabel = gr.state.getDisplayValue();
    j.hold_reason = '';
	if(j.state == '3'){
                j.hold_reason = gr.hold_reason.getDisplayValue();
		approvals_on_hold.push(j);

	}else{
		approvals.push(j);
	}
}

var arrayUtil = new ArrayUtil();
approvals = arrayUtil.concat(approvals_on_hold, approvals);

function getField(gr, name) {


    var f = {};
    f.display_value = gr.getDisplayValue(name);
    f.value = gr.getValue(name);
    var ge = gr.getElement(name);
    if (ge == null)
        return f;

    f.type = ge.getED().getInternalType();
    if (f.type == "glide_date_time")
        f.isFuture = gs.dateDiff(gr.getValue(name), gs.nowNoTZ(), true) < 0;
    else if (f.type == "glide_date")
        f.isFuture = gs.dateDiff(gr.getValue(name), gs.now(), true) < 0;
    f.label = ge.getLabel();
   // gs.log('secondaryfieldsnameFFFF' + JSON.stringify(f));

    return f;

}

data.approvals = approvals;
// for pagination
data.pagination = {};
data.pagination.hasNext = (approvals.length == (parseInt(lastRow) - parseInt(initRow)) && lastRow < rowCount);
data.pagination.hasPrevious = parseInt(initRow) > 0;
data.pagination.from = parseInt(initRow + 1);
data.pagination.to = parseInt(lastRow) < parseInt(rowCount) ? parseInt(lastRow) : parseInt(rowCount);
data.pagination.of = parseInt(rowCount);
data.pagination.showPagination = data.pagination.hasPrevious || data.pagination.hasNext;
data.pagination.currentPage = data.pagination.from > data.pagination.to ? currentPage - 1 : currentPage;

 

 

Thanks,
Anvesh

View solution in original post

5 REPLIES 5

Sai Shravan
Mega Sage

Hi @devservicenow k / @servicenow lath ,

 

To sort the state field values in your custom widget, you can modify the query by using the addQuery method to add an additional condition to sort the records with "On hold" state field values first, followed by the other state field values. Here's an example of how you can modify your script:

 

var gr = new GlideRecordSecure('incident');
gr.chooseWindow(initRow, lastRow);
gr.addEncodedQuery('active=true^caller_idDYNAMIC90d1921e5f510100a9ad2572f2b477fe^ORu_affected_userDYNAMIC90d1921e5f510100a9ad2572f2b477fe');
// Add a query to sort records by the state field in descending order, with On hold records first
gr.addQuery('state', 'On hold');
gr.addOrCondition('state', '!=', 'On hold');
gr.orderByDesc("state");
gr.query();

With this modification, the records with "On hold" state field values will appear at the top, followed by the other state field values in descending order.

 

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you