Check for duplicate records on table from RITM form?

SandyL83
Tera Guru

Hello,

I have a RITM that is related to vendor risk managment .. this ritm sets up a vendor on the core_company table.

On the RITM form, there is the a reference field that allows user to select vendor; and if the vendor is not already on the core_company table, we have a checkbox that says "vendor not found". If that is checked, the user enters the Vendor name in a free form text field and then the vendor record is created on the core_company table. The problem is, people aren't checking the reference field to see if the vendor exists, they are usign the free form field. so, many duplicate vendors are being created. 

Does anyone have any ideas on how to write a script to prevent this - ideally if someone types in "VEndor A" in the free form, i'd like a message that says "Vendor A already exits, please select that from the field above".

 

Thanks

1 ACCEPTED SOLUTION

SandyL83
Tera Guru

Hi @Ankur Bawiskar  and @Mayur2109 ,

Thanks for all of the valuable feedback. Turns out all I needed was the following client script - no script includes was required:

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


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


  return;


  }


  var vdr = new GlideRecord('core_company');


  vdr.addQuery('name',newValue);
  vdr.addQuery('vendor'true);


  vdr.query();


  if(vdr.next())


  {


  alert('Vendor already exists. Uncheck "Vendor not Found" and select the value from the "Vendor Name" field above.');


  g_form.setValue('vendor','');


  }


}

View solution in original post

13 REPLIES 13

Mayur2109
Kilo Sage
Kilo Sage

Hi @SandyL83 ,

 

You can write a onChange client script for this and pass the value entered by user to a client callable script include.

From script include you can query core_company table and check if vendor is present. If the vendor is there pass answer as false to client script and add info message "Vendor A already exits, please select that from the field above".

 

Please check and Mark Helpful and Correct if it really helps you.

Regards,
Mayur Shardul

Hi @Mayur2109 

Thanks for the response. Here is what I have so far based on your feedback and it still doesn't seem to be working.. any other suggestions? thanks again

1. here is my script includes:

var checkvendor = Class.create();
checkvendor.prototype = {
    initialize: function() {

var name = this.getParameter('sysparm_company');            
        var gr = new GlideRecord('core_company');
        gr.addQuery('name', name);
        gr.query();
        if(gr.next()){
            return 'found';
        }
        return 'not found';
    },

    type: 'checkvendor'
};
 
 
2. Then, here is my onChange client script:
nction onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    g_form.hideFieldMsg('vendor'); // give here field/variable name

    var ga = new GlideAjax('checkRecords');
    ga.addParam('sysparm_name'"checkRecordPresent");
    ga.addParam('sysparm_company', g_form.getValue('vendor')); // give here field/variable name
    ga.getXMLAnswer(function(answer){
        if(answer == 'not found'){
            var message = 'This hardware is not present';
            g_form.clearValue('vendor'); // give here field/variable name
            g_form.showFieldMsg('vendor',message,'error'true);
        }
    });
    //Type appropriate comment here, and begin script below

}
 

Hi @SandyL83 ,

 

Your script include doesn't seems to be client callable, please check it again and check outputs by putting logs.

 

Regards,

Mayur

Hi @SandyL83 ,

Try below changes it will work.

Client Script:

 

 

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

    var ga = new GlideAjax("script_include_name");
    ga.addParam("sysparm_name", "function_name");
    ga.addParam("sysparm_var", newValue);
    ga.getXML(callBackFunction);

    function callBackFunction(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
            g_form.addInfoMessage('Duplicate Message');
            g_form.clearValue("field_name")
        }

    }
}

 

 

Script Include: Make sure it is client callable.

 

 

 function_name: function() {
        var param = this.getParameter("sysparm_var");
        var gr = new GlideRecord("table_name");
        gr.addQuery("field_name", param);
        gr.query();
        if (gr.next()) {
           return true;
        }
    },

 

 

 

Please check and Mark Helpful and Correct if it really helps you.

Regards,
Mayur Shardul