Getting "null" from Data Generator Tool under JSON Message Responder
Hi Team,
I have created a virtual service and it uses Excel Data Source named "CompData". I am have added below script in Incoming Request -> Data Generator Tool, I have created "New_Out_Data" String field (Same name mentioned in 'Data Source Column' also), and added the script in "Scripted" pattern.
`import com.parasoft.api.ScriptingContext
import com.parasoft.api.Application
// The method arguments must be explicitly typed so Parasoft knows what to pass
def generateData(ScriptingContext context) {
Application.showMessage("\n=========In generateData()=========");
// 1. Extract values from the current row of your Excel Data Source
// Format: context.getValue("Data_Source_Name", "Column_Name")
String id = context.getValue("CompData", "No")
String nm = context.getValue("CompData", "Name")
Application.showMessage("id = " + id);
Application.showMessage("nm = " + nm);
// 2. Concatenate the strings with a hyphen
String idName = "${id}"+"${nm}"
Application.showMessage("idnm = " + idName);
// 3. Store the new combined strings back into the context environment
context.setValue("New_Out_Data", idName)
String nod = context.getValue("New_Out_Data")
Application.showMessage("nod = " + nod);
}
`
Note: this is a simple script to check for one field, I will add more calculated fields to this script.
Script is reading the Excel data "No", "Name" but it is returning null instead of the calculated value, could you please advise where I am making the issue? or suggest if I can achieve this using any other Output/Payload tools.
=========In generateData()=========
id = 1
nm = Name1
idnm = 1Name1
nod = null
Thank you so much for your kind help and please let me know if any additional details are needed.
Thanks,
Sati
Best Answer
-
I did some testing and it looks like context.setValue is expecting to set a value into a variable that has been defined in the Responder Suite.
If you got to the Responder Suite, and on the Variables tab define "New_Out_Data" does the script work?
1
Answers
-
Hi,
If I am understanding correctly, the issue is that the script is using setValue and then when using getValue the data that was set is not being returned?
I see the script is printing out a value for "idnm" which is the expected value, but after trying to read it from the context the value is null.
0 -
Dear William,
Thank you so much, it worked after adding the "New_Out_Data" variable to the Responder Suite.
Thanks,
Sati0