I am running a collection with a CSV file that doesn’t have all the cells filled in. In the situation where the cell is empty, I just want that the {{variable}} to be replaced with an empty string.
My scenario below:
Body - the UserInput and ProjectID are mandatory so the CSV file will always have those filled in but the least are not mandatory so either one or both can be missing:
{
“UserInput”: “{{UserInput}}”,
“ProjectID”: “{{ProjectID}}”,
“ArticleCount”: {{ArticleCount}},
“SegmentCount”: {{SegmentCount}}
}
CSV file:
|UserInput|ProjectID|ArticleCount|SegmentCount|
|bears |440 |1 ||
|bears |440 | ||
|bears |440 | |1|
|bears |440 |2 |3|
In PreRequest Script I want to check if the cell is empty and if it is I want the variables {{ArticleCount}} and {{SegmentCount}} to be filled in accordingly.
The first iteration should have the body as:
{
“UserInput”: “bears”,
“ProjectID”: “440”,
“ArticleCount”: 1,
“SegmentCount”: “”
}
Second is:
{
“UserInput”: “bears”,
“ProjectID”: “440”,
“ArticleCount”: “”,
“SegmentCount”: “”
}
Third is:
{
“UserInput”: “bears”,
“ProjectID”: “440”,
“ArticleCount”: “”,
“SegmentCount”: “1”
}
And last
{
“UserInput”: “bears”,
“ProjectID”: “440”,
“ArticleCount”: “2”,
“SegmentCount”: “3”
}
My PreRequest Script is like this:
if (!pm.iterationData.get(“ArticleCount”)) {
ArticleCount = “”;
console.log(‘inside if ArticleCount’)
}
if (!pm.iterationData.get(“SegmentCount”)) {
SegmentCount = “”;
console.log(‘inside if SegmentCount’)
}
But my code doesn’t replace the variable from the body. So in the first scenario, the call of the body is:
{
“UserInput”: “bears snakes romanian”,
“ProjectID”: “440”,
“ArticleCount”: 1,
“SegmentCount”: {{SegmentCount}}
}
And the second call is:
{
“UserInput”: “bears snakes romanian”,
“ProjectID”: “440”,
“ArticleCount”: {{ArticleCount}},
“SegmentCount”: {{SegmentCount}}
}
So my question is, how do I replace the variables from the body with the ones that I want.
Already tried these:
if (!pm.iterationData.get(“ArticleCount”)) {
pm.iterationData.set(‘ArticleCount ‘,’’);
}
if (!pm.iterationData.get(“ArticleCount”)) {
pm.environment.set(‘ArticleCount ‘,’’);
}
I don’t have ArticleCount or SegmentCount setup as global or anything else
Thank you for your help!