How I can create new user in service now using rest API and assing him a role?

dougoutigi
Mega Contributor

Hello,

I need help please

How I can create new user in service now using rest API and assing   him a role?

If possible an example will be   helpful

Thank you

1 ACCEPTED SOLUTION

Hi Alassane,



If you have the endpoint of the post method from this form you can use that and consume the REST service in your java code like any other rest webservice by passing the variables like in any other REST webservice.



Moreover, you can refer this page as well.


Table API Java Examples - ServiceNow Wiki



Hope it helps.



Thanks and Regards,


Vikas Malhotra


View solution in original post

11 REPLIES 11

Hi Alassane,



If you have the endpoint of the post method from this form you can use that and consume the REST service in your java code like any other rest webservice by passing the variables like in any other REST webservice.



Moreover, you can refer this page as well.


Table API Java Examples - ServiceNow Wiki



Hope it helps.



Thanks and Regards,


Vikas Malhotra


dougoutigi
Mega Contributor

Hi Viska,


Ok, how I can have the endpoint of the post method from this form   and   use it, please?


Thanks


Hi Alassane,



The idea mentioned in this thread, is that the REST API explorer is used to insert a record in sys_user table and the to insert a record into sys_user_has_role (assuming the role has already been created in sys_user_role).


Have you succeeded in doing that? So that the created user has the expected role.



When you use the API explorer you can generate a code sample which the end point etc


doc for Fuji is here: What version on you on BTW?


REST API Explorer - ServiceNow Wiki


..


3 Code Samples


You can obtain a code sample in various languages that you can use to send the request. Links for code samples appear at the bottom of the form after you click Send. Click a link to view the code sample for that language. The code samples change based on data you enter on the form.


Code samples are available in these languages:


  • ServiceNow Script
  • cURL
  • Python
  • Ruby
  • JavaScript
  • Perl


Best Regards



Tony


dougoutigi
Mega Contributor

Hi Tony,


Yes I can do that.



I want to create user in java like in this example :


public class PostAction {


  public static void main(String[] args)throws IOException{


  PostAction restAction =new PostAction();


  restAction.postRequest();


  }



  private void postRequest()throws HttpException, IOException{


  // This must be valid json string with valid fields and values from table


  String postData ="{\"short_description\":\"Test with java post\"}";


  HttpClient client =new HttpClient();


  client.getParams().setAuthenticationPreemptive(true);


  Credentials creds =new UsernamePasswordCredentials("Username", "UserPassword");


  client.getState().setCredentials(AuthScope.ANY, creds);


 



  PostMethod method = new PostMethod("myinstance.service-now.com/api/now/table/sys_user");


  method.addRequestHeader("Accept", "application/json");


  method.addRequestHeader("Content-Type", "application/json");


  method.setRequestEntity(new ByteArrayRequestEntity(postData.getBytes()));



  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);


  }


  }


  }


Hi Alassane,


Here is the curl code from the REST API explorer.


Note the --data   line


you need at a minimum to specify a value for   the user_name



curl --user 'admin':'admin' \


"https://emptbarratt2.service-now.com/api/now/table/sys_user" \


--request POST \


--header "Accept:application/json" \


--header "Content-Type:application/json" \


--data "{'user_name':'fred.bloggs'}"



Best Regards



Tony