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

Hi @Mayur2109 ,

I made the updates that you provided and it's still not working. I really appreciate your help. Let me know if you have any other ideas

So here is my client script: 

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

    var ga = new GlideAjax("checkvendor");
    ga.addParam("sysparm_name""function");
    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("vendor")
        }

    }
}
 
and here is the script includes (it is client callable):
function_name: function() {
        var param = this.getParameter("sysparm_var");
        var gr = new GlideRecord("core_company");
        gr.addQuery("name", param);
        gr.query();
        if (gr.next()) {
           return true;
        }
};

Hi @SandyL83 ,

Can you please change function name at both script include, client script & make it same, I'm seeing it as ga.addParam("sysparm_name""function") in client script  and in script include function_name: function() .

Let me know if you still face issue.

 

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

Regards,
Mayur Shardul

Hi @Mayur2109  - thanks so much..

Here is my updated script includes: (notice I changed the function name to checkData). it still doesn't seem to be working even though I think I am calling it correctly. Any ideas? Thanks again for your help!

 

checkData: function() {
        var param = this.getParameter("sysparm_var");
        var gr = new GlideRecord("core_company");
        gr.addQuery("name", param);
        gr.query();
        if (gr.next()) {
           return true;
        }   
};
 
 
Then, here is the Client Script: 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var ga = new GlideAjax("checkvendor"); //name of the script includes
    ga.addParam("sysparm_name""checkData"); // name of the function
    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("vendor")
        }

    }
}






Hi @SandyL83 ,

Put logs and check if you are getting correct value in script include for param then check if query is returning anything or not in if loop of script include, then check if answer contains any value in client script.

 

Regards,

Mayur Shardul

Ankur Bawiskar
Tera Patron
Tera Patron

@SandyL83 

update your script as this

Script Include: It should be client callable

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	checkData: function(){
		var name = this.getParameter('sysparm_company');            
		var gr = new GlideRecord('core_company');
		if(gr.get('name', name)){
			return true;
		}
		return false;
	},

	type: 'checkRecords'
});

AnkurBawiskar_0-1695305080090.png

 

Client script:

function 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', "checkData");
	ga.addParam('sysparm_company', g_form.getValue('vendor')); // give here field/variable name
	ga.getXMLAnswer(function(answer){
		if(answer.toString() == 'false'){
			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
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader