How can I update a record in another table when a UI action button is clicked

karunakar onted
Tera Contributor

I have 3 tables
1. X, 2. Y, 3. Z
and I have a UI action button on X table
when this button is clicked (it just generates a document and does not update the form) I want to compare 3 fields of X table with Y table (same fields are available in X and Y table) if match found then a field in Z table needs to be updated.



tried this script

compareAndUpdate: function () {
var caseSysId = this.getParameter('sysparm_caseSysId');
 var caseGr = new GlideRecord('X');
       caseGr.addQuery('sys_id',caseSysId);
caseGr.query();
if(caseGr.next())
{
   var country = caseGr.u_country;
        var legalEntity = caseGr.u_legal_entity;
        var mrc = caseGr.u_mrc;
  var footerGr = new GlideRecord('Y');
        footerGr.addQuery('u_country', country);
        footerGr.addQuery('u_legal_entity', legalEntity);
        footerGr.addQuery('u_mrc', mrc);
        footerGr.query();
if(footerGr.next())
{
var contactDetails = footerGr.u_address_contact_details;
        var htmlTemplateGr = new GlideRecord('Z');
        htmlTemplateGr.addQuery('sys_id','1234567890');
        if (htmlTemplateGr.next()) {
            htmlTemplateGr.footnote = contactDetails;
            htmlTemplateGr.update();
            return 'Footnote updated successfully';
        } else {
            return 'No HTML template record found';
        }
    }






 and the client script in ui action I have used this
var ga = new GlideAjax('CompareAndUpdateFootnote');
    ga.addParam('sysparm_name', 'compareAndUpdate');
    ga.addParam('sysparm_caseSysId', g_form.getUniqueValue());

    ga.getXMLAnswer(function(response) {
        var msg = response;
        g_form.clearMessages();
        g_form.addInfoMessage(msg);
        alert(msg); // Optional
    });

but it is returning null and not updating anything
2 REPLIES 2

palanikumar
Mega Sage

Hello @karunakar onted ,

 

While taking data from table, use toString() otherwise the data will be still stored as object and it may not return the expected response. Update these lines alone:

 

var country = caseGr.u_country.toString();
var legalEntity = caseGr.u_legal_entity.toString();
var mrc = caseGr.u_mrc.toString();
Thank you,
Palani

HI @palanikumar 
it's still the same, not getting updated.