call script action from script include

tanz
Tera Expert

Hi,

 

I have written a script action which is called from the script include via gs.eventQueue {

gs.eventQueue('Test', null, requestData);

} method.

I am trying to update the state from the request data but for some reason the state is not updating

 

This is my script action:

var parsedData = JSON.parse(event.parm1);
var txId = parsedData[0].MessageBody.u_tx_id;
var state = parsedData[0].MessageBody.state;
var grTx = new GlideRecord('x_tool');
grTx.addQuery('u_tx_id', txId);
grTx.addActiveQuery();
grTx.setLimit(1);
grTx.query();
if (grTx.next()) {
    if (JSUtil.notNil(state)) {
        var grState = new GlideRecord('sys_choice');
        grState.addQuery('name', 'x_tool');
        grState.addQuery('element', 'state');
        grState.addQuery('label', state);
        grState.query();
        if (grState.next()) {
            grTx.state = grState.value;
            grTx.update();
        } else {
            gs.info('State not present');
        }
}
}
This is the request data:
 
    
        "MessageBody": {
            "u_tx_id": "MP0001222",
            "state": "Completed"
        },
       
   So based on the TX ID, I want to check the record and update this, but I am unable to do so. Could you please help where I am going wrong with the script
 
 
1 ACCEPTED SOLUTION

@tanz oh okay if this is the format what you wrote is correct .Can you replace it with below script and try once

var parsedData = JSON.parse(event.parm1);
var txId = parsedData[0].MessageBody.u_tx_id;
var st = parsedData[0].MessageBody.state;
var grTx = new GlideRecord('x_tool');
grTx.addQuery('u_tx_id', txId);
grTx.addActiveQuery();
grTx.setLimit(1);
grTx.query();
if (grTx.next()) {
    if (st!="") {
        var grState = new GlideRecord('sys_choice');
        grState.addQuery('name', 'x_tool');
        grState.addQuery('element', 'state');
        grState.addQuery('label', st);
        grState.query();
        if (grState.next()) {
            grTx.state = grState.value;
            grTx.update();
        } else {
            gs.info('State not present');
        }
}
}

 

Hope this helps 

Mark the answer correct if this helps you 

Thanks

View solution in original post

3 REPLIES 3

Mohith Devatte
Tera Sage
Tera Sage

Hello @tanz ,

if your JSON is not stored in an array no need to access it like [0] for accessing first element 

replace like below

 

Just make sure before send the JSON as a event parm1 use JSON.stringify(your_json) 

And then access it like below

var parsedData =event.parm1;
var txId =MessageBody.u_tx_id;
var state = parsedData.MessageBody.state;
 

 

Hope this helps 

Mark the answer correct if this helps you 

Thanks

This is the JSON which is storing in parm 1 of the event from the event logs:

 

[{"MessageBody":{"u_tx_id":"MP0001222","state":"Completed"},"data_topic":"test"}]

But stil the state is not updating

@tanz oh okay if this is the format what you wrote is correct .Can you replace it with below script and try once

var parsedData = JSON.parse(event.parm1);
var txId = parsedData[0].MessageBody.u_tx_id;
var st = parsedData[0].MessageBody.state;
var grTx = new GlideRecord('x_tool');
grTx.addQuery('u_tx_id', txId);
grTx.addActiveQuery();
grTx.setLimit(1);
grTx.query();
if (grTx.next()) {
    if (st!="") {
        var grState = new GlideRecord('sys_choice');
        grState.addQuery('name', 'x_tool');
        grState.addQuery('element', 'state');
        grState.addQuery('label', st);
        grState.query();
        if (grState.next()) {
            grTx.state = grState.value;
            grTx.update();
        } else {
            gs.info('State not present');
        }
}
}

 

Hope this helps 

Mark the answer correct if this helps you 

Thanks