Tip 9: How to call an RPG program to return multiple records for a PAPI route

This tip documents how to:

  1. Call an RPG program to get back data for multiple records.

  2. Use that returned data as output data for the PAPI route.

Create a low-code PAPI file

  • Navigate to the workspace "pjstips".

  • Click New/API File.

  • Click Edit/Source, to bring up the Source view.

  • Clear the current default code, and enter the code below.

    pjstips_09_1.api.json

    { "apiFileID": "95e0a2e2-2cba-4cc6-866c-26433d5531cd", "routes": [ { "apiRouteID": "c94d3e68-6281-4413-8ae3-b7e4042d1f77", "name": "route1", "enableCors": true, "summary": "Get list of customers", "description": "Get a list of custadrps based on criteria", "method": "get", "tag": "", "path": "/pjstips_09_1", "outputdescription": "", "inputs": [], "outputs": [ { "name": "custData", "type": "object", "children": [ { "name": "custId", "type": "integer", "httpStatus": "200", "example": "" }, { "name": "custName", "type": "string", "httpStatus": "200", "example": "" } ], "multipleValues": true, "httpStatus": "200", "example": "" } ], "subcategory": "", "httpStatuses": [ "200" ], "steps": [ { "text": "Init work variable(s)", "answers": { "plugin": "Program Data:set-work-variable", "work-variable-values": { "custData": "{}", "nbrElm": "0" }, "comment": "" } }, { "text": "Call IBM i program to get list of customer data", "answers": { "plugin": "IBM i:call-ibmi-program", "program-name": "PJSTIPS091", "extractRPGParms": null, "parameter": { "dimension": "1000", "data type": "data structure", "elements": [ { "name": "custId", "type": "packed decimal", "length": 5, "decimals": 0 }, { "name": "custName", "type": "char", "length": 20 } ], "capture output": "Into work variable", "work variable": "custData" }, "parameter_2": { "dimension": null, "data type": "packed decimal", "length": "5", "decimals": "0", "capture output": "Into work variable", "work variable": "nbrElm" }, "parameter_3": { "dimension": null }, "comment": "" } }, { "text": "Truncate unused elems in DS array", "answers": { "plugin": "Custom:code", "custom-code": "custData.length = nbrElm;", "comment": "" } }, { "text": "Set API output", "answers": { "plugin": "Program Data:set-api-output", "httpStatus": "200", "api-output-values": { "custData": "custData" }, "comment": "" } } ] } ] }



  • Click Edit/Design, to bring up the Design view.

  • Click Home/Save As, and save as pjstips_09_1.api.json.

  • You've created a low-code PAPI file, as shown below, with a route named "route1" that does the following:

    1. Call RPG program PJSTIPS091 to get back 2 output parameters.

      1. This is the same program GETCUST3 as documented here on how to use API pjs.toObject() in a PJS module used as an Express web service; whereas in this example, it's called by a PAPI low-code module.

    2. Use custom Node.js code to "truncate" the unused elements so that only the actual elements are output by the PAPI route. Note that the dim as defined in the RPG data structure is the maxinum number of elements. The actual number of elements populated is returned in output parameter nbrElm.

    3. Output the data as returned from the RPG program.



image-20240417-171124.png

Create the example program PJSTIPS091 with source shown below

  • Below is the example RPG code, with 2 output parameters:

    1. A DS array for the customer data. Note that the dim here is the maxinum number of elements.

    2. Another parameter named nbrElm is used to indicate the actual number of elements populated in the DS array.

      PJSTIPS091.rpgle

      D custDs3 ds dim(1000) qualified D custId 5p 0 D custName 20a D nbrElm s 5p 0 D index s 5p 0 C *entry plist C parm custDs3 output C parm nbrElm output nbrElm = 5; // array has 1000 elems, but only 5 elems have actual data for index = 1 to nbrElm; custDs3(index).custId = index; custDs3(index).custName = 'Customer ' + %char(index); endfor; *inLR = *on; return;

Use the Test panel to test the PAPI route

  • The response body should be as shown below.