Here in this tip you would come to know how to run it.
1. Create a suitelet in which you will generate pdf and run JavaScript usingnlapiXMLToPDF using the following code..
function RunJavaScript (request, response) { try { //Create the xml var xml = "<?xml version='1.0'?>"; xml += "<pdf>"; //Add the head and body part to it xml += "<head>"; xml += "</head>"; xml += "<body>"; xml += "</body>\n</pdf>"; // convert the xml document to a PDF(fileobj) var file = nlapiXMLToPDF(xml); //generate a file response.setContentType('PDF', 'javascript.pdf ', 'inline'); // write response to the client response.write(file.getValue()); } catch (ex) { nlapiLogExecution('DEBUG', 'error', ex); } }
2. Add the form elements like text box, radio buttons that are to be inserted in pdf in xml, add them in <body> tag which was added above. Here I have added two fields one is of type ‘text’ and other is of ’radio’ button type.
//Add to the table the required fields var strName = " <table><tr>"; //Add the name field strName += "<td>Name</td>"; strName += "<td><input type='text' name='name' width=\"10em\"/></td>"; strName += "</tr><tr>"; //add the gender field strName += "<td>Sex</td>"; strName += "<td>"; strName += "Male <input display=\"inline\" type=\"radio\" name=\"sex\" value=\"male\" padding-right=\"0.2in\"/>"; strName += "Female <input display=\"inline\" type=\"radio\" name=\"sex\" value=\"female\"/>"; strName += "</td>"; strName += "</tr><tr>"; //Add it to the body xml += "<body>"; Here it is getting added to xml part inside the <body> tag same as we use to do in HTML. xml += strName; xml += "</table>"; xml += "</body>\n</pdf>";
The key things to remember is that the “name” and “type” values are mandatory and each name must be unique across the entire document i.e. the pdf.
3. Now add the JavaScript in the xml <head> tag. Same as use to do in HTML but use “CData” block instead of < and > characters in which JavaScript code is written.
//Add the script in head xml += "<head>"; xml += "<script>"; xml += "<![CDATA["; xml += "function RunScript()"; xml += "{"; xml += "app.alert('hello');"; xml += "}"; xml += "]]>"; xml += "</script>"; xml += "</head>";
To run this JavaScript add the button name” submit” in the <body> tag. On click of that button assign the JavaScript function (javascript:RunScript()) .
xml += “<input type=’button’ name=’submit’ onClick=’javascript: RunScript()’/>”;
On the button click the JavaScript function named “RunScript” would run which will show alert message.