AnsweredAssumed Answered

Batch Operation Sub-Request not working

Question asked by jmalmeida Champion on Feb 25, 2019

Hi folks.

 

I am trying to copy a value from one attribute to another, based on a template definition, using NodeJS and PI Web API's Batch request.

(Yes, I can use an Analysis to do this, but I do not want to).

 

I have made this try:

 

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
const database = 'DATABASE'
const server = 'SERVER'
const domain = 'ENTERPRISE.BIZ'
const element_filter = 'Template:=\'TEMPLATE\''
const attr_from_namefilter = 'ATTRIBUTE_FROM'
const attr_to_namefilter = 'ATTRIBUTE_TO'

var request = require('request-json')
var srvurl = 'https://'+server+'.'+domain+'/'
var apiurl = srvurl + 'piwebapi/'
var request = require('request-json')
var client = request.createClient(srvurl)
client.setBasicAuth('USERNAME', 'PASSWORD');

var tripa = {}
tripa['db'] = {}
tripa['db'].Method = 'GET'
tripa['db'].Resource = apiurl + 'assetdatabases?path=\\\\' + server + '\\' + database + '&selectedFields=WebId'
tripa['el'] = {}
tripa['el'].Method = 'GET'
tripa['el'].ParentIds = ['db']
tripa['el'].Resource = apiurl + 'elements/search?databaseWebId={0}&query=' + element_filter + '&selectedFields=Items.WebId;Items.Name'
tripa['el'].Parameters = ['$.db.Content.WebId']
client.post(apiurl + '/batch',tripa).then( (result) => {
    tripa = {}
    result.body.el.Content.Items.forEach(element => {
        tripa[element.Name+'_FROM'] = {}
        tripa[element.Name+'_FROM'].Method = 'GET'
        tripa[element.Name+'_FROM'].Resource = apiurl + 'streamsets/' + element.WebId + '/value?namefilter=' + attr_from_namefilter + '&selectedFields=Items.Value.Value;Items.WebId'
        tripa[element.Name+'_TO'] = {}
        tripa[element.Name+'_TO'].Method = 'GET'
        tripa[element.Name+'_TO'].Resource = apiurl + 'streamsets/' + element.WebId + '/value?namefilter=' + attr_to_namefilter + '&selectedFields=Items.WebId'
        tripa[element.Name+'_WRITE'] = {}
        tripa[element.Name+'_WRITE'].Method = 'POST'
        tripa[element.Name+'_WRITE'].ParentIds = [element.Name+'_TO', element.Name+'_FROM']
        tripa[element.Name+'_WRITE'].Resource = apiurl + 'streams/{0}/value'
        tripa[element.Name+'_WRITE'].Content = JSON.stringify( { Value: '{1}' } )
        tripa[element.Name+'_WRITE'].Parameters = [ '$.' + element.Name + '_TO.Content.Items[0].WebId', '$.' + element.Name + '_FROM.Content.Items[0].Value.Value']    })
    client.post(apiurl + '/batch',tripa).then( (result) => {
        for(var x in result.body) {
            console.log(x)
            console.log(result.body[x])
        }
    }).catch( (error) => { console.error(error) })
}).catch( (error) => { console.error(error) })

 

But I get 400 HTTP error on the post operation with message "Value Incompatible with target".

It seems that the JSONPath is not rendering the '{1}' token at the Content property.

 

If I change to

        tripa[element.Name+'_WRITE'].Content = JSON.stringify( { Value: 0 } )

It works (but for obvious reasons I do not want to set this value to 0, I want to copy the value).

Outcomes