apigee

how to read variable in assign message policy in APIGee


In Apigee

given a variable in javascript policy, example: var value = 123;

how to get this variable in the assign message policy?

by using {a} in the payload message is not reachable


Solution

  • For the variable to be available for use in any Apigee policy, you first have to make it a flow variable. You can use Apigee's JavaScript Object Model method called context to to this (see ref here). In your JS policy, you have var {some-value} = 123;. To make this available as a flow variable,introduce this line in your JS policy after the variable assignment: context.setVariable("preferred-variable-name", some-value);

    You can now access this preferred-variable-name in your AM policy as show below:

    <AssignVariable>
        <Name>preferred-variable-name</Name>
        <Ref>preferred-variable-name</Ref>
    </AssignVariable>
    

    Just to mention it is possible to pass the value directly in the policy as well as shown below

    <AssignVariable>
      <Name>variable_name</Name>
      <Value>123</Value>
    </AssignVariable>
    

    Read more here.

    The <Name> tag can be set to any name.