Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Workflow: how to print Object value ?

stryker129
Mega Guru

Hello,

I'm in progress with ITOM training: https://developer.servicenow.com/app.do#!/training/article/app_store_learn_orchestration_istanbul_c_...

In workflow I've "Log Message" activity - 'cause I'd like to print ${data} object itself. So JSON.stringify(${data}) doesn't work: system logs say that

InformationJSON.stringify([object com.glideapp.workflow.element.WorkflowDatabus])

Gyazo - ec69249a1ab0d896003bda65cb8dec96.png

So what is the best way to easily "print" object value ?

Thanks in advance

4 REPLIES 4

Abhishek Choudh
ServiceNow Employee

I think you will have to print each property of ${data} individually like ${data.get(3).distance} , ${data.get(3).duration}



JSON.stringify can take in javascript object and converts in JSON string. As ${data} is an object of WorkflowDatabus, so JSON.stringify could not evaluate it to JSON string.


${data.get(3).distance} , ${data.get(3).duration} are not the cases.


I would like to "debug" and see ALL DATA which comes from response. How can I do that?


Is there any loop through all elements of this JSON object?


Thanks for reply.


lcbell
Kilo Guru

You can't stringify a non JavaScript object like com.glideapp.workflow.element.WorkflowDatabus. That is probably a Java object. You would have to use a script activity to create a JavaScript object then iterate through all the properties of the Java object and add them to the JavaScript object. Then you could use JSON.stringify to convert it to a JSON encoded string. Here is a small example using GlideRecord as the source:


var gr = new GlideRecord('incident');


gr.initialize();


var JSobj = {};


for (var prop in gr) {


  try{ JSobj[prop] = String(gr[prop] ); } catch (ex) { gs.log(ex); }


}


var str = JSON.stringify( JSobj );//{'prop1':'Hello','prop2':'World'}


gs.log(str);//server side log


congthieu
ServiceNow Employee

You can try the following code in a Run Script activity:



var maxActivityCount = 100;


for (var i = 0; i < maxActivityCount; i++) {


      try {


              var databus = data.get(i);


              workflow.info("[" + i + "] " + JSON.stringify(databus));


    } catch (err) {


              // workflow.info(err.message);


    }


}