Having problem with onLoad client script

Abhijit Das7
Tera Expert

Hi everyone,

I am working on onLoad Client script and script include . I am facing problem that I am not getting back response from script include . I have placed alert(res) , and it is giving null value.

 

Script Include:
var DisplayModelName = Class.create();
DisplayModelName.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getName: function() {

        var url_sysID = this.getParameter('sysparm_id');
		gs.info("sys id of url " +url_sysID);
		
	

        var table = new GlideRecordSecure('x_care3_carear_fsm_instruct_experience');
        table.addQuery('sys_id', url_sysID);
        table.query();
        if (table.next()) {
             var name = table.getValue('instruct_model_name');
			
			gs.info("name of model" +name);
        }

        return name;

    },

    type: 'DisplayModelName'
});

.

Client Script :
function onLoad() {
    //Type appropriate comment here, and begin script below
    var doc;
    var iframe = top.document.getElementById('gsft_main'); //main iframe
    var listIframe = iframe ? iframe.contentWindow.document.querySelector('[name=gsft_list_form_pane]') : null; //split view iframe
    //listIframe is null if iframe is null (when open record outside of frame, ie. not within nav_to) OR can not be determined by the selector above (for when the record is opened in regular form view, no split view)
    if (listIframe == null) {
        doc = iframe ? iframe.contentWindow.document : top.document;
    } else
        doc = listIframe.contentWindow.document; //listIframe exists when record is opened in split view, set doc to listIframe.contentWindow.document
    var element = doc.getElementById('wm_task.x_care3_carear_fsm_instruct_experience_value.instruct_model_url_link');
    alert(g_form.getValue('x_care3_carear_fsm_instruct_experience_value'));
    if (element)
        element = g_form.getValue('x_care3_carear_fsm_instruct_experience_value'); //set text of URL

    var ga = new GlideAjax('x_care3_carear_fsm.DisplayModelName');
    ga.addParam('sysparm_name', 'getName');
    ga.addParam('sysparm_id', element);
    ga.getXMLAnswer(modelName);

    function modelName(response) {
        var res = response.reponseXML.documentElement.getAttribute('answer');
        alert(res);
       



    }




}
7 REPLIES 7

Vaibhav Dane
Tera Expert
Tera Expert

Hi Virendra,

 

In the script include is Client Callable check box checked?

Also does gs.info prints any value?

 

Regards,

Vaibhav Dane

Hi @Vaibhav Dane 

Yup gs.info is returning correct values 

Ethan Davies
Mega Sage
Mega Sage

Hey Virendra,

I think your issue is going to be one of the following:

  1. Isolate Script On Client Script
    In your client script you are accessing the DOM. For a Client Script to be able to access the DOM you need to set the Isolate Script checkbox on the Client Script to TRUE. This Isolate Script field is not on the Client Script form by default so you may need to bring it to the form or your list view.
  2. Return Value from Script Include
    In your script you are returning the var name. The issue here is that your variable is declared inside the IF statement, meaning that if that statement is not true then you are trying to return a variable that has not been declared. Consider moving your variable declarations to the top of the function and just set the appropriate value of that variable inside your IF statement.

Please mark my answer correct or helpful if it solved your issue.

Hi @Ethan Davies 

 

Even after declaring variable outside if loop , it still gives alert(answer) as null.

Client Script :

element = g_form.getValue('x_care3_carear_fsm_instruct_experience'); //set text of URL

var ga = new GlideAjax('x_care3_carear_fsm.DisplayModelName');
    ga.addParam('sysparm_name', 'getName');
    ga.addParam('sysparm_id', element);
    ga.getXMLAnswer(modelName);

    function modelName(response) {
        alert('enter loop 2');
        var answer = response;
        alert(answer);

    }

And I am getting correct answers for all the gs.info in script include

var DisplayModelName = Class.create();
DisplayModelName.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getName: function() {

        var url_sysID = this.getParameter('sysparm_id');
        gs.info("sys id of url " + url_sysID);
        


        var name;

        var table = new GlideRecordSecure('x_care3_carear_fsm_instruct_experience');
        table.addQuery('sys_id', url_sysID);
        table.query();
        if (table.next()) {
            name = table.getValue('instruct_model_name');

            gs.info("name of model" + name);
        }
		
		var care = name ;
		
		gs.info('care'+care);

        return care;
		

    },


    type: 'DisplayModelName'
});