Aside from using Javascript, how can I concatenate a string with a variable and assign it to a new variable?

Not applicable

Already tried using Assign Message policy but apparently we cannot use Assign Variable to concatenate string with a variable. I tried doing it with the <Ref> tag.

Solved Solved
0 6 3,517
1 ACCEPTED SOLUTION

New (better) answer to an old question.

You can now concatenate within AssignMessage/AssignVariable, using the new Template element.

This is available in Apigee Edge Saas as of November 2018, and will be in OPDK in the January release, 19.01 (I believe).

Example:

<AssignMessage name="AV-1">
  <AssignVariable>
    <Name>name_of_variable_to_assign_here</Name>
    <Template>Hello {request.queryparam.greeting}</Template>
  </AssignVariable>
</AssignMessage>

View solution in original post

6 REPLIES 6

@Ellaine Go , Welcome to Apigee Community !

Ideally, AssignMessage Policy Assign Variable element should support value with reference elements using curly brace notation. But unfortunately, It's not supported as of today & you have to use Ref tag to reference variables.

There is a work around, You can create a temporary query param or header to store concatenated value & remove it later.

For Example,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Properties/>
    <Set>
        <QueryParams>
            <QueryParam name="temp">Hello {request.queryparam.greeting}</QueryParam>
        </QueryParams>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

Remove it after you use it where ever you need,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-2">
    <DisplayName>Assign Message-2</DisplayName>
    <Properties/>
    <Remove>
        <QueryParams>
            <QueryParam name="temp"/>
        </QueryParams>
    </Remove>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

I would suggest to use Javascript / Java / Python to do the job instead of using 2 Assign Message policies. Hope it helps.

@Anil Sagar @ Google

is it still same work around or is there any new solution to

concatenate a string with a variable

Hi Bob

Yes, there is a new (better) solution. There's a new "Template" element in the AssignMessage/AssignVariable that allows you to assign to a variable, based on a message template.

Here's a simple example:

<AssignMessage name="AV-1">
  <AssignVariable>
    <Name>variable_name_here</Name>
    <Template>Hello {request.queryparam.greeting}</Template>
  </AssignVariable>
</AssignMessage>

For a more complex example, with multiple variables and a function:

<AssignMessage name="AV-1">
  <AssignVariable>
    <Name>isoFormatString</Name>
    <Value>yyyy-MM-dd'T'HH:mm'Z'</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>name_of_variable_to_assign_here</Name>
    <Template>Hello {request.queryparam.greeting}, at {timeFormatUTCMs(isoFormatString,system.timestamp)}</Template>
  </AssignVariable>
</AssignMessage>

Why not just use JavaScript?

You could also use a single Assign Message to create a new message variable

<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-variable-template">
    <DisplayName>AM-variable-template</DisplayName>
    <Set>
        <Payload>Hello {request.queryparam.greeting}</Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo type="request" transport="http" createNew="true">variable</AssignTo>
</AssignMessage>

Then reference later as "variable.content".

New (better) answer to an old question.

You can now concatenate within AssignMessage/AssignVariable, using the new Template element.

This is available in Apigee Edge Saas as of November 2018, and will be in OPDK in the January release, 19.01 (I believe).

Example:

<AssignMessage name="AV-1">
  <AssignVariable>
    <Name>name_of_variable_to_assign_here</Name>
    <Template>Hello {request.queryparam.greeting}</Template>
  </AssignVariable>
</AssignMessage>