Catalog client script to update a record in another table

Maria DeLaCruz
Tera Guru

Hello,

I need some help with a catalog client script that needs to update a record in another table.  I was advised that it is best practice to use GlideAjax in catalog client scripts, but I'm not familiar with how to use it to update a record in another table.

Below is my script.  I've highlighted where I was trying to update a record in another table, and that's not working.

Any help would be greatly appreciated. 

Thanks,
Maria

 

function onSubmit() {

var projName = g_form.getReference('project_name', callback);

function callback(projName)
{
var used = projName.u_used;

var ga = new GlideAjax('ConfidentialProject');
ga.addParam('sysparm_name', 'getProjectName');

var ga2 = new GlideAjax('ConfidentialProject');
ga2.addParam('sysparm_name', 'markProjectNameUsed');
ga2.addParam('projname', projName);

if (used == 'true'){
ga.getXML(projectNameUsed);
}

if (used == 'false'){

ga.getXML(projectNameUnused);
}
}
}

function projectNameUsed(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('project_name', answer);
g_form.setReadOnly('project_name', true);
}

function projectNameUnused(response2){
var answer2 = response2.responseXML.documentElement.getAttribute("answer");
var proj = new GlideRecord('u_project_name');
proj.addQuery('sys_id', answer2);
proj.query();
//alert(proj.u_used);
if (proj.next()){
proj.u_used = 'true';
proj.update();
}
}







3 REPLIES 3

TrevorK
Kilo Sage

You need two pieces - your Client Script and your Script Include. Your Client Script calls the Script Include using Glide Ajax. I will give you a simple example below - this users the sys_user table so should be able to be replicated in your instance. It is merely passing in a user and getting their Phone / Email.

 

For example this could be your client script:

find_real_file.png

 

And this would be your Script Include:

find_real_file.png

 

Now in your case you would pass in a value to your Script Include, then do the update, and return a Success or Failure to your Client Script. The goal is to not do anything GlideRecord related in the Client Script and leave it all in the Script Include. That's why your update itself should occur in the Script Include.

 

Any questions let me know!

Thanks Trevor!  

Here's my script include.  I'm assuming that if I would like my catalog client script to update a record in another table, I would need to have the code in the script include.  I think I'm just not sure how to call the function (highlighted in yellow) in my catalog client script.  Help please.  Thanks!


find_real_file.png

The blue arrow (on the left side) points to the name of your Script Include. The black arrow (on the right side) points to the name of your function:

find_real_file.png

 

So in your code it would be:

var myGlideAjax = new GlideAjax('ConfidentialProject');
myGlideAjax.addParam('sysparm_name','markProjectNameUsed');
myGlideAjax.addParam('sysparm_projname',YOUR_VARIABLE_TO_PASS);
myGlideAjax.getXML(parseObject);

Tips:
- I would return the value from the .update() to indicate if the update was successful
- You'll need the this.getParameter code from your first function included so that you can get 'projName'