How to insert a record into a table from a business rule?

doug_andres
Kilo Explorer

Hi All,

How do I insert a record into a table from a business rule?

First, a little background on what I'm trying to do:

I created a Business Rule script that pulls data from GitLab's API. This is triggered after a someone creates or updates a record on a table. The business rule takes the info the user entered and generates the API call to GitLab. It then pulls that info and has it within the business rule. My goal is to then take this information, and insert it into a custom table that I created.

I originally built an external Python application that could do this, but I am now transferring this into ServiceNow so both the script and data are stored in the same place.

How would I go about inserting into a table from a business rule? I have been experimenting with the "REST API Explorer" and the JavaScript code that it generates to be able to do this. However, I haven't had any success yet. Any advice on how to do this?

Here is my code so far:

var client=new XMLHttpRequest();

client.open("post","MY-SERVICENOW-INSTANCE");

// for loop runs through the data I got from GitLab, formats it to JSON, and then should push to the table for the all the records I pulled from GitLab

for (var s = 0; s < commit_array.length; s++) {

        var requestBody = {

                  'u_archive_link' : repo_obj.get_purl(),

                  'u_change_request' : api_obj.get_crnum(),

                  'u_commit_archive_artifcat_name' : commit_array[s].get_cn(),

                  'u_commit_archive_date' : commit_array[s].get_cd(),

                  'u_commit_developer_name' : commit_array[s].get_cp(),

                  'u_jira' : api_obj.get_jiranum(),

                  'u_last_activity' : repo_obj.get_la(),

                  'u_number_of_files' : repo_obj.get_nf(),

                  'u_project_name' : repo_obj.get_pn()

        };

client.setRequestHeader('Accept','application/json');

client.setRequestHeader('Content-Type','application/json');

//Eg. UserName="admin", Password="admin" for this code sample.

client.setRequestHeader('Authorization', 'Basic ' + btoa('admin'+':'+'MY_PASSWORD'));

JSON.stringify(requestBody);

client.send(requestBody);

Is this how one would insert into a table from a Business Rule? I am new to ServiceNow and using Business Rules so I'm not sure how to accomplish what I want to do.

Thanks!

Doug

3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

You dont need REST to insert data to a table. Just to a GlideRecord to the table you want to insert



for (var s = 0; s < commit_array.length; s++) {


var ga = new GlideRecord('Your custom table name');


ga.initialize();


ga.u_archive_link=repo_obj.get_purl();


ga.u_change_request=api_obj.get_crnum();


ga.u_commit_archive_artifcat_name=commit_array[s].get_cn();


ga.u_commit_archive_date=commit_array[s].get_cd();


ga.u_commit_developer_name=commit_array[s].get_cp();


ga.u_jira=api_obj.get_jiranum();


ga.u_last_activity=repo_obj.get_la();


ga.u_number_of_files=repo_obj.get_nf();


ga.u_project_name=repo_obj.get_pn();




ga.insert();


}



Please mark this response as correct or helpful if it assisted you with your question.

sanjivmeher,



Thank you for getting back to me so fast! I gave it a try and it inserted the records into the table I wanted.



Have a great rest of your day!



Thanks again!


Doug


Thats Great Doug. Can you mark this post answered so that it is removed from the unanswered list?



Please mark this response as correct or helpful if it assisted you with your question.