You have to create custom class and then add it as extension to standard controller.Fore main webservice call from an external system call the Java Function defined in page which is calling RemoteAction defined in class. The Http Response and Request are defined in class The page and the class would look like this,
<apex:page standardController="STD_CTRL__C" id="pg" extensions="
CustomRestClass"> <apex:form id="frm"> <apex:pageBlock title="Calculate Total Production"
id="pgBlock1" > <apex:pageBlockSection id="secCalc" > <apex:commandButton value="Save" action="{!save}"
id="saveButton" /> <apex:commandButton value="Cancel" action="{!cancel}"
id="cancelButton"/>
<!-- Biniding input and output field values to Objects -->
<apex:inputField id="A1=" value="{!value}" /><apex:inputField id="A2=" value="{!value}" /><apex:outputField id="output1" value="{!value}" > </apex:outputField><apex:commandButton onclick="CalculateValueFromExternalSRC(); "
value="Calculate Production" rerender="secCalc"/> </apex:pageBlockSection>
</apex:pageBlock> </apex:form><body><script>CalculateValueFromExternalSRC()var inputA1, inputA2, output;<!-- get input values ids from VF page to java script>inputA1 = j$("[id$='A1']").val(); inputA2 = j$("[id$='A12']").val();<!-- assign output to pageblocksections particular output field)output = "{!$Component.frm.pgBlock1.secCalc.output1}";Visualforce.remoting.Manager.invokeAction( '{!$RemoteAction.CustomRestClass.getProduction}', address,azimuth,systemLosses,kilowatts,tilt,ArrayStr,'{!PV_Watts__c.id}', function(result, event){ document.getElementById(output).innerHTML = result;</script> </body> </apex:page>Classpublic class CustomRestClass { public static String production{get;set;} public static ID accid; custOBJ__c obj ; ApexPages.StandardController sController; public CustomRestClass (ApexPages.StandardController controller) { sController = controller; obj = (custOBJ__c)controller.getRecord();} public PageReference save() { // write your save logic here } update obj;PageReference pvPage = new ApexPages.StandardController(obj).view(); pvPage.setRedirect(true); return pvPage; } @RemoteAction public static String getProduction(String address,String azimuth,String systemLosses,String kilowatts,String tilt,String ArrayStr,Id recId){ String response = ''; Decimal acAnnual=0; // sample input Double RoundedProd=0; custOBJ pvObj = new custOBJ(); try{ String url = 'https://developer.API_KEY_TO_EXTERNAL_WEBSERVICE'; Http h=new Http(); HttpRequest req= new HttpRequest(); req.setEndpoint(url); req.setMethod('GET'); HttpResponse res = h.send(req); response = res.getBody(); JSONParser parser = JSON.createParser(response); while (parser.nextToken() != null) { if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText() == 'ac_annual')) { parser.nextToken(); response = '{response:'+String.valueOf(acAnnual)+'}'; production=response; } } } catch(Exception e){ response = e.getMessage(); System.debug(response); } return JSON.serialize(AcAnnual); } }
Comments
Post a Comment