Dynamic Payload in Assign Message

I have an Assign message as which has payload. This AM then calls target endpoint.

Payload is set in AM, where data is retrieved using a query. The where clause in the query is dynamic and changes as per the queryparam we pass to the API endpoint.

For example, if I pass queryparam as 'emplid' then the where clause in the query in the payload should be 'WHERE EMPLID = '{request.queryparam.emplid}''

if I pass queryparam as 'FIRST_NAME' then the where clause in the query in the payload should be 'WHERE FIRST_NAME= '{request.queryparam.firstname}''

So the where clause must be dynamic based on queryparam.

For now, if I have ten queryparams then, I have to write ten different AM and each AM has to called based in some condition.

Is there a way where I can make it more generic and dynamic. Below is my AM

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-GetEmplID">
    <DisplayName>AM-GetEmplID</DisplayName>
    <Properties/>
    <Set>
        <Headers>
            <Header name="Authorization">Bearer {WDAccess_token}</Header>
        </Headers>
        <Payload contentType="application/json">
                {
    "query" : "SELECT ASOFDATE, EMPLID, EMPL_RCD, NAME_PREFIX, FIRST_NAME, LAST_NAME cds_WD_MMC_ActiveEE_TBL WHERE EMPLID = '{request.queryparam.emplid}'"
    }
        </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo type="request" createNew="false" transport="http"/>
</AssignMessage>

 

 

 

 

 

Solved Solved
0 4 431
1 ACCEPTED SOLUTION

Sounds like a perfect use case for a JavaScript Policy. Using JavaScript policy in front of your AM policy, you can effectively and flexibly generate dynamic fragments of the payload using the full might of javascript and set them into context variable(s).Then AM would reference those context variables using {} syntax.

View solution in original post

4 REPLIES 4

Sounds like a perfect use case for a JavaScript Policy. Using JavaScript policy in front of your AM policy, you can effectively and flexibly generate dynamic fragments of the payload using the full might of javascript and set them into context variable(s).Then AM would reference those context variables using {} syntax.

Hi Yuri,

Do you have any sample code for this test case ?

Regards,
Pravin

A good approach to find a sample code would be to search across github.

This repo is especially appropriate for browsing to study Apigee DSL https://github.com/apigee/api-platform-samples

A search for assignmessage+payload keywords https://github.com/apigee/api-platform-samples/search?q=assignmessage+payload gives me an example of usage context variable in a payload, variable called output-jwt

https://github.com/apigee/api-platform-samples/blob/5b67fe2c3ab23514b67d458a19b63159a2e3f2ab/perf-pr...

A search for context.setvariable keyword https://github.com/apigee/api-platform-samples/search?q=context.setvariable returns plenty of examples of javascript code and its interaction with a proxy context

For example, for more complex javascript with functions and branching, see here

https://github.com/apigee/api-platform-samples/blob/ce9f88832485e9a1ccbced6f1bdad79c2a8b08ac/sample-...

Adding 2 and 2 together, you use those policies as an inspiration to create yours JS and AM polices that work together in concert.

 

This was resolved using JavaScript. The queryparams were fetched using request.querysting and then the logic was written to get each parameter and pass it on to the WQL.