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!