Get transaction id in script from postman.

Ruchi9
Mega Contributor

Hi,
I am trying to get transaction id in my script from postman when api is triggered.

Tried this but it's giving undefined.
var reqheader = request.headers;
var transaction_id= reqheader.transaction_id;
gs.log('Test transaction id '+transaction_id);

I found this code somewhere so not sure if this is the correct backend name of transaction id. In that case how can I get the backend name of this field 'X-Transaction-ID' ?


find_real_file.png

3 REPLIES 3

Allen Andreas
Administrator
Administrator

Hi,

You can use this method:

getAllHeaders()

And loop through them to get the information such as:

var headers = request.getAllHeaders();
for(var i in headers){ //if the getAllHeaders was associated to the "headers" JavaScript variable
if (headers[i].name == "X-Transaction-ID") {
  gs.info(headers[i].name + ': ' + headers[i].value);
 }
}

For example

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen,

Thanks for the reply.

I tried the above logic in my scripted rest api but it's not working.
I gave log inside for statement but it seems it's not running at all as i got no logs.

Also tried request.getHeader('X-Transaction-ID'); but this is giving null

but request.getHeader('Content-Type'); is giving the expected value.

Is it because transaction id is generated everytime api is triggered ?

Hi,

Yes, the transaction ID is generated every time the API is triggered, but as far as it not working, unfortunately, you didn't supply that section of code so I could take a look. To give documentation/support towards what I supposed, here's this link: https://developer.servicenow.com/dev.do#!/reference/api/paris/server/sn_ws-namespace/c_RESTResponseV...

From here, I modified that code to focus on the one header you were wanting to use and with that, I personally tested it and it does work.

So I think there may be a small adjustment you need to make to get it to work, but you can use the link above to test sending it even in background script.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!