How can I set a KV Map parameter value in a Javascript??

Not applicable

I have Global Key Value Map in my proxy as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="GlobalKeyValue">
    <DisplayName>GlobalKeyValue</DisplayName>
    <ExpiryTimeInSecs>-1</ExpiryTimeInSecs>
    <InitialEntries>
        <Entry>
            <Key>
                <Parameter>Key</Parameter>
            </Key>
            <Value>ba91d757742d31ee8c5617de528b9dcaa639e459d971ad3ff08145ccb09b15a3b7249afb72508372d28ae0921ede44d787f7a8fce31c6758f4a9007ad9015ce2</Value>
        </Entry>
        <Entry>
            <Key>
                <Parameter>Counter</Parameter>
            </Key>
            <Value>7589027556679898853</Value>
        </Entry>
    </InitialEntries>
    <Get assignTo="Key">
        <Key>
            <Parameter>Key</Parameter>
        </Key>
    </Get>
    <Get assignTo="Counter">
        <Key>
            <Parameter>Counter</Parameter>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

I would want to set the variable 'Counter' in a Javascript. I executed the following but in vain.

context.setVariable("Counter", parseFloat(context.getVariable("Counter")) + 1);

Altough there is no error in the code, the variable is not getting updated either. Am I missing something over here?

2 2 611
2 REPLIES 2

I see one missing attribute on your KeyValueMapOperations policy. I believe it wants a mapIdentifier.

I used the same policy configuration as you, but adding that one attribute, so that it looks like this:

<KeyValueMapOperations name="KVM-Get-1" mapIdentifier='map-1'>
    <Scope>environment</Scope>
    <ExpiryTimeInSecs>-1</ExpiryTimeInSecs>
    <InitialEntries>
     ...
</KeyValueMapOperations>

When I inserted that into an API Proxy, and then inserted a JS policy that increments the counter, it all worked as expected.

My demonstration proxy is attached here.

Deploy it to any org + env, and then invoke it as:

curl -i http://ORG-ENV.apigee.net/kvmtest-2/t1 

You should see an ever-increasing counter.

kvmtest-2.zip

I think it's probably a bug that Edge does not reject deployment of a proxy with a KVMO policy configuration that is missing the mapIdentifier. I will request a change to make Edge more strict about this.

Thanks @Dino for this answer.

Yes you are right. It was missing the mapIdentifier element. Having added that, there is one more issue.

Your code seems to be working fine for smaller numbers. But I am required to deal with larger numbers. i.e., 7589027556679898853.

When I use context.getVariable("Counter"), it returns me a truncated number which cannot be used by me. It results in an Authentication error moving forward.

I exactly need the number 7589027556679898853 to be incremented by 1 to 7589027556679898854 and so on with every iteration. How can I achieve that?