// GraalJS Script (engine type: 2)
// Batch to Get Accounts from Salesforce via Salesforce REST API (ver. 50, Winter'21)
//
// OAuth2 config
// Authorization Endpoint URL: https://login.salesforce.com/services/oauth2/authorize
// Token Endpoint URL: https://login.salesforce.com/services/oauth2/token
// Scope:
// Consumer Key: (Get by Salesforce Connected App screen)
// Consumer Secret: (Get by Salesforce Connected App screen)
//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){
//// == 工程コンフィグの参照 / Config Retrieving ==
const oauth2 = configs.get( "conf_OAuth2" ); //required
const subDomain = configs.get( "conf_SubDomain" ); //required
const strQuery = configs.get( "conf_Query" ); //required
const objectList = configs.getObject( "conf_DataIdD" ); //required
const strTopKey = configs.get( "conf_DataIdE" ); //required
const strConfigJsonPaths = configs.get( "conf_DataIdF" ); //required
const strResponse = configs.getObject( "conf_DataIdG" );
const decRecordCount = configs.getObject( "conf_DataIdH" );
//// == ワークフローデータの参照 / Data Retrieving ==
// (Nothing. Retrieved via Expression Language in Config Retrieving)
//// == 演算 / Calculating ==
const uri = "https://" + subDomain + ".salesforce.com/services/data/v50.0/query/";
const response = httpClient.begin()
.authSetting(oauth2)
.queryParam("q", strQuery)
.get(uri);
engine.log( " AutomatedTask ApiRequest Start: " + uri );
const responseCode = response.getStatusCode() + ""; // (primitive string);
const responseBody = response.getResponseAsString() + "";
engine.log( " AutomatedTask ApiResponse Status: " + responseCode );
if( responseCode !== "200" ){
throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
responseCode + "\n" + responseBody + "\n" );
}
if( strResponse !== null){
engine.setData( strResponse, responseBody );
}
const objJsonText = JSON.parse( responseBody );
// set key for getting data
const arrStrConfigJsonPaths = strConfigJsonPaths.split(",");
let arrJsonPathValues = new Array(objJsonText[strTopKey].length);
engine.log( " AutomatedTask: # lenght of arrJsonPathValues: " + arrJsonPathValues.length );
// setup List Type data item
let table = engine.findData(objectList);
table = objectList.createListArray();
//engine.log("responseBody: " + responseBody );
// parse json
for (let i=0; i < objJsonText[strTopKey].length; i++){
arrJsonPathValues[i] = new Array(arrStrConfigJsonPaths.length);
let row = table.addRow();
for (let j=0; j < arrStrConfigJsonPaths.length; j++){
let strConfigJsonPathAndCol = arrStrConfigJsonPaths[j].split(":");
let arrSubKey = strConfigJsonPathAndCol[0].split(".");
let obj = objJsonText[strTopKey][i];
for (let k=0; k < arrSubKey.length; k++){
obj = obj[arrSubKey[k]];
}
if (obj === null){
arrJsonPathValues[i][j] = "";
}else{
arrJsonPathValues[i][j] = obj;
}
// engine.log("arrJsonPathValues[" + i + "][" + j+ "]" + arrJsonPathValues[i][j]);
row.put(strConfigJsonPathAndCol[1] , arrJsonPathValues[i][j].toString() );
}
}
//// == ワークフローデータへの代入 / Data Updating ==
// テスト用ログ表示
for (let i=0; i < arrJsonPathValues.length; i++){
for (let j=0; j < arrJsonPathValues[i].length; j++){
engine.log("arrJsonPathValues[" + i + "][" + j + "]=" + arrJsonPathValues[i][j] );
}
}
engine.setData(objectList, table);
engine.setData(decRecordCount, new java.math.BigDecimal(objJsonText[strTopKey].length) );
if( strResponse !== null){
engine.setData( strResponse, responseBody );
}
} //////// END "main()" /////////////////////////////////////////////////////////////////