スタティック WSDL スクリプトインクルードの例

  • リリースバージョン: Yokohama
  • 更新日 2025年01月30日
  • 所要時間:4分
  • この例は、スタティック WSDL の動作の多くを実装する FakeStockValue スクリプトインクルードを示しています。

    var FakeStockValue = Class.create();
     
    FakeStockValue.prototype = {
      initialize : function(requestXML) {
        //Use some backend XML utilities...you could use string tools if you wish
        this.xmlutil = Packages.com.glide.util.XMLUtil;
        //converting the string to an XML Document
        this.fSoapDoc = new XMLDocument(requestXML);
      },
     
      process : function() {
        var soapBody = this.fSoapDoc.getNode("/Envelope/Body");
        //Our WSDL was formatted to have the only first child element be the function
        var funcNode = this.xmlutil.getFirstChildElement(soapBody);
        var nodeName = this.xmlutil.getNodeNameNS(funcNode);
     
        //If the function for this SOAP request is TradePriceRequest, perform the necessary actions
        if (nodeName == "TradePriceRequest") {
          return this.fakeOutTradePriceRequest(funcNode);
        }
     
        //Couldn't find any supported functions in this SOAP request
        return this.generateSoapFault("un-supported API call: " + nodeName);
      },
     
      fakeOutTradePriceRequest : function (funcNode) {
        //Create the beginnings of our XML response
        var r = new XMLDocument("<GetLastTradePriceOutput xmlns='https://www.service-now.com/vws/FakeStockValue'/>");
     
        //Do the necessary actions here...we're going to get the USER ID of the user
        //used to make this SOAP call.  Then we will return the 
        //stock symbol they were asking about
        var usersysid = gs.getUserID();
        var now_GR = new GlideRecord("sys_user");
        gr.get(usersysid);
        var username = gr.user_name;
        var quoteSymbol = this.xmlutil.getText(funcNode);
        //Create a "message" element to store our response message
        r.createElement("message", username + ", You were looking for a quote on "+quoteSymbol);
        return r.getDocumentElement();
      },
     
     
      generateSoapFault : function (str) {
         var f = "<SOAP-ENV:Fault>" +
                   "<faultcode xsi:type='xsd:string'>SOAP-ENV:FakeStockValue</faultcode>" +
                   "<faultstring xsi:type='xsd:string'>" + str + 
                   "</faultstring>" +
                 "</SOAP-ENV:Fault>"
         var s = new XMLDocument(f);
         return s.getDocumentElement();
      }
    }
    

    initialize 関数

    initialize 関数は XML 要求文字列を受け取り、ライブラリを使用して移動および操作できる XML Document オブジェクトに変換します。XML 要求を文字列のままにして、正規表現を使用して移動することもできます。

    process 関数

    process 関数は、スクリプト利用 Web サービスによって呼び出されます。この関数は、XML 内で本文要素の後の最初の子要素を取得します。WSDL はこの子要素を使用して、使用する関数を決定します。この WSDL には 1 つの関数しかありませんが、ほとんどの WSDL には多くの関数があります。より多くの関数が利用可能な場合、さまざまな関数名の最初の子要素をテストする「if」ステートメントが多くなります。

    falseOutTradePriceRequest 関数

    FakeOutTradePriceRequest 関数は、WSDL で唯一使用できる関数の実装です。この関数は、SOAP 要求で認証されたユーザーを検索して [user_name] を取得し、それを SOAP クライアントに返します。FakeOutTradePriceRequest 関数を拡張して、銘柄コードの検索や前回の取引価格を返すなど、便利なアクティビティを実行できます。

    generateSoapFault 関数

    generateSoapFault 関数は、問題がある場合に呼び出し可能な SOAP エラーを返します。