Technology Blog Posts by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
NaotoSakai
Product and Topic Expert
Product and Topic Expert
889

SAP Build Process Automationのちょっとしたテクニックを紹介します。

データ型を変換するには?


SAP Build Process Automationでは文字列型や数値型のフィールドを配置してフォームを設計します。たまに「データ型を変換できればよいのに」というシチュエーションに遭遇することがあります。

例えば経費申請のフローで考えると、以下のようなフォームがあるとして




承認者への承認依頼のSubjectを「50000円の経費申請依頼が来ました」のようにこのフォームで入力された金額の数値を使用したいという場合があります。実際やろうとすると


このSubjectの欄には数値型の値を設定することができません。

他にも似たようなシチュエーションは存在するかと思います。数値型から文字列型にデータ型を変換できればなと思った方多いかと思います。



さてどうするか?というのが今回のお題です。2つ方法を紹介します。

1.Automationを利用する


実はAutomationを利用すると非常に簡単にこの型変換を行えます。
まずAutomationの入力・出力を以下の様に設定します。




そしてAutomationの中身は何も設定する必要がありません。


EndのOutput Parametersに設定する値として式エディタを使用し


このようにtoString()関数を引数として入力に定義した数値を設定するだけです。


これで数値型フィールドの値を文字列型に変換できますので


このように承認フォームのSubjectに申請フォームで入力した数値を使用できるようになります。



2.Actionを利用する


次の方法はActionを使用する方法です。Actionを使用するということは何らかの外部APIを使用する必要があります。今回はCloud Foundry Runtime上で動作するNode.jsでAPIを作成します。


非常に単純なAPIです。これはデータベースなどは必要なく、単独で動作します。


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);



(ものはついでなので使用するかわかりませんが逆の文字列型から数値型への変換ロジックをつけてみました。ただし、文字列が数字に変換できなかった場合の処理を入れていませんので参考までに。)




これをActionで使用できるようにするには以下のようなOpen API定義を使用します。


{
"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"
}
}
}
}
}
}
}
}
}
}
}

このActionを使用することで数値型を文字列型に変換することができます。




これで文字列しか設定できないフィールドにフォームの数値型の値を設定できるようになりました。


片方はAutomationを使用する=Desktop Agentを使用するという点、もう片方は別途何処かにRestAPIを作成しなければならないという点がネックになるかと思いますが、どちらの方法でも数値から文字列への変換が可能です。個人的には汎用的に使いそうなロジックですのでActionを使用する方法のほうが良いのではと思います。


ちょっとしたものですが、参考になれば幸いです。