Pagination in a Suitelet page using a SELECT field in the form to select page number – Simplified

This code will dynamically change the data according to the selected page number using Suitelet and Client Scripts. Sample search is used in the code ni order to set the sublist values.

Suitelet code:

Create a select field for pagination

let selectPageField = form.addField({

id: ‘custpage_page_select’,

type: serverWidget.FieldType.SELECT,

label: ‘Select Page’

});

// Get current page from URL parameter (default to 1)

let currentPage = scriptContext.request.parameters.pageIndex || 1; // recieve the value from the client script or else set it to 1 by default

let resultsPerPage = 10; // Define how many results per page

selectPageField .defaultValue = pageIndex;

// Fetch search results (for example, getting a list of customers)

let customerSearch = search.create({

type: search.Type.CUSTOMER,

columns: [‘internalid’, ‘entityid’],

filters: []

});

// Get total results count

let pagedData = customerSearch.runPaged({

pageSize: resultsPerPage

});

let totalPages = pagedData.pageRanges.length;

// Populate select field with page numbers

for (var i = 1; i <= totalPages; i++) {

selectPageField.addSelectOption({

value: i,

text: i,

isSelected: (i === currentPage)

});

}

// Fetch results for the current page

let pageData = pagedData.fetch({

index: currentPage – 1

});

// Add sublist to display results

let sublist = form.addSublist({

id: ‘custpage_results_sublist’,

type: serverWidget.SublistType.LIST,

label: ‘Results’

});

sublist.addField({

id: ‘custpage_internalid’,

type: serverWidget.FieldType.TEXT,

label: ‘Internal ID’

});

sublist.addField({

id: ‘custpage_entityid’,

type: serverWidget.FieldType.TEXT,

label: ‘Entity ID’

});

// Populate sublist with data

for (var i = 0; i < pageData.data.length; i++) {

let result = pageData.data[i];

sublist.setSublistValue({

id: ‘custpage_internalid’,

line: i,

value: result.getValue(‘internalid’)

});

sublist.setSublistValue({

id: ‘custpage_entityid’,

line: i,

value: result.getValue(‘entityid’)

});

Client Script:

let page = scriptContext.currentRecord.getValue({fieldId: ‘custpage_pagenumber’}); // Field Id of the Page Selector field

if(scriptContext.fieldId === ‘custpage_pagenumber’){

let suiteletUrl = url.resolveScript({

scriptId: ‘deployment_id_of_your_script’,

deploymentId: ‘script_id_of_your_script’,

params: {

pageIndex: page

}

});

window.location.href = suiteletUrl;

}

Leave a comment

Your email address will not be published. Required fields are marked *