4

i am building a app which takes input from user and then search to a remote database(MySql).And then display the retrieved data on VisualForce page.One important note that my app will be distributable.

here is my controller class

public with sharing class Search 
{ 
  public String strTag {get;set;}
  public String strlocation {get;set;}

  public PageReference find() {       

     return null;
  }
}

Thanks in advance.

Updated

public with sharing class Search 
{ 

public String strTag {get;set;}
public String strlocation {get;set;}
  public PageReference find() {         

  HttpRequest req = new HttpRequest();
  HttpResponse res = new HttpResponse();
  Http http = new Http();

  req.setEndpoint('https://localhost/salesforce/');
  req.setMethod('POST');


  req.setCompressed(false);
  req.setBody('key1=value1&key2=value2');
  req.setHeader('Content-Type', 'application/x-www-form-urlencoded');  

  try {
    res = http.send(req);
  } catch(System.CalloutException e) {
    system.debug('Callout error: '+ e);
  }
  system.debug(res.getBody());
     return null;
 }
}

I am getting below error now

error message System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = localhost/salesforce

8
  • bro in your prev qus you wasn't clear. If you said I need to query based on strTag and strlocation then I can provide ans yesterday only . Ok no issue which object record you want to query ?
    – Ratan Paul
    Commented Dec 19, 2015 at 7:09
  • yes bro i need to query based on strTag and strlocation but the database is hosted on other host.Not in salesforce
    – moin khan
    Commented Dec 19, 2015 at 7:15
  • ohh. Then you need to use REST or SOAP API for callout that will fetch your data. check documentation developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… and pls add you want to get data from external system using callout in your above question. So other don't misunderstood
    – Ratan Paul
    Commented Dec 19, 2015 at 7:21
  • Ratan i updated my code.pls have look.That I have been trying before but result is this error message System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = localhost/salesforce
    – moin khan
    Commented Dec 19, 2015 at 7:31
  • You can't use local host.Endpoint has to be publicly hosted.Also you need to create a remote site setting record for endpoint . Commented Dec 19, 2015 at 7:39

2 Answers 2

5

To make callout to external system via apex ,one has to create remote site settings first to Authorize Endpoint Addresses

Trailhead Apex Integration Services Module explains how to create remote site settings

Trailhead Reference

Also the endpoint should be Https webservice and should be accessible outside local host .

Also since you need to package this ,you can include remote site settings in your managed package

3

Just to extend @Mohith's answer :

Your Salesforce app will be hosted in the cloud. As a consequence, it cannot see any database or system hosted on your localhost, or your internal network for that matter.

If you want to query an external database, you need to expose it via a publicly available Web Service. Only then will Salesforce be able to see it and talk to it.

Can you confirm that you have such a web service? Once that is in place, all the considerations that @Monith gave above become relevant.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .