guidewiregosu

Can you use java script in guidewire?


Instead of using a Link in guidewire I want to use a button to kick off a URL using POST.. example:

<form name="input" method="Post" id="aerForm" name="aerForm" action="https://geico.com">
    <input type="hidden" name="rs:command" id="rs:command" value="test"/>
    <input type="hidden" name="rs:Format" id="rs:Format" value="PDF"/>
    <input type="hidden" name="rc:Toolbar" id="rc:Toolbar" value="false"/>
    <input type="submit" value="Get your total">
</form>

I am not sure how to use this in studio or if there is a different way to achieve this. Does anyone have experience with this?


Solution

  • You can use javascript to do that.

    1. En customer.js file you need to add a function like this.

    function createFormAndSubmit(formTxt)
    {
    	formTxt = formTxt.split("&lt;").join("<");
    	formTxt = formTxt.split("&gt;").join(">");
    	formTxt = formTxt.split("'post'>").join("'post' >");
    	var parser = new DOMParser();
    	var formElement = parser.parseFromString(formTxt,"text/xml").documentElement;
    
    	var newForm = document.createElement('FORM');
    	newForm.name = 'NewForm';
    	newForm.method = 'POST';
    	newForm.action = formElement.getAttribute("action");
    	newForm.target = 'NewFormTab';
    	newForm.style.display = "none";
    
    	for(var i = 0; i < formElement.childNodes.length; i++) {
    		var node = formElement.childNodes[i];
    		var name = node.getAttribute("name");
    		var value = node.getAttribute("value");
    		if (name == "null") {
    			continue;
    		}
    		var inputbox = document.createElement('INPUT');
    		inputbox.type = 'HIDDEN';
    		inputbox.name = name;
    		inputbox.value = value;
    		newForm.appendChild(inputbox);
    	}
    	document.body.appendChild(newForm);
    	newForm.submit();
    }

    1. In PCF file you need to add a TemplatePanel like this.

    <TemplatePanel><![CDATA[<script>function callMyForm() {createFormAndSubmit("${new organization.package.MyClass().getFormString(policyPeriod)}")}</script>]]></TemplatePanel>

    1. In the Button action call the created function

    <ButtonInput action="javascript:callMyForm()" id="MyFormButton" value="displaykey.MyFormButtonLabel"/>