Thursday, November 8, 2018

Custom navigation buttons for sublist records

The navigation buttons at the top of the record enable users to view the previous and next records as sorted by internal ID. However, there are instances when users may need to cycle only through the records in a custom sublist, and not across all records of the same type. For these situations, the standard record navigation buttons do not apply.

The following solution places two custom buttons on the record to enable this feature. It uses two scripts, a User Event and a Client script. The user event retrieves the previous and next internal IDs to the current record and places the custom navigation buttons as appropriate. The client script is then attached to the custom buttons and redirects the user to the appropriate record.

/*1) Create a User Event script with the following code and set it to the Before Record Load event.*/function beforeLoad(type, form){	var parentId = nlapiGetFieldValue('parent_record_field');	var id = nlapiGetRecordId(parentId);	var previd = getPrevNext(parentId, id, true);	var nextid = getPrevNext(parentId, id, false);		form.setScript(384);	// in the setScript call, simply replace the internal ID of the client script with the one specified below		if(previd != '0')	{		form.addButton('custpage_prevrec', 'Previous', 'moveToRecord('+previd+')');	}	if(nextid != '0')	{		form.addButton('custpage_nextrec', 'Next', 'moveToRecord('+nextid+')');	}}// This is the helper function that retrieves the previd and nextid values.function getPrevNext(parent, id, prev){	var filters = [ new nlobjSearchFilter('parent_record_field', null, 'is', parent) ];	if(prev == true) {		filters.push(new nlobjSearchFilter('id', null, 'lessthan', id));	}	else {		filters.push(new nlobjSearchFilter('id', null, 'greaterthan', id));	}	var column = new nlobjSearchColumn('internalid');	var results = new nlapiSearchRecord('sublist_record_internal_id', null, filters, column.setSort(prev));	if(results != null && results.length > 0)	{		return results[0].getValue('internalid');	}	else	{		return 0;	}}/*2) Create a client script with the following code.  Do not create a deployment for this client script.3) Take note of the resulting internal ID for this client script.  The internal ID is used in the setScript function in the user event script above.*/function moveToRecord(id){	window.location = nlapiResolveURL('RECORD', 'sublist_record_internal_id', id);}

No comments:

Post a Comment