Retrieve a table field value using script include and display values in alert using client script

Somujit1
Tera Contributor

Hello Experts,

 

I have a requirement to display an alert message with table field values, on load of a catalog item.

 

Scenario -

1. Table A (device_asset) contains fields Asset (asset), Assigned To (assigned_to), End Date (end_date) and Description (asset_description)

2. Catalog items has variable Requested For

3. If Assigned To matches with Requested For then alert message to display as 'Requestor has and existing Asset {asset_description}, with the end date as {end_date} 

 

I created a Script Include to fetch the values from Table A (which works fine) and a Catalog Client Script to return the alert message (which isnt working)

 

Catalog Client Script -

function onLoad() {
  var requestedFor =  g_form.getValue('requested_for');
  var ajax = new GlideAjax('GetAssetAssigneeDetails');
ajax.addParam('sysparm_name', 'RetrieveAssetDetails');
ajax.addParam('sysparm_req_for', requestedFor);
ajax.getXMLAnswer(ajaxResponse);
 
function ajaxResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
var returneddata = answer.evalJSON(true);
alert("Requestor has and existing Asset"+returneddata.assetDesc+", with the end date as "+returneddata.endDate);
return false;
}
}
}

 

Script Include (Client Callable) -

var GetAssetAssigneeDetails = Class.create();
GetAssetAssigneeDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
RetrieveAssetDetails : function(){
var asset = new GlideRecord('device_asset');
var reqUsr = this.getParameter('sysparm_req_for');
asset.addQuery('assigned_to', reqUsr);
asset.query();
if (asset.next()){
var json = new JSON();
var results = {
"endDate": asset.getValue('end_date'),
"assetDesc": asset.getValue('asset_description')
};
return json.encode(results);
}
else
{
return null;
}
},
})

 

Could you please let me know what i am doing wrong to display the field values in the alert message.

 

Thanks and Regards,

Somujit

 

 

1 ACCEPTED SOLUTION

Arpan Baishya
Kilo Sage

Hi @Somujit1,

 

I spotted an issue with the syntax you have used for the getXMLAnswer method. Also, it might be a good idea to stick to the stringify() and parse() methods of JSON to deal with the data that is being passed from server to client.

I have rewritten the scripts in a slightly different manner where I have taken the liberty of changing the variable names and function names to make them more pertinent to what they're being used for.

 

/*  Catalog Client Script */

function onLoad() {
    var requestedFor = g_form.getValue('requested_for');
    var ga_asset = new GlideAjax('GetAssetAssigneeDetails');
    ga_asset.addParam('sysparm_name', 'RetrieveAssetDetails');
    ga_asset.addParam('sysparm_req_for', requestedFor);
    ga_asset.getXMLAnswer(assetResponse);

    function assetResponse(answer) {
        //var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
            var returneddata = JSON.parse(answer);
            alert("Requestor has and existing Asset" + returneddata.assetDesc + ", with the end date as " + returneddata.endDate);
            return false;
        }
    }
}

 

 

 

/* Script Include */

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

    RetrieveAssetDetails: function () {

        var reqUsr = this.getParameter('sysparm_req_for');

        var gr_asset = new GlideRecord('device_asset');
        gr_asset.addQuery('assigned_to', reqUsr);
        gr_asset.setLimit(1);
        gr_asset.query();
        if (gr_asset.next()) {
            var results = {
                "endDate": gr_asset.getValue('end_date'),
                "assetDesc": gr_asset.getValue('asset_description')
            };

            return JSON.stringify(results);
        }

        return false;
    },

    type: 'GetAssetAssigneeDetails'
});

 


 Note: I have not tested out the code. Consider debugging both scripts if you come across any issues.

Hope this helps.

View solution in original post

1 REPLY 1

Arpan Baishya
Kilo Sage

Hi @Somujit1,

 

I spotted an issue with the syntax you have used for the getXMLAnswer method. Also, it might be a good idea to stick to the stringify() and parse() methods of JSON to deal with the data that is being passed from server to client.

I have rewritten the scripts in a slightly different manner where I have taken the liberty of changing the variable names and function names to make them more pertinent to what they're being used for.

 

/*  Catalog Client Script */

function onLoad() {
    var requestedFor = g_form.getValue('requested_for');
    var ga_asset = new GlideAjax('GetAssetAssigneeDetails');
    ga_asset.addParam('sysparm_name', 'RetrieveAssetDetails');
    ga_asset.addParam('sysparm_req_for', requestedFor);
    ga_asset.getXMLAnswer(assetResponse);

    function assetResponse(answer) {
        //var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
            var returneddata = JSON.parse(answer);
            alert("Requestor has and existing Asset" + returneddata.assetDesc + ", with the end date as " + returneddata.endDate);
            return false;
        }
    }
}

 

 

 

/* Script Include */

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

    RetrieveAssetDetails: function () {

        var reqUsr = this.getParameter('sysparm_req_for');

        var gr_asset = new GlideRecord('device_asset');
        gr_asset.addQuery('assigned_to', reqUsr);
        gr_asset.setLimit(1);
        gr_asset.query();
        if (gr_asset.next()) {
            var results = {
                "endDate": gr_asset.getValue('end_date'),
                "assetDesc": gr_asset.getValue('asset_description')
            };

            return JSON.stringify(results);
        }

        return false;
    },

    type: 'GetAssetAssigneeDetails'
});

 


 Note: I have not tested out the code. Consider debugging both scripts if you come across any issues.

Hope this helps.