​Condition not working in Assign Message Policy. Is any possibility to include conditional logic in an AssignMessage policy?

vermagovind92
Participant III

Hi All,

I have a usecase where I have to send Json and Xml "Post Request". And I'm Using two JavaScript policies in ProxyEndpoint preflow (one for json and another for xml data) and similarly two AssignMessage Policies in ProxyEndpoint postflow.

Note: Is there any possibility to include conditional logic in an AssignMessage policy to use only one AssignMessage Policy.

And it's partially working when I'm sending Json data in postman body but it's not working when I'm sending Xml data in postman body.

And I also want to increment and decrement a final response by the value of a and b which i sent by postman.(But I'm not able to perform this)

JavaScript code for Json:

var body = request.content.asJSON;
print(body.Add.a);
print(body.Add.b);
body.Add.c = body.Add.a + body.Add.b;
context.setVariable("total",JSON.stringify(body.Add.c));
print(body.Add.c);
context.setVariable(JSON.stringify(body.Add.d));
body.Add.d = body.Add.c + body.Add.a;
print(body.Add.d);
context.setVariable(JSON.stringify(body.Add.e));
body.Add.e = body.Add.c - body.Add.a;
print(body.Add.e);
context.setVariable(JSON.stringify(body.Add.f));
body.Add.f = body.Add.c + body.Add.b;
print(body.Add.f);
context.setVariable(JSON.stringify(body.Add.g));
body.Add.g = body.Add.c - body.Add.b;
print(body.Add.g); 

JavaScript code for Xml:

Note: In place of asXML, I tried XPATH too but geeting same fault error response.

var body = request.content.asXML;
print(body.Add.a);
print(body.Add.b);
body.Add.c = body.Add.a + body.Add.b;
context.setVariable("total",XML.stringify(body.Add.c));
print(body.Add.c);
context.setVariable(XML.stringify(body.Add.d));
body.Add.d = body.Add.c + body.Add.a;
print(body.Add.d);
context.setVariable(XML.stringify(body.Add.e));
body.Add.e = body.Add.c - body.Add.a;
print(body.Add.e);
context.setVariable(XML.stringify(body.Add.f));
body.Add.f = body.Add.c + body.Add.b;
print(body.Add.f);
context.setVariable(XML.stringify(body.Add.g));
body.Add.g = body.Add.c - body.Add.b;
print(body.Add.g); 

AssignMessage code (In ProxyEndPoint postflow for Json data):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-Quote-St">
    <DisplayName>AM-Quote-St</DisplayName>
    <Properties/>
    <Set>
        <Payload contentType="application/json" variablePrefix="@" variableSuffix="#">
{
   "Add": {
      "c": "@total#"
   }
}
        </Payload>
        <QueryParams/>
        <FormParams/>
        <Verb>POST</Verb>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="true" transport="http" type="request"/>
</AssignMessage>

AssignMessage code (In ProxyEndPoint postflow for Xml data):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-Quote-St1">
    <DisplayName>AM-Quote-St1</DisplayName>
    <Properties/>
    <Set>
        <Payload contentType="application/xml" variablePrefix="@" variableSuffix="#">
            <root>
                <Add>
                    <c>@total#</c>
                </Add>
            </root>
        </Payload>
        <QueryParams/>
        <FormParams/>
        <Verb>POST</Verb>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="true" transport="http" type="request"/>
</AssignMessage> 

Now when I'm sending Json body reqest by PostMan , I'm getting Partial correct response.

{
"Add" : {
"a" : 10,
"b" : 20,
"target" : "json"
}

} 

Response is :

{
    "Add": {
        "c": "30"
    }
}

My Expected Output is:

{
    "Add": {
        "c": "30",
	"d": "40",  //(i.e c+a)
	"e": "20",  //(i.e c-a)
	"f": "50",  //(i.e c+b)
	"g": "10"   //(i.e c-b)
    }
}

Now I'm sending XML body reqest by PostMan , I'm getting "error".

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <Add>
      <a>10</a>
      <b>20</b>
      <target>xml</target>
   </Add>
</root> 

Response is :

{
    "fault": {
        "faultstring": "Execution of JavaScript-1 failed with error: Javascript runtime error: \"TypeError: Cannot read property \"Add\" from undefined. (JavaScript-1.js:2)\"",
        "detail": {
            "errorcode": "steps.javascript.ScriptExecutionFailed"
        }
    }
}

My Expected Output:

   <Add>
      <c>30</c>
      <d>40</d>  //(i.e c+a)
      <e>20</e>  //(i.e c-a)
      <f>50</f>  //(i.e c+b)
      <g>10</g>  //(i.e c-b)
   </Add> 

Could someone please help me?

Any Advice what i'm doing wrong here?

Thanks and Regards

Govind Verma

@Anil Sagar @ Google, @Dino-at-Google,@Brendan, @deboraelkin,@Siddharth Barahalikar,@Priyadarshi Ajitav Jena,@Robert Johnson,@Nisha Mallesh,@Anil Sagar,@sudheendras,@Mukundha Madhavan,@Jeremy Whitlock,@Nagashree B,Krishnamurthy

Solved Solved
0 4 2,408
2 ACCEPTED SOLUTIONS

sidd-harth
Participant V

Hi @Govind Verma, what is your exact requirement, I have seen multiple questions from you on similar topics.

Go through the attached proxy for your queries. This is a sample proxy, you need to modify it for production usage.

govind-rev2-2019-04-01.zip

View solution in original post

@Govind Verma

Firstly, you can have a Condition in your step to execute the specific assign message policy based on the Content-Type and Accept header

If your input is JSON and output needs to be in XML, execute the javascript based on content-type and the AssignMessage based on Accept header

<Step>
<Name>JS-Parse-JSON</Name>
<Condition>(request.header.Content-Type = "application/json")</Condition></Step>
<Step>
<Name>AM-Quote-St</Name>
<Condition>request.header.Accept= "application/json")</Condition></Step>

On the response side:

<Step>
<Name>JS-Parse-XML</Name>
<Condition>(request.header.Content-Type = "application/xml")</Condition></Step>
<Step>
<Name>AM-Quote-St1</Name>
<Condition>request.header.Accept= "application/xml")</Condition></Step>

Secondly:

Why do you want two javascripts and Assign-Message policies to handle the request parsing and constructing the response, will you not be able to use JSON-to-XML or XML-to-JSON policy? Example:

On the request side:

<Step>
<Name>XML-to-JSON-1</Name>
<Condition>(request.header.Content-Type = "application/xml")</Condition>
</Step>
<Step>
<Name>JS-Parse-JSON</Name>
</Step>

One the response side:

<Step>
<Name>AM-Quote-St</Name>
</Step>
<Step><Name>Json-to-XML</Name>
<Condition>request.header.Accept= "application/xml")</Condition>
</Step>

View solution in original post

4 REPLIES 4

sidd-harth
Participant V

Hi @Govind Verma, what is your exact requirement, I have seen multiple questions from you on similar topics.

Go through the attached proxy for your queries. This is a sample proxy, you need to modify it for production usage.

govind-rev2-2019-04-01.zip

Thanks for your reply @Siddharth Barahalikar

@Govind Verma

Firstly, you can have a Condition in your step to execute the specific assign message policy based on the Content-Type and Accept header

If your input is JSON and output needs to be in XML, execute the javascript based on content-type and the AssignMessage based on Accept header

<Step>
<Name>JS-Parse-JSON</Name>
<Condition>(request.header.Content-Type = "application/json")</Condition></Step>
<Step>
<Name>AM-Quote-St</Name>
<Condition>request.header.Accept= "application/json")</Condition></Step>

On the response side:

<Step>
<Name>JS-Parse-XML</Name>
<Condition>(request.header.Content-Type = "application/xml")</Condition></Step>
<Step>
<Name>AM-Quote-St1</Name>
<Condition>request.header.Accept= "application/xml")</Condition></Step>

Secondly:

Why do you want two javascripts and Assign-Message policies to handle the request parsing and constructing the response, will you not be able to use JSON-to-XML or XML-to-JSON policy? Example:

On the request side:

<Step>
<Name>XML-to-JSON-1</Name>
<Condition>(request.header.Content-Type = "application/xml")</Condition>
</Step>
<Step>
<Name>JS-Parse-JSON</Name>
</Step>

One the response side:

<Step>
<Name>AM-Quote-St</Name>
</Step>
<Step><Name>Json-to-XML</Name>
<Condition>request.header.Accept= "application/xml")</Condition>
</Step>

Thanks for your reply @Nagashree B