Skip to Content
0

Service Layer Script Engine with UDT's

Oct 31, 2016 at 10:20 PM

124

avatar image

Hello Experts!

I've been trying to use the Layer Service Script Engine with a UDT but I have not been successful

I'm trying to delete a record of the UDT

The first thing I do in my script is to generate the Service Layer Context

var ServiceLayerContext = require('ServiceLayerContext.js');
var slContext = new ServiceLayerContext();

And then I tried to remove the record in 4 different ways:

//Test 1
var dataSrvRes = slContext.MYTABLE.remove(key);

//Test 2
var dataSrvRes = slContext.U_MYTABLE.remove(key);

//Test 3
var dataSrvRes = slContext.remove("MYTABLE", key);

//Test 4
var dataSrvRes = slContext.remove("U_MYTABLE", key);

Tests 1 and 2 generate the error: "Script error: call function 'POST' failure [TypeError: Cannot read property 'remove' of undefined]."

Test 3 returns an XML with the error: "The proxy server received an invalid response from an upstream server. The proxy server could not handle the request"

And Test 4 generates the error: "Bad Request - Error in query syntax."

I don't know what I'm doing wrong, I hope you can help me

The script is installed correctly with the "SAP Business One Extension Manager" and the test calls are made using "Postman"

Thanks in advance

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Maria Trinidad MARTINEZ GEA
Jul 05, 2017 at 06:41 AM
1

Hi Ismael,

In a script to be uploaded to SL Script Engine you need to implement the GET/POST/PATCH/DELETE operations that will be called by SL when invoking your script.

Here you have a sample script for a UDO having a main table with name DMS_ROUTESETTING:

Hope it helps
Trinidad.

var ServiceLayerContext = require('ServiceLayerContext.js');
var DMS_ROUTESETTING = require('EntityType/DMS_ROUTESETTING.js');
var http = require('HttpModule.js');
/**
 * Entry function the POST http request.
 * POST /b1s/v1/script/test/ScriptDMS_ROUTESETTING
 * 
 * {
  "Code": "05",
  "Name": "TRM Test",
  "U_RefNo": "100",
  "U_Description": "Sesson 2",
  "DMS_RS_LINESCollection": [
    {
      "U_Monday": "N",
      "U_StartWeek": 10,
      "U_Status": "Started"
    },
    {
      "U_Monday": "Y",
      "U_StartWeek": 20,
      "U_Status": "Granted"
    }
  ]
}
 */
function POST() {
    var jsonObj = http.request.getJsonObj();
    if (!jsonObj) {
        throw http.ScriptException(http.HttpStatus.HTTP_BAD_REQUEST, "fail to get the content of JSON format from the request payload");
    }

// Modify 
    var dmsrs = DMS_ROUTESETTING.create(jsonObj);
console.log("ScriptDMS_ROUTESETTING request body " + JSON.stringify(dmsrs));
dmsrs.U_Description = "Added via SL ScriptEngine";
console.log("ScriptDMS_ROUTESETTING modified " + JSON.stringify(dmsrs));

    var slContext = new ServiceLayerContext();
    var dataSrvRes = slContext.DMS_RouteSetting.add(dmsrs);
    if (dataSrvRes.isOK()) {
        http.response.send(http.HttpStatus.HTTP_CREATED, dataSrvRes.body);
    } else {
        http.response.send(http.HttpStatus.HTTP_BAD_REQUEST, dataSrvRes.body);
    }
}
/** 
 * Entry function the GET http request.
 * GET /b1s/v1/script/SA/ScriptDMS_ROUTESETTING('05')
 * 
 */
function GET() {
var key = http.request.getEntityKey();
    if (!key) {
        throw http.ScriptException(http.HttpStatus.HTTP_BAD_REQUEST, "Fail to get entity key from request URL");
    }
    console.log("ScriptDMS_ROUTESETTING request body" + key);
    var slContext = new ServiceLayerContext();
    var dataSrvRes = slContext.DMS_RouteSetting.get(key);
    if (!dataSrvRes.isOK()) {
        http.response.send(http.HttpStatus.HTTP_NOT_FOUND, dataSrvRes.body);
    } else {
        http.response.send(http.HttpStatus.HTTP_OK, dataSrvRes.body);
    }
}
/** 
 * Entry function the PATCH http request.
 * PATCH /b1s/v1/script/test/ScriptDMS_ROUTESETTING('05')
 * 
 * {"Name": "TRM NEW"}
 */
function PATCH() {
    var key = http.request.getEntityKey();
    if (!key) {
        throw http.ScriptException(http.HttpStatus.HTTP_BAD_REQUEST, "Fail to get entity key from request URL");
    }
    var jsonObj = http.request.getJsonObj();
    if (!jsonObj) {
        throw http.ScriptException(http.HttpStatus.HTTP_BAD_REQUEST, "fail to get content of JSON format from the request payload");
    }
    var slContext = new ServiceLayerContext();
    var dataSrvRes = slContext.DMS_RouteSetting.update(jsonObj, key);
    if (dataSrvRes.isOK()) {
        http.response.send(http.HttpStatus.HTTP_NO_CONTENT, dataSrvRes.body);
    } else {
        http.response.send(http.HttpStatus.HTTP_BAD_REQUEST, dataSrvRes.body);
    }
}
/** 
 * Entry function the DELETE http request.
 * DELETE /b1s/v1/script/test/ScriptDMS_ROUTESETTING('05')
 * 
 */
function DELETE() {
    var key = http.request.getEntityKey();
    if (!key) {
        throw http.ScriptException(http.HttpStatus.HTTP_BAD_REQUEST, "Fail to get entity key from request URL");
    }
    var slContext = new ServiceLayerContext();
    var dataSrvRes = slContext.DMS_RouteSetting.remove(key);
    if (dataSrvRes.isOK()) {
        http.response.send(http.HttpStatus.HTTP_NO_CONTENT, dataSrvRes.body);
    } else {
        http.response.send(http.HttpStatus.HTTP_BAD_REQUEST, dataSrvRes.body);
    }
}
Share
10 |10000 characters needed characters left characters exceeded