Need sample scripts on how to call a WSDL Function from a Business Rule

kirstina
Kilo Contributor

Need sample scripts on how to call a WSDL Function from a Business Rule

Can someone help please?

Thanks KD

1 ACCEPTED SOLUTION

vab_13
ServiceNow Employee
ServiceNow Employee

have a read:


SOAPMessageV2 Uses code in business rule...


find_real_file.png



Problem statement in above location:



I'm trying to use SOAPMessageV2 API, for that I have created one table with 3 columns (First Name, Last Name & Number) in my test environment. Since all the tables in Service Now can be published by Direct Web Services.


I've created an outbound web services and use direct Web Services WSDL in this out bound web services, Now I want that on my custom table when I'll enter number and submit first name and last name will return.


Can anyone please help me to write a code for this via Business Rule and SOAPMessageV2 API.



Write an onSubmit client script something like this:


function onSubmit() {


    var firstn = '';
    var lastn = '';
    var nbr = '';   //<Assign this field to what was entered>
    var requid = "<WebServiceName>"
    var script = "getName('" + firstn + "','" + lastn + "','" + nbr + "','" + requid + "')";
    var answer = AJAXEvaluateSynchronously(script);
    g_form.setValue('u_result', answer);


    var myExp = /<CODE>/
    var code;


    code = answer.search(/RAN Successfully/i);


    if (code != '1'){     //This is looking in the returned result to confirm it ran successfully.
            var id = answer.search(/<ID>/i);
            logonid = answer.substr(id+4, 6);
    }


    g_form.setValue('u_username',logonid);


}



This is the Preview Script that will display for the Soap Message


try {
var s = new sn_ws.SOAPMessageV2('<Soap Message Name>', '<Soap Message Function>');


//override authentication profile
//authentication type ='basic'
//r.setAuthentication(authentication type,profile name);


s.setStringParameterNoEscape('first_name', 'TestReqq');
s.setStringParameterNoEscape('last_name', 'TestReqqLast');
s.setStringParameterNoEscape('nbr', '4785');
s.setStringParameterNoEscape('requesting_id', '<WebServiceName>');
var response = s.execute();
var responseBody = response.getBody();
var status = response.getStatusCode();
}
catch(ex) {
var message = ex.getMessage();
}




And this is the Business Rule that is called from the client script:


function getName(firstn, lastn, nbr, requid)   {


    var s = new SOAPMessage('<Soap Message Name>', '<Soap Message Function>');
    s.setParameter('first_name', firstn);
    s.setParameter('last_name', lastn);
    s.setParameter('num', nbr);
    s.setParameter('requesting_id', requid);
    var response = s.post();
    return response;
}



I have not tested anything, and this might not even work, but this is just to give you an overview of how developers customise.


View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kirstina,



Following links should be helpful:


Outbound SOAP Web Service - ServiceNow Wiki


http://www.john-james-andersen.com/blog/service-now/tutorial-handling-a-soap-response-in-servicenow....



you can call the soap message from any server side scripting



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks Ankur. Appreciate the help.



KD


Harsh Vardhan
Giga Patron

Hi Kristina,



you can use SOAPMessageV2() api.




try {


var s = new sn_ws.SOAPMessageV2('<soapMessage>', '<soapFunction>');


s.setStringParameter('description', 'helloWorld');


var response = s.execute();


var responseBody = response.getBody();


var status = response.getStatusCode();


}


catch(ex) {


var message = ex.getMessage();


}



you can refer the doc below.


https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=c_SOAPMessageV2API


Thanks Harsh, Appreciate the help.



KD