Using Dynamic WS to consume a LWS in a script

Here is a new solution to call SOAP Web Services from a Smart Office script that complements the previous known solutions. It’s a very easy and fast way to call LWS and does not require you to write any C# or XML.

It’s using a private API in Smart Office so it might change in future releases, without any announcement.

Here’s the result:

2

To consume Lawson web services in a script the WSDataService class is used. The class already exist in Smart Office and its placed in the Mango.Core.DynamicWs namespace.

It has a static method that does all the work for you:

public static void CallAsync(
  string wsdl,
  string contractName,
  string operationName,
  Dictionary<string, object=""> parameters,
  Uri address,
  WSDataService.WSDataServiceCallbackDelegate callbackDelegate,
  bool useBasicHttp = false,
  string userName = null,
  string password = null,
  string maxReceivedMessageSize = null)

And here’s an example when using the method:

import Mango.Core.DynamicWs;
package MForms.JScript {
  class WebserviceTest
  {
    public function Init(element: Object, args: Object, controller : Object, debug : Object)
    {
      var p = new TypeObjectCollection().GetValues();
      p.Clear();
      p.Add("W1CUNO", "10001");

      WSDataService.CallAsync("http://mylws/service/address?wsdl",
                 "MMS001_CUSTOM", "MMS001_CUSTOM", p, new Uri("https://mylws/service/address"),
                 CallbackDelegate, true, "username", "password");
    }

    public function CallbackDelegate(result: Object, message: String, ex: Exception, operation: String)
    {
      Debug("Operation: " + operation);
      Debug("Message: " + message);

      if (ex.InnerException)
        Debug("Exception: " + ex.InnerException.Message);
      else
        Debug("Exception: " + ex.Message);

      Debug("Result: " + result);
      Debug("-----------");
    }
  }
}

I didn’t manage to create a dictionary in JScript so I use a method that returns one, then clears it. Might need some improving.

UPDATE – JUN 14 2013:

To send parameters to the webservice the keys in the dictionary have to have the exact binding names, or they wont be sent at all.
One way to find the names is by using the WsTest tool.

  1. Start Smart Office and then the tool by typing “tool://wstest”.
  2. Enter the wsdl and hit “Load”.
  3. Uncheck the checkbox called “Show tree view” and scroll down to your parameter.
  4. There you can see all the names and right click to copy one.
wstest
*Thanks Karin for this

/Erik

Related Articles

15 thoughts on “Using Dynamic WS to consume a LWS in a script”

  1. Hi Eric,

    I was able to execute the web service and retrieved a result value, but it seems to be only a string containing the method name, and “ResponseType” added – in my example CRS277_GetResponseType. I can see the structure of the full SOAP response in Fiddler, but how can I access the fields in the “result” return object from my JScript ? /Heiko

    Like

    1. Hi, it may look like a string but doing a WriteLine() on an object will output the name of the type. So if you look in your response xml, locate the element with that name and try to access its children on your response object.

      Maybe something like: result.CRS277.W1CUNO

      Have you also tried the tool://wstest ? You should be able to see the types and properties there.
      Or, you could always browse to the wsdl location and take a look at the type structure (though it could be a bit messy)

      Like

  2. I want to know can we set the values to the uneditable fields using Smart Office Scripting.
    for ex:I need to call MMS121 from MMS080 adn need to pass some values and set it in MMS121 but i am not able to do so ,

    Actually I get an error saying Failed to comply request

    Might this be because fields no MMS121 are not editable.
    please help if you any idea

    Like

  3. Hi!
    thanks for this good explanation.
    I have a bit the same problem as Heiko.
    I donĀ“t get the values from the object. When I get the object type and compare that with the type structure on the wsdl page it seems it is on the wrong “sub type”.
    For example I do this “result.GetType();” and the result is “YXResponseType”
    When I look on the wsdl page I see now:

    Main transaction

    YXResponseType
    new0Collection
    new0collectionType
    new0Item
    new0ItemType
    My value 1
    My value 2

    The fiddler result looks good to me:
    – Transaction response
    new0collection
    new0Item
    My Values

    Any idea?

    Thanks in advance!

    Like

    1. Hi Daniel, I learned a lot in the meantime, and this is very simple. It depends on the name that you applied in the web service for your result set.

      Default is “new0”. But you can change it in the designer, above the result fields. I always give my resultset in the MWS designer a meaningful name.

      If your object name in the CallBackDelegate is “result”, you can do:
      var items = result.new0Collection;

      now loop your resultset:
      for (var record in items)
      yourVariable = items[record].YourFieldName;

      if yourVariable is a String you will have trailing blanks, so better add …Trim().

      A good written SQL at the right place can be faster then an API. And you can even create StoredProcedures and wrap it via MWS.

      Liked by 1 person

Leave a comment