How to set Field values " script include to client side" when form Load

Kishore8
Kilo Guru

Script Include :

var HelloWorld   = Class.create();

HelloWorld .prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

      Approve: function(a) {

                    return "The Server Says Hello " + this.getParameter('sysparm_user_name') + "!" ;

    }

});

client Side OnLoad:                                                                                                                                                                                              

       

function onLoad() {                                                                                                                                                                                                      

    //Type appropriate comment here, and begin script below

    var ga = new GlideAjax('HelloWorld');

ga.addParam('sysparm_name','Approve');

ga.addParam('sysparm_user_name',"Bob");

ga.getXMLWait();

alert(ga.getAnswer());

}

i would like to set values

state=3,category=software, short_description=''welcome to scripting"     i want to write this code in script include and i would like to call that server code when   i load a form   in client side, this values     should set into field.

1 ACCEPTED SOLUTION

Hi,



The client script code is using parse() function and I had provided a different one. You can ignore the warning message on script include, Please copy and replace these scripts exactly and check if it is working. Also see if alerts are prompted.



function onLoad() {


  //Type appropriate comment here, and begin script below


  var ga = new GlideAjax('HelloWorld');


  ga.addParam('sysparm_name','Approve');


  ga.addParam('sysparm_user_name',"Bob");


  ga.getXML(HelloWorldParse);



  function HelloWorldParse(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");



  answer = answer.evalJSON(); //Transform the JSON string to an object


  alert(answer);


  alert(answer.short_description);



  g_form.setValue("short_description", answer.short_description);


  g_form.setValue("category", answer.category);


  g_form.setValue("state", answer.state);



  }


}





var HelloWorld   = Class.create();


HelloWorld.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {



  Approve: function(a) {


  var obj = {short_description: "welcome to scripting", state: 3, category: "software"};


  var data = new JSON().encode(obj); //JSON formatted string


  return data;


  }



  });




Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

16 REPLIES 16

Alikutty A
Tera Sage

Hi Kishore,



You should be using Asynchronous Ajax calls on OnLoad client scripts as a best practice. Try this code and let me know if it works



On your client script



function onLoad() {


  //Type appropriate comment here, and begin script below


  var ga = new GlideAjax('HelloWorld');


  ga.addParam('sysparm_name','Approve');


  ga.addParam('sysparm_user_name',"Bob");


  ga.getXML(HelloWorldParse);



  function HelloWorldParse(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  answer = answer.evalJSON();


  g_form.setValue("short_description", answer.short_description);


  g_form.setValue("category", answer.category);


  g_form.setValue("state", answer.state);


    }


}



On your script include (Mark it as client callable)



var HelloWorld   = Class.create();


HelloWorld .prototype = Object.extendsObject(global.AbstractAjaxProcessor, {



  Approve: function(a) {


  var obj = {short_description: "welcome to scripting", state: 3, category: "software"};


  var json = new JSON();


  var data = json.encode(obj); //JSON formatted string


  return data ;


  }



});




Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


Very Thanks to you,



i am very glad with your replay..


but there are showing error...



find_real_file.png


Can you plse correct this .....



and plse help me


Sorry ,This code is not working....


Sorry , This code is not working, ,


result   was not displayed in fields...