throw error if value already present in table

Evan2
Kilo Guru

Hi Team, 

On Portal we have populate the value Name and the value of name is stored in "site using" table.

Requirement is to check if value of name already present in site using table, throw an error and RITM not created.

We can add multiple value by clicking on add and need to check all the values.

Note:- This all variables came  under multi line variable set

 
Ve
19 REPLIES 19

Uh oh, your requirement was not clear initially.

now you need both script include,and  glideajax to prevent creation.

using your script itself:

make sure client callable is checked

var siteDetails = Class.create();
siteDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
sitecheck: function(){
var ans= 'false';
var gr=new GlideRecord('u_cmdb_ci_sites_using');
gr.addQuery("name",this.getParameter('sysparam_id'));
gr.query();
gr.next();
if(gr.next())
{
ans='true';
}
return ans;
},
type: 'siteDetails'
});

Catalog client script(inside multiline variable set)

function onSubmit() {
var arr=[];
var loc=g_form.getValue('create_new_sites');// Internal name of multiline variable set
if(loc == '[]' ){
g_form.addErrorMessage('You must enter at least 1 row');
return false;
}
var data = JSON.parse(loc);
var ct= 0;
for(var i in data){
if (data.hasOwnProperty(i)) {
arr.push(data[i].name1.toString().replace(/\s/g, "")) ; //added a ) here
}
}

for(var j=0;j<arr.length;j++){

var ga=new GlideAjax('siteDetails');
ga.addParam('sysparm_name','sitecheck');
ga.addParam('sysparam_id',arr[j].toString());
ga.getXML(Process);
}
}

function Process(response)
{
var answer=response.responseXML.documentElement.getAttribute("answer");
if(answer=='false')
{
g_form.clearValue('name1');
alert("error");
return false;
}
}

 

 

these should work unless there are  other dependency with the namings

It give me the alert but submit the form.

ok then ,end the onsubmit bracket to the last instead of the glideajax call .

also change the getXML and its function try:

for(var j=0;j<arr.length;j++){

var ga=new GlideAjax('siteDetails');
ga.addParam('sysparm_name','sitecheck');
ga.addParam('sysparam_id',arr[j].toString());

var answer=ga.getAnswer();
if(answer=='false')
{
g_form.clearValue('name1');
alert("error");
return false;
}//if ends

}//for ends

}//onsubmit

 

 

Did this work for you?

One more thing we need to check, 

We need to check application name also. 

If the comibnation of application name and Name is already present then the request will not be submitted.

Screenshot of Catalog item:-

find_real_file.png

Value of application name will come in Adm application field of site using form.

find_real_file.png