1 2 Previous Next 17 Replies Latest reply on Dec 5, 2014 11:00 AM by Laurent Matheo

    Issue with running javascript on form - function undefined

      Share:|

      Hi all.  I'm currently trying to implement some javascript onto my remedy form.  However, whenever I do a run process of javascript testCall("12345") or javascript xmlParse("test"), I get function is undefined.  I've read the articles on this website by Laurent Matheo and Sylvain YVON where they detail how to get this working but I'm not sure what's wrong.

       

      In the Web Header Content property I have the following where webMethod is first part of URL to my web service and parameters is the second part:

       

      Note: I built this in an html file to first just test outside of the remedy world to make sure it's working properly and it is.  It's only not working in Remedy.

       

      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" defer></script>

      <script language="javascript">

       

       

      var myarray=new Array();

      myarray["field1"]="arid_WIN_0_636865050";

       

      function xmlParse(name) {

        var result = $(XML).find(name).text(); {

        $("#"+myarray["field1").val($("#"+myarray["field1").val() + result);

        }

      }

       

      function testCall(pin) {

        var pin = pin;

        var webMethod = webMethod;

        var parameters = parameters;

       

        $.ajax({

        url: webMethod,

        type: 'Post',

        data: parameters,

        contentType: "application/json",

        dataType: 'jsonp'

        );

      }

       

      function outputCall(xmlMsg) {

        XML = xmlMsg;

      }

       

      </script>

       

       

      Thanks in advance.

        • 2. Re: Issue with running javascript on form - function undefined
          Sylvain YVON

          Hi Brice,

          As Kenny mentions you have some basic errors in your JS code.

          You also miss a "success" callback function in your ajax call (and a "fail", for it matters). You should use dataType json, not jsonp. Your xmlParse is wrong too.

          Is it the code you tried in your html page, and in your webHeader ?
          How do you pass the url & parameters ?

          1 of 1 people found this helpful
          • 3. Re: Issue with running javascript on form - function undefined

            Hi Kenny and Sylvain, thank you both for your replies.  Sylvain, so my code actually works if I run it in IE or Chrome.

             

            I'm not actually using "success" or "error" because I'm actually basically just "tricking" the ajax/js that outputCall function will actually return the XML web service message.  Basically I have a parameter inside of the parameters that specifies "restjsonp=outputCall".  So that's why I'm setting a global variable inside of that function to be used in my Parse function.  What I have in the web header content is what I have in the OP.  I added some HTML elements to just try it in a browser but instead of the myarray stuff I'm just setting an html field so it looks like:

             

            $("xmlResponse").val($("#xmlResponse").val() + result);

             

            This is basically what I'm doing for the URL and parameters:

             

            var webMethod = "https://mydomain.com/baorestful/HSVC_RIT_ESP/Hold%20and%20Release%20Hold?";

            var parameters = "restjsonp=outputCall&restuser=TEMPESP&restpass=pass&someotherparams";

             

            Right now that will do but I plan on making it dynamic to what people are entering on an SRM form.  I have a good idea how that will work though.

             

            Kenny, I'm going to try those syntax fixes right now.  I was looking at it forever trying to find syntax errors so it's always nice to get another eye on your code.  Thanks!

             

            I'll let you guys know of my results.  Crossing my fingers that this works

             

            edit: Syntax fixes worked Thanks guys!

            • 4. Re: Issue with running javascript on form - function undefined

              Huh...during testing your code in ie8, seems defer was not supported, i change to defer="defer" still not work.

               

              Forget it...I will create a request to helpdesk to replace my win xp

              :P

              • 5. Re: Issue with running javascript on form - function undefined

                Hey Kenny.  I'm not sure you actually need defer, I've tried it with and without it and it worked.  I just had it in as a safety precaution.  I luckily know that no one will be using this on anything lower than IE9 and we're planning on rolling out IE11 Q2 next year.  Plus, you're going to need a web service call to actually have it work.  I gave you guys bogus WS URL as it's all local to my company's network.

                • 6. Re: Re: Issue with running javascript on form - function undefined
                  Laurent Matheo

                  Btw., calling JQuery might not be a good idea here. JQuery (well, an old version, I agree, 1.4.x or something) is already loaded by the Mid-Tier.

                  If you load another instance of it, you could have some weird issues going on.

                  "Simple" ajax calls shouldn't need a new version of JQuery, should work correctly with the Mid-Tier's version

                   

                  To read and write to ARS fields, you should also use the "ARS" functions to avoid problems and handling .val() or .text().

                  F(windowID,536870918).G(); 
                  

                  https://communities.bmc.com/thread/112993?start=30&tstart=0

                   

                  F(0,536870916).S(new CharType("Incident Number = INC000000064311")); 
                  

                  javascript or jquery help with bmc remedy

                  • 7. Re: Issue with running javascript on form - function undefined

                    So, I removed the jquery call and just left my header as:

                     

                    <script type="text/javascript"></script>

                    <script language="javascript">

                     

                    I almost want to take out the script language but I read somewhere on one of these threads that you need that for Remedy to know it's javascript, not sure how true that is.

                     

                    So now I'm getting an "Access Denied" message.  I'm getting it in my ajax call because I'm not getting the alert that says it finished.  Obviously I only have alerts in there for debugging purposes.  Any ideas why I would get access denied?  Please note that my web service is defintely fine.  I've tested it locally and I also have an alternate way I'm considering handling my issue with active links and a Filter that makes an AO call (my web service is coming from AO).  Those both work and my JS used to work, I didn't always get "Caught exception: Access Denied". 

                     

                    My JS looks something like this:

                     

                    <script type="text/javascript"></script>

                    <script language="javascript">

                     

                     

                    function testCall(action, jobName, dataCenter, option, pin, name) {

                      var myarray=new Array();

                      myarray["field1"]="636865050";

                      var action = encodeURI(action);

                      var jobName = encodeURI(jobName);

                      var dataCenter = encodeURI(dataCenter);

                      var option = encodeURI(option);

                      var pin = encodeURI(pin);

                      name = name;

                      var webMethod = "https://test.com/" + action +"?";

                      var parameters = "restjsonp=outputCall&restuser=TEMPESP&restpass=pass1&JobName=" + jobName + "&DataCenter=" + dataCenter + "&Option=" + option + "&PinToSearchFor=" + pin + "&restdebug=True";

                     

                     

                      alert(name);

                      alert(webMethod + parameters);

                     

                      $.ajax({

                      url: webMethod,

                      type: 'Post',

                      async: false,

                      data: parameters,

                      contentType: "application/json",

                      dataType: 'jsonp'

                      });

                     

                      alert("ajax call finished");

                    }

                     

                     

                    function outputCall(xmlMsg) {

                      alert(name);

                      XML = xmlMsg;

                      alert(XML);

                      var result = $(XML).find(name).text(); {

                      alert(result);

                      F(0,myarray["field1"]).S(new CharType(result));

                      }

                    }

                     

                     

                    </script>

                    • 8. Re: Issue with running javascript on form - function undefined
                      Sylvain YVON

                      I thought you had it there for testing the html page. You don't need the first line.

                       

                      You're probably getting the access denied message because what you are doing is calling XSS, cross site scripting. This is when a JS functions in a page taken from a domain is trying to reach for another domain.

                      To enable XSS, the second domain has to explicitely give the browser permission to do this.

                       

                      Read this for more informations : https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

                      • 9. Re: Issue with running javascript on form - function undefined

                        It's actually on the same domain, and that outputCall is something set up in the servelet that is a workaround for CORS.  The call DID actually work too, when I started modifying other things in the code is when I got that error.  It would seem that the error is indeed coming from the call but I've verified from the same source that it works (on same server calling web service).  I'm realizing that this might be out of scope of help from on here due to no knowledge of the environment.  If you can think of anything else that would be awesome, I appreciate everyone's help.

                         

                         

                        edit:  I did some research to find out what the exact version of jQuery is loaded with mid-tier and it's 1.4.2 which is the reason why I'm erroring out because it doesn't support how I'm doing the ajax call.

                        • 10. Re: Issue with running javascript on form - function undefined
                          Laurent Matheo

                          So is it working now?

                          Btw, if it's on the same domain, you shouldn't use jsonp which is used for cross domain.

                          • 11. Re: Issue with running javascript on form - function undefined

                            No, it's not working.  It's not working in my local HTML copy now that I'm using the 1.4.2 library either.  I'm contemplating just having that file call a js that will run the call in a version that was working for me.

                             

                            I tried doing it without the jsonp and that wasn't working which comes into play with the whole parameter for outputCall in my rest URL parameter.  Basically, it's "tricking" the js and passing the XML back through that function instead of the ajax call.  The ajax call technically errors but then outputCall returns the XML.

                            • 12. Re: Issue with running javascript on form - function undefined
                              Sylvain YVON

                              Brice,

                              Why do you have to use this workaround for CORS if you are in the same domain then ?
                              Did you spot the source of the "Access Denied" error with a debugging tool ?

                              • 13. Re: Issue with running javascript on form - function undefined

                                So while they are technically on the same domain, they don't share the same naming structure. i.e. test-remedy, test-ao.  The way I understand "cross domain" is it actually needs the same name, test-remedy, test-remedy.  I don't have control over the web service.  Without getting too much into the details, I spoke with the guy that developed the web service and he was initially looking into incorporating CORS but then decided against this as he was able to create the "work around".  So I don't have the ability to change the web service. 

                                 

                                Going back to my error message.  Just to recap, it worked fine when I was using 2.1.1 in my test HTML code.  When I put the same version that mid-tier uses in my HTML test, 1.4.2, it stopped working.  I'm debugging by just throwing alerts into the code for each step and it's erroring in the web service call.

                                 

                                This is currently what my ajax call looks like using 1.4.2:

                                 

                                $.ajax({

                                  url: webMethod,

                                  type: 'Post',

                                  data: parameters,

                                  contentType: "application/json",

                                  dataType: 'jsonp'

                                  });

                                 

                                I've been searching the web for 1.4.2 documentation and it would appear that everything I'm doing in the call should work but obviously it's not.

                                 

                                I'm considering just having the mid-tier version call another js file on the server and load 2.1 with that to do the call but I'm not sure if that will just cause me to run into the same issue where it's conflicting with the 1.4.2 version on the mid-tier because I don't know if the new js file will still be considered to be a part of the mid-tier if I call it from within remedy.

                                • 14. Re: Re: Issue with running javascript on form - function undefined
                                  Laurent Matheo

                                  Weird. I don't have the time to check right now, maybe this week end. Two things I can tell though:

                                   

                                  1°) View field:

                                  You can create a "view" field on the form, call a html file inside of it (set field?).

                                  This way it'll be a totally separate "page" and you can load whatever you like.

                                  Since it's an Iframe for ARS, you can pass / get values using standard javascript functions from the "form" to the "view".

                                  I don't have an example right now I'm on remote, but you got the idea.

                                   

                                  Add a "view" field to the form. Create your html page with your code and calling your version of jquery. The html page does the ajax, parse the answer and send information to the parent form using javascript functions.

                                  Something like:

                                  "parent.F(" stuff since "F" is in the parent (iframe is a "child").

                                   

                                   

                                  2°) Not aynchronous calls:

                                  I know I used that and it worked with JQuery 1.4.2.

                                  It's something like:

                                   

                                  my_url="http://whatever.com/test.php";
                                  
                                  //endpoint url must handle jsonp...
                                  is_jsonp=true;
                                  
                                  
                                  //for jsonp
                                  if (is_jsonp){
                                      my_url=my_url+"&callback=?";
                                  }
                                  
                                  $.getJSON(my_url,
                                      {
                                      },
                                      function(json){
                                           //Function to handle the result
                                           //handle_json_answer_list(fieldid,menuname,json)
                                           });
                                  

                                   

                                   

                                  3°) Proxy:

                                  As sylvain told you, you can't Cross Domain unless you use "CORS". Another option that you can use is using a "proxy".

                                  The idea is simple.

                                  Instead of your page on "company.com" calling directly "disney.com", you call a "jsp" page that is on your Mid-Tier, so on the same domain ("company.com") and you pass it the parameters of your query.

                                  The "jsp" does actually the call to "disney.com" and asnwers back.

                                  So this way you don't have to worry about cross domains or stuff since the data query is done by the "jsp" page.

                                  1 of 1 people found this helpful
                                  1 2 Previous Next