You need to sign in to do that
Don't have an account?

unable to display return type in vf page
Hi
I am trying to understand basics of displaying data in visual force page.
Below is the code:
public class HttpExample3
{
public string result{get;set;}
public Double grand_total{get;set;}
public InvoiceList inv{get;set;}
public string callservice()
{
Http p = new Http();
HttpRequest request = new HttpRequest();
request.setEndPoint('https://docsample.herokuapp.com/jsonSample');
request.setHeader('Content-type', 'application/json');
request.setMethod('GET');
HttpResponse response=p.send(request);
return response.getBody();
}
public void GetTotalPrice()
{
grand_total=InvoiceList.TotalPrice(callservice());
}
}
<apex:page controller="HttpExample3">
<apex:form>
<apex:pageBlock title="GetResponse">
<apex:pageblockSection>
<apex:pageblockSectionItem>
<apex:commandButton value="GetResponse"
action="{!callservice}"/>
</apex:pageblockSectionItem>
</apex:pageblockSection>
</apex:pageBlock>
<apex:pageBlock title="TotalPrice">
<apex:pageblockSection>
<apex:pageblockSectionItem>
<apex:commandButton value="GetTotalPrice"
action="{!GetTotalPrice}"/>
</apex:pageblockSectionItem>
</apex:pageblockSection>
{!grand_total}
</apex:pageBlock>
</apex:form>
</apex:page>
public class InvoiceList
{
public static Double totalPrice{get;set;}
public DateTime statementDate {get;set;}
public List<LineItems> lineItems {get;set;}
public Integer invoiceNumber {get;set;}
public static double grand_total=0.0;
public static double TotalPrice(string jsonString)
{
System.JSONParser jp=JSON.createParser(jsonString);
while (jp.nextToken() != Null)
{
if ((jp.getCurrentToken() == JSONToken.FIELD_NAME) && jp.getText() == 'totalPrice')
{
jp.nextToken();
grand_total+=jp.getDoubleValue();
}
}
return grand_total;
}
public class LineItems
{
public Double UnitPrice {get;set;}
public Double Quantity {get;set;}
public String ProductName {get;set;}
}
}
I am making callservice method have return type because I need the response in other calculations.
code works fine when I click Get Total price button , but it fails when I click callservice button.
I get error as : he name can only contain underscores and alphanumeric characters. It must begin with a letter and be unique, and must not include spaces, end with an underscore, or contain two consecutive underscores. "
Please help me .
divya
I am trying to understand basics of displaying data in visual force page.
Below is the code:
public class HttpExample3
{
public string result{get;set;}
public Double grand_total{get;set;}
public InvoiceList inv{get;set;}
public string callservice()
{
Http p = new Http();
HttpRequest request = new HttpRequest();
request.setEndPoint('https://docsample.herokuapp.com/jsonSample');
request.setHeader('Content-type', 'application/json');
request.setMethod('GET');
HttpResponse response=p.send(request);
return response.getBody();
}
public void GetTotalPrice()
{
grand_total=InvoiceList.TotalPrice(callservice());
}
}
<apex:page controller="HttpExample3">
<apex:form>
<apex:pageBlock title="GetResponse">
<apex:pageblockSection>
<apex:pageblockSectionItem>
<apex:commandButton value="GetResponse"
action="{!callservice}"/>
</apex:pageblockSectionItem>
</apex:pageblockSection>
</apex:pageBlock>
<apex:pageBlock title="TotalPrice">
<apex:pageblockSection>
<apex:pageblockSectionItem>
<apex:commandButton value="GetTotalPrice"
action="{!GetTotalPrice}"/>
</apex:pageblockSectionItem>
</apex:pageblockSection>
{!grand_total}
</apex:pageBlock>
</apex:form>
</apex:page>
public class InvoiceList
{
public static Double totalPrice{get;set;}
public DateTime statementDate {get;set;}
public List<LineItems> lineItems {get;set;}
public Integer invoiceNumber {get;set;}
public static double grand_total=0.0;
public static double TotalPrice(string jsonString)
{
System.JSONParser jp=JSON.createParser(jsonString);
while (jp.nextToken() != Null)
{
if ((jp.getCurrentToken() == JSONToken.FIELD_NAME) && jp.getText() == 'totalPrice')
{
jp.nextToken();
grand_total+=jp.getDoubleValue();
}
}
return grand_total;
}
public class LineItems
{
public Double UnitPrice {get;set;}
public Double Quantity {get;set;}
public String ProductName {get;set;}
}
}
I am making callservice method have return type because I need the response in other calculations.
code works fine when I click Get Total price button , but it fails when I click callservice button.
I get error as : he name can only contain underscores and alphanumeric characters. It must begin with a letter and be unique, and must not include spaces, end with an underscore, or contain two consecutive underscores. "
Please help me .
divya
JSONDeserialize.cls
JSONDeserialize.vfp
All Answers
NOTE: When providing code, please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
I hv not got the code from anywhere. I am trying to understand the concept.
I have modified the apex class and vf page to give a clear error picture.
public class Jsonexample_company
{
public CompanyContactsWrapper companyContacts{get;set;}
public void deserialize()
{
string jsonInput='{"CompanyContacts":[{"contact": "pn0","postalcode": "0","contactnumber": "pc0"},{"contact": "pn1","postalcode": "1","contactnumber": "pc1"},{"contact": "pn2","postalcode": "2","contactnumber": "pc2"}]}';
/*line 11:*/ companyContacts=(CompanyContactsWrapper)JSON.deserializeStrict(jsonInput,CompanyContactsWrapper.class);
}
}
<apex:page controller="Jsonexample_company">
<apex:form>
<apex:commandButton value="submit"
action="{!deserialize}"/>
{!companyContacts.contact}
{!companyContacts.postalcode}
{!companyContacts.contactnumber}
</apex:form>
</apex:page>
error :Unknown field: CompanyContactsWrapper.CompanyContacts
Error is in expression '{!deserialize}' in component <apex:commandButton> in page vf_company: (System Code)
Class.Jsonexample_company.deserialize: line 11, column 1
An unexpected error has occurred. Your solution provider has been notified. (System)
I am doing the above because I want to have 2 methods in apex class which will serialize and deserialize the json input.
Hope I am clear.
JSONDeserialize.cls
JSONDeserialize.vfp