• Marjan Koneski
  • NEWBIE
  • 10 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hello, so i want to connect my visualforce page to a class that get something from an api. And that to be shown on the home page.

here my class code
 
public class CatFactsDaily {
    
     public String catFact3{get;set;}
    
    public void toGetFacts() {
        String requestEndPoint = 'https://catfact.ninja/fact';
        
        Http http=new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(requestEndPoint);
        request.setMethod('GET');
        HttpResponse response=http.send(request);
        
        if(response.getStatusCode()==200){
            
            Map<String, Object> results=(Map<String, Object>) JSON.deserializeUntyped(response.getBody());
             catFact3=String.valueOf(results.get('fact'));           
            
        }
        
        else {
            ApexPages.Message myMsg=new ApexPages.Message(ApexPages.Severity.ERROR, 'There was an error in reading Data');
            ApexPages.addMessage(myMsg);
        }
        
    }

}

here is my visualforce code
 
<apex:page controller="CatFactsDaily" >
    
     <apex:pageBlock title="Cat Fact of the day!!">
        
        <apex:pageBlockSection >
            
            <apex:pageMessages ></apex:pageMessages>
            
            <apex:outputText label="CatFact" value="{!catFact3}"></apex:outputText>
        
        </apex:pageBlockSection>
    
    </apex:pageBlock>
    
</apex:page>

and this is what i get

User-added imageI ve tried this same api with for and account, and it works, but i cannot make it work for the home page​​​​​​​
Hello, so i want to connect my visualforce page to a class that get something from an api. And that to be shown on the home page.

here my class code
 
public class CatFactsDaily {
    
     public String catFact3{get;set;}
    
    public void toGetFacts() {
        String requestEndPoint = 'https://catfact.ninja/fact';
        
        Http http=new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(requestEndPoint);
        request.setMethod('GET');
        HttpResponse response=http.send(request);
        
        if(response.getStatusCode()==200){
            
            Map<String, Object> results=(Map<String, Object>) JSON.deserializeUntyped(response.getBody());
             catFact3=String.valueOf(results.get('fact'));           
            
        }
        
        else {
            ApexPages.Message myMsg=new ApexPages.Message(ApexPages.Severity.ERROR, 'There was an error in reading Data');
            ApexPages.addMessage(myMsg);
        }
        
    }

}

here is my visualforce code
 
<apex:page controller="CatFactsDaily" >
    
     <apex:pageBlock title="Cat Fact of the day!!">
        
        <apex:pageBlockSection >
            
            <apex:pageMessages ></apex:pageMessages>
            
            <apex:outputText label="CatFact" value="{!catFact3}"></apex:outputText>
        
        </apex:pageBlockSection>
    
    </apex:pageBlock>
    
</apex:page>

and this is what i get

User-added imageI ve tried this same api with for and account, and it works, but i cannot make it work for the home page​​​​​​​