You cannot reference variables defined in your javascript module, from other policies.
It can seem confusing at first, but the way to think about it is this: the scope of variables you use in JavaScript is limited to the JavaScript callout. There is an entirely different scope of variables available through the flow of a message - this is called the “Message Context” in Apigee documentation. Policies like RaiseFault, AssignMessage or Quota can read or write variables from or into the message context.
To set a value into the message context from a JS module, you do this:
var var1="hi";
context.setVariable('hiVariable', var1);
// same as
context.setVariable('hiVariable', 'hi');
…and then you can reference that variable, ‘hiVariable’ in a policy like RaiseFault.
When client does ?wsdl I need to retrieve the wsdl contents from wsdl script file and respond with wsdl contents.I was able to do with RaiseFault by pasting wsdl contents in it.But there is a catch, the support team can paste any wsdl but our code should change the soap address location in the wsdl to fixed hostname and respond back
yes - well first, I would not use RaiseFault for that purpose, as it does not seem to be a fault. It’s a correct, normal response. So instead I would recommend that you use AssignMessage.
You may want to keep the WSDL file separate, in which case I suggest you store it in the KVM. It’s unfortunate that a policy is not able to retrieve a resource associated with the org (like a WSDL file etc). It would be a nicer model, I think. I’ll see if we can add that feature.
The last thing you mentioned is an address replacement. For that I suggest you use JavaScript or XSLT to modify the WSDL file. XSLT might be easier since WSDL is all XML. There is just one, or maybe just a handful of locations you would need to modify. The XSLT would be perfect for that, and should be really simple.
Probably the “new” replacement address should also be stored in the KVM, and that value passed as a parameter to the XSLT.
We are now very very far from your original question, which is “how can I set a variable into the Message Context from JavaScript”? So if you have further questions, please ask a new question.