Field validation and field error message

Shivi4
Tera Contributor

Hi ,

I have three fields  on new record creation of service portal widget (data table from URL definition) entity, region, country name. 

I need to put  two validation on country name field are - 

1) The 'country name' field Should not have duplicate values for the combination of Entity-Region, otherwise should display below error message: "The Value already exist for the combination Entity-Region".

 

2) 'Country name' Field Value should not exceed the Max Length otherwise should display below error message: "Field Value exceeds maximum characters.

can someone please help me to achieve this validation on 'country name' field. ??

 

Thanks in advance !

8 REPLIES 8

Basheer
Mega Sage

Hi @Shivi4 

You can use below scripts to achieve this

In Client Script

function onChange(control, oldValue, newValue, isLoading) {
      if (isLoading || newValue == '') {
              return;
      }
var countryName= g_form.getValue("country_name");//specify the correct column name
var entity= g_form.getValue("entity");//specify the correct column name
var max = 20;

var countryLength = countryName.length();
if(countryLength<=max)
{
var ga = new GlideAjax('GetRegionData');
ga.addParam('sysparm_name', 'countryDuplicate');
ga.addParam('sysparm_country', countryName);
ga.addParam('sysparm_entity',entity);
ga.getXML(callback);
}
else{
alert("Country name cannot be more than 20");
}
}
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == "true"){
alert("Your region is already present");
}
}

In Script Include

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

countryDuplicate: function () {
var country= this.getParameter('sysparm_country');
var entity= this.getParameter('sysparm_entity');
var gr = new GlideRecord("tableName");
gr.addQuery("country",country);
gr.addQuery("entity",entity);
gr.query();
if(gr.next()){
return true;
}
}
});

 

 

Shivi4
Tera Contributor

Hi @Basheer ,

for which validation you are suggesting this script for the first one or second ??

the script has logic of both

Shivi4
Tera Contributor

hi @Basheer ,

But we need to check the combination of  Entity & region field in country_name field. where you are checking for combination of this entity,region field. 

as you are only getting value of country_name and entity field not region.