How to trigger outbound REST API when State is change?

jackh
Kilo Contributor

Hi,

My use case requires to call external REST API from ServiceNow when the incident state is changed.   For example, our release pipeline will create a new incident (via inbound REST API call) for someone to approve this release.   Once the incident assignee approves the ticket by changing the State (e.g. from New to Approved), it will trigger to call outbound REST API to our release pipeline.   How to do that in ServiceNow to trigger such outbound API when State is changed?


Any advice would be appreciated.

Thanks

1 ACCEPTED SOLUTION

That is because, when you click "Update" you are redirected to the recent page you opened. When you click save, you can still stay on the same incident form even after updating the form.


View solution in original post

22 REPLIES 22

Hi Kumar,



I created a simple client script called "State changed" with following.  


        Table: Incident


        UI Type: Desktop


        Type: onChange


        Field name: State


        Script:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue == '') {


          return;


    }




    //Type appropriate comment here, and begin script below


  if (newValue == 'Awaiting User Info') {


      alert('State changed to Awaiting User Info');


  }


}



But when I change the State from "New" to "Awaiting User Info" on a test incident, it doesn't pop up the alert message.   Did I miss something here?



Thanks


I think state is number field in incident table, there will be a corresponding numbers for the string that we display on screen, please check Incident table for state field and use that number in the script instead of the string 'Awaiting User Info'


tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Jack,



It could be a better alternative to use a business rule on the incident table as suggested by bss and Tim?



edit:


Would you like some guidance on going that route?


Or have you spotted a reason not to use that approach?



Best Regards



Tony


Jack Hsu,



    In your code,   <if (newValue == 'Awaiting User Info')> you are using the choice label. You should always use choice value.


Rick click on State field and click "Show choice list", you should see the value of your choice, in your case for "Awaiting User Info"



Thanks,


Abhinay


jackh
Kilo Contributor

Changing "if (newValue == 'Awaiting User Info')" to "if (newValue == 4)" does work. Thanks for the tips.



Sorry, I'm slow on learning the ServiceNow development tool.   I just created a business rule called "Stage changed" with:


        Table: Incident [incident]


        When: after Update


        Filter Conditions: Stage, changes to, Awaiting Evidence


        Script:



function onAfter(current, previous) {


    //This function will be automatically called when this rule is processed.


    alert('State has changed to Awaiting Evidence');


}



and submitted it.   But when I change the State to Awaiting Evidence on a test incident, it doesn't pop up the alert message.   Did I miss something here?



Thanks