Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
NaotoSakai
Product and Topic Expert
Product and Topic Expert

Here is a tips for SAP Build Process Automation.

How to convert data types?


In SAP Build Process Automation, we design forms by placing fields of string and numeric types. I sometimes encounter situations where I wish I could convert the data type.
For example, let's consider the following form in an expense application flow

I may want to use the numeric value of the amount entered on this form for the Subject of the approval request to the approver, such as "You have a $1000 expense approval request received". If you actually try to do this, you will have to do the following.

It is not possible to set a value of numeric type in this Subject field.
There may be other similar situations. Many of you may have wished you could convert the data type from numeric to string.

What should we do? Here are two methods.

 

1. Use Automation


In fact, this type conversion can be done very easily using Automation.
First, set up the Automation input and output as follows




And nothing in Automation needs to be set.


Use the Expression Editor as the value to set for the Output Parameters of End and


Simply set the numeric value defined for the input with the toString() function as an argument like this.


This will convert the value of a numeric type field to a string type, so


In this way, the number entered in the application form can be used for the Subject of the approval form.



2. Use Action


The next method is to use an Action, which means that an external API of some kind must be used. In this case, I will create an API in Node.js running on the Cloud Foundry Runtime.


It is a very simple API. It does not require a database, etc., and works on its own.


var express = require("express");
var router = express();
var cors = require("cors");

// Allow CORS
router.use(cors());
router.use(express.json());

router.post('/numbertostring', (req, res) => {
console.log("numbertostring in");
console.log("Value="+ req.body.input);
var returnvalue={};
returnvalue.returnvalue = String(req.body.input);
res.json(returnvalue);
})
router.post('/stringtonumber', (req, res) => {
console.log("stringtonumber in");
console.log("Value="+ req.body.input);
var returnvalue={};
//NOTE : This part should originally describe the logic for the case where conversion to a numeric type is not possible.
returnvalue.returnvalue = Number(req.body.input);
res.json(returnvalue);
})
router.listen(process.env.PORT || 4000);



(I don't know if you will use this one since it is just for the sake of it, but I added the reverse string-to-number conversion logic. but I have not included a process for when a string cannot be converted to a number, just for reference.)

To make this available to Action, use the following Open API definition.

{
"openapi": "3.0.0",
"info": {
"title": "String to Number and Number to String Converter",
"version": "1.0.0",
"description": "A sample API to convert string to number and number to string."
},
"paths": {
"/stringtonumber": {
"post": {
"summary": "Converts a string to a number",
"requestBody": {
"description": "Input JSON object containing the string to be converted to a number",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"input": {
"type": "string",
"example": "123"
}
},
"required": ["input"]
}
}
}
},
"responses": {
"200": {
"description": "Successful response with the converted number",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"returnvalue": {
"type": "number",
"example": 123
}
}
}
}
}
}
}
}
},
"/numbertostring": {
"post": {
"summary": "Converts a number to a string",
"requestBody": {
"description": "Input JSON object containing the number to be converted to a string",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"input": {
"type": "number",
"example": 123
}
},
"required": ["input"]
}
}
}
},
"responses": {
"200": {
"description": "Successful response with the converted string",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"returnvalue": {
"type": "string",
"example": "123"
}
}
}
}
}
}
}
}
}
}
}

This Action can be used to convert numeric types to string types.




Now you can set a numeric value of a form to a field that can only be set to a string.





I think the bottleneck is that one uses Automation = Desktop Agent, and the other requires a separate RestAPI to be created somewhere. Personally, I think the method using Action is better since the logic is likely to be used universally.


This is just a small thing, but I hope it helps.