Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Unable to replace special character in Script include and Business Rules

ideamax
Mega Expert

Hello All,
I am facing a problem in xml creation due to special characters. I am creating xml in script include by using parameters from business rule. There are some string which contains special symbol like (&,') .

To replace them with corresponding html name or html code I am using following code :
mystring.replace("&", "&") or mystring.replace("&", "&")

but , When I save this business rule, it changes the above line to
mystring.replace("&", "&");

Please suggest a way or approach to prevent this .

11 REPLIES 11

It sounds like you are trying to modify a script include after-the-fact and save it?


If you want to encode strings for XML then you can use this:


JSUtil.escapeText(my_string);


- James


HI ,


I tried to used both package call and JSUtil to escape, but it did not work


I got SAX Exception, prolog not allowed... please advise , below is the snippet:


var sm = new SOAPMessage('function1', 'Query');
            sm.setStringParameter('id',id);
            sm.setStringParameter('Code',Query);
            sm.setStringParameter('Password','123');
            sm.setStringParameter('Username','abc');
            sm.post();



            var k=1;
            var response = sm.getResponse();
           
            while (response == null) {
                              //gs.log("waiting for response ... " + k + " seconds");
                              response = sm.getResponse(1000);
                              k++;


                              if (k > 30) {
                                gs.print("validateClarityAjax Response exceeded 30 serc . " + k + " seconds");


                                      status = "408"; // request timeout
                                      response = "timed out waiting for SOAPProbe response, please check the ECC queue at a later time";
                                      break; // service did not respond after 60 tries
                              }
                  }


                            gs.log('validateClarityAjax : Response:' +response);



                            // var clean=JSUtil.escapeText(response.toString());


                              var clean=Packages.org.apache.commons.lang.StringEscapeUtils.escapeXml(response.toString());



                   
                        var xmldoc = new XMLDocument(clean);



                            projName=xmldoc.getNodeText("//name");
                           
                            gs.log(projName);


sindhuja2
Kilo Explorer

HI ,


I tried to used both package call and JSUtil to escape, but it did not work


I got exception:


org.xml.sax.SAXParseException: Reference is not allowed in prolog.


Evaluator: java.lang.NullPointerException




.. please advise , below is the snippet:


var sm = new SOAPMessage('function1', 'Query');
            sm.setStringParameter('id',id);
            sm.setStringParameter('Code',Query);
            sm.setStringParameter('Password','123');
            sm.setStringParameter('Username','abc');
            sm.post();



            var k=1;
            var response = sm.getResponse();
           
            while (response == null) {
                              //gs.log("waiting for response ... " + k + " seconds");
                              response = sm.getResponse(1000);
                              k++;


                              if (k > 30) {
                                gs.print("validateClarityAjax Response exceeded 30 serc . " + k + " seconds");


                                      status = "408"; // request timeout
                                      response = "timed out waiting for SOAPProbe response, please check the ECC queue at a later time";
                                      break; // service did not respond after 60 tries
                              }
                  }


                            gs.log('validateClarityAjax : Response:' +response);



                            // var clean=JSUtil.escapeText(response.toString());


                              var clean=Packages.org.apache.commons.lang.StringEscapeUtils.escapeXml(response.toString());



                   
                        var xmldoc = new XMLDocument(clean);



                            projName=xmldoc.getNodeText("//name");
                           
                            gs.log(projName);


Check the line "sm.setStringParameter('Code',Query);"



Probably "Query" should be quotes unless it is a variable declared earlier in the script.


geoffcox
Giga Guru

If you are creating XML that is ultimately being processed by jelly, you should substitute the ampersand with $[AMP] or ${AMP} (depending on which jelly pass you want it process on). Does this help?