function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
divya manohardivya manohar 

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

 
Best Answer chosen by divya manohar
pconpcon
The following will do what you are looking to do

JSONDeserialize.cls
public class JSONDeserialize {
    public class CompanyContact {
        public String contact {get; set;}
        public String postalcode {get; set;}
        public String contactnumber {get; set;}
    }
    
    public class CompanyContactsWrapper {
        public List<CompanyContact> companyContacts {get; set;}
    }
    
    public CompanyContactsWrapper contactWrapper {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"}]}';
       
		contactWrapper = (CompanyContactsWrapper) JSON.deserializeStrict(jsonInput, CompanyContactsWrapper.class);
	}
}

JSONDeserialize.vfp
<apex:page controller="JSONDeserialize">
	<apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="submit" action="{!deserialize}" reRender="companyContactTable" />
            </apex:pageBlockButtons>
        
            <apex:pageBlockTable value="{!contactWrapper.companyContacts}" var="contact" id="companyContactTable" >
                <apex:column value="{!contact.contact}" headerValue="Contact" />
                <apex:column value="{!contact.postalcode}" headerValue="Postal Code" />
                <apex:column value="{!contact.contactnumber}" headerValue="Contact Number" />
            </apex:pageBlockTable>
        </apex:pageBlock>
	</apex:form>
</apex:page>

 

All Answers

pconpcon
Did you get this code from somewhere?  Do you have a line number associated with the error (if you look in the debug log it may give more information).  Is there a particular reason you are using JSON.createParser instead of deserializing this into a typed class?

NOTE: When providing code, please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
divya manohardivya manohar
Hi
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.
    
divya manohardivya manohar
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);
        system.debug(companyContacts);
    }
   
}
I am getting the error when I deserialzie the json input. I have modified the VF page , add a button to show error.
divya manohardivya manohar
<apex:page controller="Jsonexample_company">

<apex:form>

<apex:commandButton value="submit"
                   action="{!deserialize}"/>

{!companyContacts.contact}
{!companyContacts.postalcode}
{!companyContacts.contactnumber}

</apex:form>

</apex:page>

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)
pconpcon
The following will do what you are looking to do

JSONDeserialize.cls
public class JSONDeserialize {
    public class CompanyContact {
        public String contact {get; set;}
        public String postalcode {get; set;}
        public String contactnumber {get; set;}
    }
    
    public class CompanyContactsWrapper {
        public List<CompanyContact> companyContacts {get; set;}
    }
    
    public CompanyContactsWrapper contactWrapper {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"}]}';
       
		contactWrapper = (CompanyContactsWrapper) JSON.deserializeStrict(jsonInput, CompanyContactsWrapper.class);
	}
}

JSONDeserialize.vfp
<apex:page controller="JSONDeserialize">
	<apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="submit" action="{!deserialize}" reRender="companyContactTable" />
            </apex:pageBlockButtons>
        
            <apex:pageBlockTable value="{!contactWrapper.companyContacts}" var="contact" id="companyContactTable" >
                <apex:column value="{!contact.contact}" headerValue="Contact" />
                <apex:column value="{!contact.postalcode}" headerValue="Postal Code" />
                <apex:column value="{!contact.contactnumber}" headerValue="Contact Number" />
            </apex:pageBlockTable>
        </apex:pageBlock>
	</apex:form>
</apex:page>

 
This was selected as the best answer