Tutorial on Execute Server Code from UI Builder
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2024 01:57 AM
The idea is extracted from this youtube video (https://www.youtube.com/watch?v=3SgY8E6T7oA) related to consume an external rest api in ui builder, but extended to all code and with input data, because most times you want to call server side code, you what to pass data from current ui builder state.
The first thing is to create a tranform, go to data sources -> add -> transform. It is important to follow this path and no go directly to create the tranform from "Now Experience" application because if you do so, then the transform no is selectable from ui builder:
Put it a name and submit, you can now open it in Not Experience -> Tranforms, add all the parameter you will need in this format, for example, for my example i only need the requested item sys id i am working on:
As you can see, the input data can be readed from input objetc for example:
function transform(input) {
var reqid = input.request_item_id;
This is my hole code:
function transform(input) {
var reqid = input.request_item_id;
//var reqid = '6ea9730893100210ad32704efaba1092';
var grItem = new GlideRecord('sc_req_item');
grItem.get(reqid);
var reqLocation = !grItem.requested_for.location.nil() ? grItem.requested_for.location.toString() : '';
var stockRoom = '';
if (reqLocation) {
stockRoom = serachStockRoom(reqLocation);
}
var returnObj = {};
returnObj.description = 'Request Item Related Data ' + reqid;
returnObj.stockroom = stockRoom;
returnObj.stockroom_name = '';
if (stockRoom) {
var grStockRoom = new GlideRecord('alm_stockroom');
grStockRoom.get(stockRoom);
returnObj.stockroom_name = grStockRoom.name.toString();
}
return returnObj;
function serachStockRoom(locationId) {
var grLocation = new GlideRecord('cmn_location');
grLocation.get(locationId);
var grStockRoom = new GlideRecord('alm_stockroom');
grStockRoom.addQuery('location', locationId);
grStockRoom.query();
if (grStockRoom.next()) {
return grStockRoom.sys_id.toString();
} else if (!grLocation.parent.nil()) {
return serachStockRoom(grLocation.parent.toString());
} else {
return '';
}
}
}
As you can see, it is needed to return an object, in my case of the form:
{
description: "Some description",
stockroom: "Sysid of the stockroom",
stockroom_name: "Name of the stockroom"
}
Additionally, you can create an ACL to this record with the permissions needed for the users, in my case it is public and the sysid in the image is the sysid of the transform:
And thats all, you can use now the transform as other data sources, i have use it for read, but you can use it for write, only check in the transform what it modify server data: