Scripting Issue: Trying to get a particular value in a column
I have a script where I generate a key based on the request. This key is a value listed in the first column in a table, and the script should return the value of the corresponding value next to the key value. Ex: In column 1 my key is A and I need to return the value next to it BC.
So far my script results in an error when ran, saying there is no column named "the actual key value"
How can I fix this?
Here is my code:
function getDocumentUId(input, context){
var platformDocumentUId;
var key = context.getValue("Generated Data Source","docCategoryCode")+context.getValue("Generated Data Source","docTypeCode")+context.getValue("Generated Data Source","docSubCode");
platformDocumentUId = context.getValue("ValueMap", key);
if (platformDocumentUId == null)
{
platformDocumentUId="nil"+"|"+key;
}
//return "3AACE39674C74F85B944A448B0557DFF";
return platformDocumentUId;
}
Also when I call on this value, platformDocumentUId, do I just call it as ${platformDocumentUId} or do I need to capture it in an XML databank first?
Comments
-
Hi,
Would it make sense to instead enable tools chained to the incoming request to alter the request before data source correlation so that your generated key could be used during data source correlation? Then the correlation would match the generated key to the column in the data source and set the appropriate row to be used by the response payload.0 -
I dont quite follow, what tools would you recommend to use?
0 -
Hi,
I am suggesting that your alter the incoming request so that it includes your generated value, then you could use a data source correlation to select the expected row in the data source.See this in the documentation, find "Allow incoming" in the page
https://docs.parasoft.com/display/SOAVIRT9104/Message+Responder+OverviewThen chain an extension tool to the request with the script you created and have it modify the request.
0 -
so enable alter the incoming request option on the responder and keep the extension tool code the same, or does the code need further modification, do you think?
0 -
Hi,
You probably need to change the script to modify the request. The way it is now it will replace the existing incoming request with just the "platformDocumentUId" which I don't know if that is an issue in your case.0 -
I need it to get various values from the request, put them together as a key which exists as a value in one of the columns in a table and retrieve the value next to that key value in the table. That value will be returned as platformDocumentUId which will be passed off as a single value to the response.
0 -
Hi,
You might want to use a data bank to extract all the desired values from the request, then have your script access those data banked columns to create the key that will be used for correlation.0 -
Well the current code works in forming the key together but rather the error keeps displaying that there is no column titled "20134234" or whatever the key value is. So I just need it to look in the column next to it and find the value to the right of that key value
0 -
Good morning,
It seems that you're almost there. Data source correlation needs to be configured to use the column name that contains the key value. I can write up an example for you. Is the request a JSON or XML payload?
0 -
Hey Omar, it is XML
0 -
I also wanted to add, in relation to the screenshot you attached to your comment, I would be getting that key value and I would retrieve that value next to it so like if the values I create in the request is 2013, it would respond with 20134234
0 -
Also the key is pieced together from several elements put together in a single value, which like I said, the code does that already
0 -
I attached an example of your use case. I provided a scripted solution and an xpath solution for you.
The scripted solution retrieves the data bank values and combines them into a single value or key. Your script should already do this. The script will return the key in XML to allow you to use it for data source correlation.
Example:<root>123456789</root>
This payload is added to the "request body" section of the data source correlation and configured to use the column name from your table. Also note the option to "Allow incoming request tools to modify....." is enabled.The xpath solution is a bit simpler. The concat function in xpath is used in an XML transformer to concatenate the necessary values into a single key.
Example:(concat(/root/docCategoryCode/text(),/root/docTypeCode/text(),/root/docSubCode/text()))
It also concatenates XML tags around the key to allow you to use it for data source correlation.
Example:concat("<root>",/root/docCategoryCode/text(),/root/docTypeCode/text(),/root/docSubCode/text(),"</root>")
You can evaluate the xpath to see the XML payload produced at runtime. Again, this payload is added to data source correlation and the option to "Allow incoming request tools to modify....." is enabled.Take a look at the attached artifacts and let us know if you have any questions.
1 -
Hey Omar, unfortunately I cannot open that file, it is 7zip, which I do not have on my machine. Could your try saving it as a regular zip instead?
0 -
-
It is working now, thanks everyone
0 -
The solution was to use the .indexOf function and pointing to the columns correctly:
ArrayList keys = context.getValues("ValueMap","A") ArrayList docIDs = context.getValues("ValueMap","B") int index = keys.indexOf(key) platformDocumentUId = docIDs.get(index)
1 -
Glad to hear that you were able to get it working!
0