# How to input variable parameter inside a string in testscript?

**URL:** <https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [November 5, 2020, 2:47pm UTC](https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439 "2020-11-05T14:47:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![immanuelsamuel](https://avatars.discourse-cdn.com/v4/letter/i/54ee81/32.png) [@immanuelsamuel](https://community.postman.com/u/immanuelsamuel)\
**Post date:** [November 5, 2020, 2:47pm UTC](https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439/1 "2020-11-05T14:47:19Z")

</div>

I need to input a parameter inside a string in testscript( eg: {{currentdate}} ), Inorder to verify the date in the response matches the date(variable) that I gave in the request URL  
For example:  
Request: [Google Search](http://www.google.com/search?q=%7B%7Bcurrentdate%7D)}  
Response: "Invalid date q should not be ‘somedate’

I need to check whether the {{currentdate}} parameter which inputs present date is same as the ‘somedate’ displayed in the response

I hope my question is clear, thanks in advance

---

<div class="post-metadata">

**Author:** ![kshatriya.amit.singh](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kshatriya.amit.singh/32/5623_2.png) [@kshatriya.amit.singh](https://community.postman.com/u/kshatriya.amit.singh)\
**Post date:** [November 5, 2020, 3:20pm UTC](https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439/2 "2020-11-05T15:20:42Z")

</div>

Save the {{currentdate}} in your environment variable or global variable, you can use the same in your request header as well as validation in your response.

---

<div class="post-metadata">

**Author:** ![immanuelsamuel](https://avatars.discourse-cdn.com/v4/letter/i/54ee81/32.png) [@immanuelsamuel](https://community.postman.com/u/immanuelsamuel)\
**Post date:** [November 5, 2020, 3:39pm UTC](https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439/3 "2020-11-05T15:39:57Z")

</div>

Thanks Amit, but what happens is when I use it in script. I will be verifying like this

pm.test(“Check errorMessage”, function () {  
var jsonData = pm.response.json();

```
pm.expect(jsonData.errorMessage).to.eql("Invalid date range, \"date\" cannot be after current date {{currentdate}}");

```

the current date value is inside a string so I’m not able to use it in {{currentdate}} format

---

<div class="post-metadata">

**Author:** ![meenakshi.dhanani](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/meenakshi.dhanani/32/5980_2.png) [@meenakshi.dhanani](https://community.postman.com/u/meenakshi.dhanani)\
**Post date:** [November 5, 2020, 4:02pm UTC](https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439/4 "2020-11-05T16:02:38Z")

</div>

Hey @immanuelsamuel,  
You should be able to get the value of the variable current data in the script as `pm.variables.get("currentdate"); `  
And if you want to concat it with a string, you could try the following:

`pm.expect(jsonData.errorMessage).to.eql(`Invalid date range, \"date\" cannot be after current date ${pm.variables.get("currentdate")}`);`

Thanks,  
Meena

---

<div class="post-metadata">

**Author:** ![immanuelsamuel](https://avatars.discourse-cdn.com/v4/letter/i/54ee81/32.png) [@immanuelsamuel](https://community.postman.com/u/immanuelsamuel)\
**Post date:** [November 5, 2020, 5:22pm UTC](https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439/5 "2020-11-05T17:22:07Z")

</div>

Thanks a lot @meenakshi.dhanani, It works flawlessly. I would be requiring more help, I just started creating testscripts in postman. So I think I would be posting more questions. It would be very helpful If you share your knowledge.  
Thanks a lot again.

---

<div class="post-metadata">

**Author:** ![cryosat-observer-223](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@cryosat-observer-223](https://community.postman.com/u/cryosat-observer-223)\
**Post date:** [February 13, 2023, 10:38am UTC](https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439/6 "2023-02-13T10:38:52Z")

</div>

Faced with error message | AssertionError: expected ‘Unexpected number of accounts for acc…’ to deeply equal ‘Unexpected number of accounts for acc…’  
Response body has  
“message”: “Unexpected number of accounts for account number 7777, expected 1 found 0”,  
7777 value is variable and called as {{incorrect\_Customer\_acc}}  
so I make such tets:

var account = pm.variables.get(“incorrect\_Customer\_acc”);  
pm.expect(message).to.eql(‘Unexpected number of accounts for account number {{account}}, expected 1 found 0’);

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 13, 2023, 11:39am UTC](https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439/7 "2023-02-13T11:39:18Z")

</div>

@cryosat-observer-223

I’m assuming that your test is failing?

This is using a JavaScript feature called template literals.

[JavaScript Template Literals (w3schools.com)](https://www.w3schools.com/JS//js_string_templates.asp)  
Scroll down to the section for “Variable Substitutions”.

You reference the variable using ${variableName} not {{variableName}}

```auto
let message = "Unexpected number of accounts for account number 7777, expected 1 found 0"
let account = "7777";

pm.test("Unexpected number of accounts", () => {
    pm.expect(message).to.equal(`Unexpected number of accounts for account number ${account}, expected 1 found 0`);
});

```

Those are back ticks `` around the string.

---

<div class="post-metadata">

**Author:** ![cryosat-observer-223](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@cryosat-observer-223](https://community.postman.com/u/cryosat-observer-223)\
**Post date:** [February 13, 2023, 12:30pm UTC](https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439/8 "2023-02-13T12:30:17Z")

</div>

Still the same error - AssertionError: expected ‘Unexpected number of accounts for acc…’ to deeply equal ‘Unexpected number of accounts for acc…’

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 13, 2023, 12:36pm UTC](https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439/9 "2023-02-13T12:36:30Z")

</div>

@cryosat-observer-223

Did you try the code I pasted, you can see that it works. You just need to transpose the elements for the actual response.

If in doubt, console log the two values, so you can see them side by side.

```auto
console.log(message);
console.log(`Unexpected number of accounts for account number ${account}, expected 1 found 0`)

```

Both lines should be identical.

---

<div class="post-metadata">

**Author:** ![cryosat-observer-223](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@cryosat-observer-223](https://community.postman.com/u/cryosat-observer-223)\
**Post date:** [February 13, 2023, 12:52pm UTC](https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439/10 "2023-02-13T12:52:24Z")

</div>

console.log(‘Unexpected number of accounts for account number ${account}, expected 1 found 0’);  
shows me:  
Unexpected number of accounts for account number ${account}, expected 1 found 0

---

<div class="post-metadata">

**Author:** ![cryosat-observer-223](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@cryosat-observer-223](https://community.postman.com/u/cryosat-observer-223)\
**Post date:** [February 13, 2023, 12:54pm UTC](https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439/11 "2023-02-13T12:54:53Z")

</div>

inverse ` should be used, but I had ’ or "  
NOW it Works  
Thanks @michaelderekjones

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 13, 2023, 1:05pm UTC](https://community.postman.com/t/how-to-input-variable-parameter-inside-a-string-in-testscript/17439/12 "2023-02-13T13:05:51Z")

</div>

It has to be backticks for the template literal to work (which is what allows you to use variable substitutions).

I use this all of the time to create dynamic test case names.
