How can i set string field value from Async Business rule?

Ankit Kumar6
Tera Contributor

Hi Team,

I have a string field on incident and I have a requirement to set this field value from the soap Api body response.

here the code written in async business rule.Howcan i do this.

Thanks,

Ankit

 

1 ACCEPTED SOLUTION

sumanta pal
Kilo Guru


Here are the steps to set a string field value from a SOAP API response using an asynchronous business rule in ServiceNow:

1. Create an asynchronous business rule:
- Navigate to System Definition > Business Rules.
- Click on New to create a new business rule.
- Set the 'When to run' section as 'After' and 'Async' to make it an asynchronous business rule.

2. In the 'Advanced' tab, write a script to call the SOAP API:
- Use the 'SOAPMessageV2' function to create a new SOAP message.
- Set the endpoint, SOAP action, and any necessary headers.
- Use the 'setRequestBody' method to set the body of the SOAP message.
- Use the 'getXMLResponse' method to get the response from the SOAP API.

3. Parse the SOAP response:
- Use the 'XMLDocument2' function to parse the SOAP response.
- Use the 'getNode' method to get the specific node from the SOAP response that contains the value you want to set in the string field.

4. Set the string field value:
- Use the 'setValue' method to set the value of the string field on the incident record with the value from the SOAP response.

Here is a sample code:

javascript
(function executeRule(current, previous /*null when async*/) {

// Create a new SOAP message
var sm = new SOAPMessageV2('your_soap_message', 'your_soap_function');

// Set the endpoint, SOAP action, and any necessary headers
sm.setEndpoint('your_endpoint_url');
sm.setSOAPAction('your_soap_action');

// Send the SOAP message and get the response
var response = sm.getXMLResponse();

// Parse the SOAP response
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(response.toString());

// Get the value from the SOAP response
var value = xmlDoc.getNode('your_node').getTextContent();

// Set the value of the string field on the incident record
current.your_string_field = value;

// Update the incident record
current.update();

})(current, previous);


Please replace 'your_soap_message', 'your_soap_function', 'your_endpoint_url', 'your_soap_action', 'your_node', and 'your_string_field' with your actual SOAP message, function, endpoint URL, SOAP action, node, and string field respectively.

 

For asking ServiceNow-related questions try this :

For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.

Please visit : https://nowkb.com/home
Our Website :https://nowkb.com/home
Link - https://nowgpt.ai/

View solution in original post

5 REPLIES 5

sumanta pal
Kilo Guru

For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.

Please visit : https://nowkb.com/home
Our Website :https://nowkb.com/home
Link - https://nowgpt.ai/

@Jaspal Singh