How to Display an alert in Record producer if form variable value not matched in table

vsharma
Tera Contributor

Hi All:

I have a Record producer (table - u_visitor with fields - Name/ mobile no/ checkin time/ checkout time/ state) where I need to check mobile number and if it matches in table then user state change to "checked out" else alert user to check mobile number and abort.

I tried but not able to get else condition work to alert user & abort process. Please suggest.

 

—— Record Producer Script section -------

var mno = producer.u_mobile_number;

var rec = new GlideRecord('u_visitor');

rec.addQuery('u_mobile_number', mno);

rec.query();

 

if (rec.next()) {

rec.u_state = 'checkedout';

rec.u_checkout_time = current.u_checkout_time;

rec.update();

current.setAbortAction(true);

Note: All RP form fields mapped to table fields.

 

Thanks

1 ACCEPTED SOLUTION

I just noted that this was a record producer. You could directly with the producer script. Can you please try this once after deactivating the client script and see if it works?

var mno = producer.u_mobile_number;
var rec = new GlideRecord('u_visitor');
rec.addQuery('u_mobile_number', mno);
rec.query();
if (rec.next()) {
rec.u_state = 'checkedout';
rec.u_checkout_time = current.u_checkout_time;
rec.update();
}else{
gs.addErrorMessage("Update Mobile number First");
current.setAbortAction(true);
//producer.redirect =""; 
} 

View solution in original post

28 REPLIES 28

shloke04
Kilo Patron

Hi,

 

I would suggest you to write an On Change Client Script on your Mobile variable which in turn will call  a Script Include(Client Callable as True) to perform the check for the Mobile Number present or not in your Custom table created and then based on the value returned you can allow or abort the Form Submission.

 

Below is a Reference of the Script Include :

var ans = 'false';

var mno = this.getParameter('sysparm_number');

var rec = new GlideRecord('u_visitor');

rec.addQuery('u_mobile_number', mno);

rec.query();

 if(rec.next()) {

rec.u_state = 'checkedout';

rec.u_checkout_time = current.u_checkout_time;

rec.update();

ans = 'true';

else{

ans = 'false';

}

return ans;

 

Now use the Glide Ajax function and check for this return value (ans) i.e if it is false do return false in client script to abort form submission else allow it. For Glide Ajax Refer the below links:

 

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference/r_GlideAjax.html

 

I haven't tried the above script in my instance, but it's an overview on how to proceed on the same. Modify it accordingly as needed.

 

Hope this help. Please mark the answer as helpful/correct based on impact.

 

Regards,

Shloke

 

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

vsharma
Tera Contributor

Hi, 

I have tried as advised but did not work. A new entry created. Please check below code if I missed something.

------ Script Include (Client callable - true)------

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

checkMobileno: function() {
var ans = 'false';
var mno = this.getParameter('sysparm_number');
var rec = new GlideRecord('u_visitor');
rec.addQuery('u_mobile_number', mno);
rec.query();
if(rec.next()) {
rec.u_state = 'checkedout';
rec.u_checkout_time = current.u_checkout_time;
rec.update();
ans = 'true';
}
else{
ans = 'false';
}
return ans;
},
type: 'checkMobileno'
});

 

-----Catalog Client Script (On Change - u_mobile_number) -----

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

//Type appropriate comment here, and begin script below

var ga = new GlideAjax('checkMobileno');
ga.addParam('sysparm_name', 'checkMobileno');
ga.addParam('sysparm_number',g_form.getValue('u_mobile_number'));

ga.getXML(checkMobileDetails); 
}
function checkMobileDetails(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");

var answers = answer;
if(answers == false)
{
alert("Please check your mobile number");
return false;
}
}

 

Thanks

Hello,

Please try changing the following part of your client script and see if it works. The false should be quotes

var answers = answer;
 if(answers == 'false')
 {
  g_form.clearValue('u_mobile_number');
  alert("Please check your mobile number");
  return false;
 }