Check for existing record in a table

nicolemccray
Tera Expert

I have a custom table that inserts some user fields each time a request is submitted.  How can I do a check on the table to see if the Employee ID already exists, and if it does, display a message to the requester and do not allow submission?

1 ACCEPTED SOLUTION

I was able to get this working by using an OnChange script:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var employeeId = g_form.getValue('employee_id');

if (employeeId != ''){

var ga_applicant = new GlideAjax("CorporateTravel");
ga_applicant.addParam("sysparm_name","getRequestInfo");
ga_applicant.addParam("employeeId", employeeId );
ga_applicant.getXML(ajaxResponse);

}

function ajaxResponse(serverResponse) {
var applicant = serverResponse.responseXML.getElementsByTagName("applicant");

if (applicant.length > 0){
already_applied = true;

alert("This person already has an application request in the system. Only one application request for a person may be submitted.");

g_form.setValue('understand_accept1', '');
g_form.setValue('understand_accept2', '');
g_form.setValue('understand_accept3', '');

}
}

}

View solution in original post

17 REPLIES 17

Tanushree Doiph
Mega Guru

Hey,

Use this code,

var record = new GlideRecord('<Your table Name>');

 

record.addQuery('u_record_id',current.u_record_id);

 

record.query();

 

 

 

if (record.next())

 

{

 

current.u_id = record.u_id;

 

current.setAbortAction(true);

 

}

 

else

 

{

 

insert();

 

}

 

Also, refer below link,

https://community.servicenow.com/community?id=community_question&sys_id=3f4aced4db1e730014d6fb243996...

Thanks

Tanushree

I need to do a comparison with the Requester.  I need to determine the employee ID of the requester, and if that Employee ID is already in the table, prevent submission.

Hi,

You can use before insert BR. Try something like below

 

var user = gs.getUserID();
var gr = new GlideRecord('your_table_name');
gr.addQuery('employee_id_field',user);
gr.query();

if(gr.next())
{

gs.addInfoMessage("Employee ID already present");
current.setAbortAction(true);
}

else

{

current.insert();

}

Thanks,

Dhananjay.

I tried the following before BR, set on my custom table.  Did not work.  Should it be set on the 'sc_req_item' table instead?

 

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

// Add your code here

var user = gs.getUserID();
var gr = new GlideRecord('u_travel');
gr.addQuery('u_employee_id',user);
gr.query();

if(gr.next())
{

gs.addInfoMessage("Employee ID already present");
current.setAbortAction(true);
}

else

{

current.insert();

}

})(current, previous);