The CreatorCon Call for Content is officially open! Get started here.

Java API code to create new incident

chitrat
Tera Contributor

Hi All,

Am new to ServiceNow and in the process valuating it.

Here my requirement is I need to create a new incident with the Java code.

Can anyone help me with the API Java code to create new incident on ServiceNow?

Thanks in advance

Chitra

1 ACCEPTED SOLUTION

tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Chitra,



There is example java code here:


Table API Java Examples - ServiceNow Wiki



3.2 POST


..



Does that help answer your question?



Best Regards



Tony


View solution in original post

24 REPLIES 24

tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Chitra,



Thanks for getting back with the solution so other Community members can benefit!



Best Regards



Tony


Hi Chitrat;



I am trying to call the   service now newly created instance from HTTP Client   but getting the connection time out error .




og4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
            at java.net.DualStackPlainSocketImpl.connect0(Native Method)
            at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
            at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
            at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
            at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
            at java.net.PlainSocketImpl.connect(Unknown Source)
            at java.net.SocksSocketImpl.connect(Unknown Source)
            at java.net.Socket.connect(Unknown Source)
            at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)



Here   are the code   client code:-



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;


public class GetAction {
            public static void main(String[] args) throws IOException {
                          GetAction restAction = new GetAction();
                          restAction.getRequest();
            }


            public void getRequest() throws HttpException, IOException {
                          HttpClient client = new HttpClient();
                          client.getParams().setAuthenticationPreemptive(true);
                          Credentials creds = new UsernamePasswordCredentials("XXXXXX", "XXXXXXX");
                          client.getState().setCredentials(AuthScope.ANY, creds);


                          HttpMethod method = new GetMethod("https://dev13051.service-now.com/api/now/table/table");
                          method.addRequestHeader("Accept", "application/json");
                          int status = client.executeMethod(method);
                          System.out.println("Status:" + status);
                          BufferedReader rd = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
                          String line = "";
                          while ((line = rd.readLine()) != null) {
                                        System.out.println(line);
                          }
            }
}




Could you please help me on this.


Hi Shiv,



What is the result of a


telnet dev13051.service-now.com 443


from the workstation where you are running your java code?



or a tracert dev13051.service-now.com


?



To confirm you have network connectivity.



Best Regards



Tony


Hi Tony,



Here are the response, I am getting



telnet dev13051.service-now.com 443


Connecting To dev13051.service-now.com...Could not open connection to the host,


on port 443: Connect failed



C:\Users\shivshankar_barik>


Hi Shiv,



Can you reach out to your IT department to determine if they can take steps so that non-browser traffic can access host dev13051.service-now.com on port 443?



Best Regards




Tony