Questetra BPMS: Process, List All File URLs

Lists the download URLs for the files stored as workflow data of Questetra BPM Suite. If two files are attached, output two lines of text. This is useful for browser extensions and email text attachment.

2019-11-05 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/questetra-bpms-process-list-all-file-urls/

Configs
  • A: Select OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • B: Set WF Platform URL (${var[applicationRoot]} or https://../) * #{EL}
  • C: Set Process ID ( e.g., “123” ) * #{EL}
  • D: Select STRING DATA for URL List (update)
  • E: Select STRING DATA for URL List (new lines added)
Script
// about Questetra Workflow API
// See https://online-demo-ja.questetra.net/s/swagger/index.html?urls.primaryName=Workflow%20API
// or "https://{YOUR}.questetra.net/s/swagger/index.html"

// about Questetra OAuth2 (for System Admin)
// Get ID&Secret: System Settings > API Clients > OAuth2 Clients > Add OAuthw Client
// (config "Redirect URL" = "https://s.questetra.net/oauth2callback" )
// (Scope: "read" or "any" )
// Set ID&Secret: Workflow App > Detail > OAuth2 Setting > (Get Refresh Token)


//////// START "main()" ////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const oauth2          = configs.get( "conf_OAuth2"  )   + "";  // required
const applicationRoot = configs.get( "conf_ApplicationRoot" ) + "";  // required
const processId       = configs.get( "conf_ProcessId" ) + "";  // required
const dataIdD         = configs.get( "conf_DataIdD" )   + "";  // not required
const dataIdE         = configs.get( "conf_DataIdE" )   + "";  // not required
// 'java.lang.String' (String Obj) to javascript primitive 'string'

engine.log( " AutomatedTask Config: Application Root: " + applicationRoot );
engine.log( " AutomatedTask Config: Process ID: " + processId );


//// == Data Retrieving / ワークフローデータの参照 ==
let urlTextSource = engine.findDataByNumber( dataIdE );

//// == Calculating / 演算 ==
// obtain Access Token
const token  = httpClient.getOAuth2Token( oauth2 );

// get ProcessInstance via Questetra Workflow API
// com.questetra.bpms.core.event.scripttask.HttpClientWrapper
let apiRequest = httpClient.begin(); // HttpRequestWrapper
    apiRequest = apiRequest.bearer( token );
    apiRequest = apiRequest.queryParam( "processInstanceId", processId );
const apiUri = applicationRoot + "API/OR/ProcessInstance/view";

// request to API
engine.log( " AutomatedTask Trying: GET " + apiUri );
const response = apiRequest.get( apiUri );
const responseCode = response.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse: Status " + responseCode );
if( responseCode !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
         responseCode + "\n" + response.getResponseAsString() + "\n" );
}
const responseStr = response.getResponseAsString() + "";
const responseObj = JSON.parse( responseStr );
//engine.log( responseStr );

// retrieve JSON
let urlText = "";
for( const key in responseObj.processInstance.data ){
  if( responseObj.processInstance.data.hasOwnProperty(key) ) {
    engine.log( key + ": " + responseObj.processInstance.data[key].dataType );
    if( responseObj.processInstance.data[key].dataType === "FILE2" ){
      if( responseObj.processInstance.data[key].value == null ){
        engine.log( " AutomatedTask Json: FILE not attached" );
      }else{
        engine.log( " AutomatedTask Json: #FILE " + 
                   responseObj.processInstance.data[key].value.length );
        for(let i = 0; i < responseObj.processInstance.data[key].value.length; i++ ){
          urlText += applicationRoot + "PE/Workitem/File/download?id=" + 
             responseObj.processInstance.data[key].value[i].id + 
             "&processDataInstanceId=" +
             responseObj.processInstance.data[key].value[i].processDataInstanceId + "\n";
        }
      }
    }
  }
}
urlText = urlText.replace(/[\n]*$/, "");


//// == Data Updating / ワークフローデータへの代入 ==
if( urlText === "" ){
  engine.log( " AutomatedTask DataUpdating: FILE DATA not found" );
}
if( dataIdD !== "" ){ 
  engine.setDataByNumber( dataIdD, urlText );
}
if( dataIdE !== "" ){ 
  if( urlTextSource !== null ){
    engine.setDataByNumber( dataIdE, urlTextSource + "\n" + urlText );
  }else{
    engine.setDataByNumber( dataIdE, urlText );
  }
}


} //////// END "main()" ////////

Download

Capture

Lists the download URLs for the files stored as workflow data of Questetra BPM Suite. If two files are attached, output two lines of text. This is useful for browser extensions and email text attachment.

Notes

  1. Use with “Service Task (Data Assignment)” and “Multiline String, Get Specific Line” etc.

See also

1 thought on “Questetra BPMS: Process, List All File URLs”

  1. Pingback: Questetra BPMS: Process, Concat All Strings – Questetra Support

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: