Hi everyone, I would like to customize search func...
# suitecommerce
t
Hi everyone, I would like to customize search function. Currently, the search works only for products. But I would like to apply search for category also.
c
I suppose you could create a service that queries the commerce category record and call it alongside the regular call that happens when the user types in the search bar. When the service returns you add the results into whatever it currently in the result set.
t
Thanks for your replies @Chris Currently, I calls the API and I can't customize that API.
In that case, I have to build custom API response using Restlet?
t
Thanks @Chris
c
Copy code
/**
* @NApiVersion 2.x
* @NModuleScope Public
*/
define([
	'N/log',
	'N/search',
	'N/runtime'],
	function (
		log,
		search,
		runtime) {
		"use strict";

		return {
			service: function (ctx) {

				var request = ctx.request;
				try {

					const user = runtime.getCurrentUser();
					log.debug('user', user);
					// if there is no userId then nobody is logged in
					if (!user.id) return;
					const params = JSON.parse(ctx.request.body);
					log.debug("params", params);
					ctx.response.setHeader('Content-Type', 'application/json');
					var soid = params.soid;
					if (!soid) {
						log.debug("soid not found", request.parameters);
						return ctx.response.write(JSON.stringify({ success: false }));
					}
					var salesOrderId = null;
					search.create({
						type: "salesorder",
						filters:
							[
								["type", "anyof", "SalesOrd"],
								"AND",
								["numbertext", "is", soid],
								"AND",
								["mainline", "is", "T"]
							],
						columns:
							[
								"internalid"
							]
					}).run().each(function (r) {
						salesOrderId = parseInt(r.id);
						return true;
					});
					if (salesOrderId) {
						// ... do something with it here
					}
					return ctx.response.write(JSON.stringify({ success: true }));
				}
				catch (ex) {
					log.error("Error in Backend Model", ex);
					return ctx.response.write(JSON.stringify({ success: false }));
				}
			}

		};
	});
Here's an example that looks up a sales order.
t
Thank you
👍🏻 1
Currently, this is my code for searching api request And I can't find the code for
/api/items
and I need to customize it
c
The
/api/items
endpoint isn't customizable.
t
I have to build my own endpoint?
c
Yea
t
I see. But response format should be same as
api/items
?
c
You can send different requests to that endpoint, but it's functionality isn't something you can edit.
With service files, you choose the format
The service file is just a wrapper around the
N/https
module.
which gives you access to the
request
and
response
objects to do what you want with.
t
make sense