Skip to Content
0
Sep 20 at 05:52 AM

By using python ABAP logic validation in SAP ABAP

103 Views

Hello Experts,

Actually I have CAPM application using Node JS. So there I used child_process to call python script into my execution logic.

This is my execution logic.

Controller.ts

import { callPython } from "../../srv/util/pythonCaller"

const result = await callPython("rules", "validate", [oATable]);

pythonCaller.ts

import { spawn } from "child_process";
const pythonDir = (__dirname + "/../pythonCode/"); // Path of python script folder
const python = pythonDir + "pythonEnv/bin/python"; // Path of the Python interpreter
/** remove warning that you don't care about */
function cleanWarning(error) {
 return error.replace(/Detector is not able to detect the language reliably.\n/g, "");
}
function callPython(scriptName,functionName, args) {
 return new Promise(function (success, reject) {
 const pyArgs = ["-c", `import ${scriptName}; print(${scriptName}.${functionName}(${JSON.stringify(args)}))`]
 const pyprog = spawn("py", pyArgs);
 let result = "";
 let resultError = "";
 pyprog.stdout.on('data', function (data) {
 result += data.toString();
 });
 pyprog.stderr.on('data', (data) => {
 resultError += cleanWarning(data.toString());
 });
 pyprog.stdout.on("end", function () {
 if (resultError == "") {
 success(result);
 } else {
 const error = new Error(resultError);
 console.error(error);
 reject(resultError);
 }
 })
 });
}
export { callPython } 

rules.py (Some basic validation)

import sys, json, sys
def validate(arr):
    errMsg = []
    
    for x in arr:
            if x['knumh'] == '':  
                errMsg.insert(0, "Condition Record Number cannot be empty")
    errorData = { 'errorMessages': errMsg}
    print(json.dumps(errorData))

 

I did some basic validations by sending data to python script and return data to controller.ts file.
Here my question is Can I replicate the same in SAP ABAP class? Please share your thoughts.

Thanks in Advance.