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
sumpritsumprit 

How can I send data from Salesforce to an external system?

Hi there,

How can I send the data from Sales force to an external system?

I believe it can be done through a S-Control which can be attached to the custom button (URL type) that will trigger to send the data using HTTP POST method.

Has anyone ever done anything like this and could you share the details with me.?

Thanks

sfdcfoxsfdcfox
You can do this using Salesforce SOA, in an S-Control, button, or link. If you have Unlimited Edition, you can also create calls using Http, HttpObject, and HttpResponse from the Apex Code object library. If you have the AJAX version, simply call the function in a place convenient for you. In Apex Code, you can't (unfortunately) use triggers, so you have to use a webservices method or use Apex AJAX to invoke the code via the Salesforce API through the browser. For example:

Code:
{!RequireScript("/soap/ajax/11.1/connection.js")}
postParams = {}
postParams.url = 'http://www.mydomain.com/services/function'
postParams.method = 'POST'
postParams.requestData = 'data to send' // place your data here
postParams.requestHeaders = {}
postParams.requestHeaders['Content-Type'] = 'text/plain' //or another mime-type
postParams.requestHeaders['Content-Length'] = postParams.requestData.length //required for POST
postParams.onSuccess = processSuccess
postParams.onError = processError
sforce.connection.remoteFunction(postParams)

function processSuccess(message)
{ // do something with results
}

function processError(message)
{ // do something with the error
}

 You can use that in a button or link on the object, or you can replace the first line with a normal script include for an S-Control. I do hope you find this useful.

sumpritsumprit
Thank you.

I will try this right now and post my replies again here.


Thanks a lot
sfdcfoxsfdcfox
I forgot to mention that the server that you specify has to be configured in Setup before you can use it. You'll find it under Setup | Administration Setup | Security Controls | Remote Site Settings.
sumpritsumprit
Thank you again.

For testing purposes, I have set up IIS - locahost server. Therefore I would like to see the data displayed on the web page on my localhost.

So, I am assuming that I would have to set up the IIS - localhost webserver through salesforce first. Correct?

Also, in the code you mentioned earlier, where will I define the web page address:- Like this:-

postParams.url = 'http://localhost/SFO/DisplayFields.html'


thanks
sfdcfoxsfdcfox
You're connecting through a proxy, so if you specify localhost, you're actually specifying the localhost of the proxy-- the salesforce.com server that's handling your request. Instead, you have to specify the connection as Salesforce sees it, which is your IP address or domain name. You'll probably need to also configure your firewall to allow connections to port 80 or 443 (or another named port) in order to allow the servers to access your server. A long round-trip just to access your own computer, I realize, but that's the way browser security is designed.
sumpritsumprit
Here is the S-Control ;-
=======================================================================
<html>
<head>
<script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>
<script>
function setupPage()
{
    //function contains all code to execute after page is rendered
  
   var state =
   {
        //state that you need when the callback is called

       output : document.getElementById("output"),
       startTime : new Date().getTime()
   };

       var callback =
       {
                   //call layoutResult if the request is successful
                  
                   onSuccess: layoutResults,

                  //call queryFailed if the api request fails

                   onFailure: queryFailed,
                   source: state
         };

                                        alert("start");

sforce.connection.query
(

"Select o.AccountId, o.Account.BillingCity, o.Account.BillingCountry, o.Account.BillingPostalCode, o.Account.BillingState, o.Account.BillingStreet, o.Account.Fax, o.Account.Id, o.Account.Name, o.Account.Phone, o.Account.Website, o.Id, o.Name, o.OwnerId, o.Owner.Name from Opportunity o where o.Id = '{!Opportunity.Id}' ",callback);

alert("end query");
}

function queryFailed(error, source)

{

 alert("QueryFailed...");

 source.output.innerHTML = "An error has occurred - query: " + error;

}

function layoutResults(queryResult, source)

{

 var output = ""; // this variable will store the data and display the results using HTTP POST method.

if (queryResult.size > 0)

 {

  //get the records array

  var records = queryResult.getArray('records');

 
  //loop through the records and construct html string

  for (var i = 0; i < records.length; i++)

  {

   var Opportunity= records[i];

// Using HTTP POST method to send data.

output.url = "http://localhost:8080/suma/",
output.method = 'POST',

output.requestData += "Opportunity Account ID=" + Opportunity.AccountId + "," +

"Opportunity Name=" + Opportunity.Name + "," +

"Opportunity ID=" + Opportunity.Id + "," +

"Account Billing City=" + Opportunity.Account.BillingCity + "," +

"Account Billing Country=" + Opportunity.Account.BillingCountry+ "," +

"Account Billing Postal Code=" + Opportunity.Account.BillingPostalCode+ "," +

"Account Billing State=" + Opportunity.Account.BillingState + "," +

"Account Billing Street=" + Opportunity.Account.BillingStreet + "," +

"Account Fax=" + Opportunity.Account.Fax + "," +

"Account ID=" + Opportunity.Account.Id + "," +

"Account Name=" + Opportunity.Account.Name + "," +

"Account Phone=" + Opportunity.Account.Phone + "," +

"Account Website=" + Opportunity.Account.Website + "," +

"Opportunity Owner ID=" + Opportunity.OwnerId + "," +

"Opportunity Owner Name=" + Opportunity.Owner.Name;

output.requestHeaders = {};
output.requestHeaders ['Content-Type'] = 'text/plain',
output.requestHeaders['Content-length'] = "postParams.output.length",
output.onSuccess = function (response)
                                     {
                                            sforce.debug.log(response);
                                     },
output.onError = function (response)
                              {
                                   alert("The code failed" + response)
                              },

sforce.connection.remotefunction(output);

  } // this is the closing for FOR loop

 
 } // this is the closing bracket for IF loop for records array.


  //render the generated html string
 // Comment this out if you are sending data from Salesforce to localhost.

  //source.output.innerHTML = output;


} // this is closing bracket for FUNCTION where you have defined OUTPUT as the variable.




                                     



     

</script>

</head>

 

<body onload="setupPage()">

<div id="output"> </div>

</body>

</html>
=======================================================================
sumpritsumprit
the error message is -

========================================================================

An error has occurred - query: TypeError: output.requestHeaders has no properties

========================================================================


sumpritsumprit
The solution to this problem is following:-
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

<html>
<head>
<script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>
<script>
function setupPage()
{
    //function contains all code to execute after page is rendered
  
   var state =
   {
        //state that you need when the callback is called

       output : document.getElementById("output"),
       startTime : new Date().getTime()
   };

       var callback =
       {
                   //call layoutResult if the request is successful
                  
                   onSuccess: layoutResults,

                  //call queryFailed if the api request fails

                   onFailure: queryFailed,
                   source: state
         };

                                        alert("start");

sforce.connection.query
(

"Select o.AccountId, o.Account.BillingCity, o.Account.BillingCountry, o.Account.BillingPostalCode, o.Account.BillingState, o.Account.BillingStreet, o.Account.Fax, o.Account.Id, o.Account.Name, o.Account.Phone, o.Account.Website, o.Id, o.Name, o.OwnerId, o.Owner.Name from Opportunity o where o.Id = '{!Opportunity.Id}' ",callback);

alert("end query");
}

function queryFailed(error, source)

{

 alert("QueryFailed...");

 source.output.innerHTML = "An error has occurred - query: " + error;

}

function layoutResults(queryResult, source)

{

 var output = ""; // this variable will store the data and display the results using HTTP POST method.

if (queryResult.size > 0)

 {

  //get the records array

  var records = queryResult.getArray('records');

 
  //loop through the records and construct html string

  for (var i = 0; i < records.length; i++)

  {

 var Opportunity= records[i];

output.requestData += "Opportunity Account ID=" + Opportunity.AccountId + "," +

"Opportunity Name=" + Opportunity.Name + "," +

"Opportunity ID=" + Opportunity.Id + "," +

"Account Billing City=" + Opportunity.Account.BillingCity + "," +

"Account Billing Country=" + Opportunity.Account.BillingCountry+ "," +

"Account Billing Postal Code=" + Opportunity.Account.BillingPostalCode+ "," +

"Account Billing State=" + Opportunity.Account.BillingState + "," +

"Account Billing Street=" + Opportunity.Account.BillingStreet + "," +

"Account Fax=" + Opportunity.Account.Fax + "," +

"Account ID=" + Opportunity.Account.Id + "," +

"Account Name=" + Opportunity.Account.Name + "," +

"Account Phone=" + Opportunity.Account.Phone + "," +

"Account Website=" + Opportunity.Account.Website + "," +

"Opportunity Owner ID=" + Opportunity.OwnerId + "," +

"Opportunity Owner Name=" + Opportunity.Owner.Name;

// Using HTTP POST method to send data starts here

sforce.connection.remoteFunction({
        url : "http://www.cheenath.com",
        onSuccess : function(response) {
            alert("Got response" + response);
        },
        onFailure : function(error) {
            alert("ERROR: " + error);
        },
        async: false
    });


  } // this is the closing for FOR loop

 
 } // this is the closing bracket for IF loop for records array.


  //render the generated html string
 // Comment this out if you are sending data from Salesforce to localhost.

  //source.output.innerHTML = output;


} // this is closing bracket for FUNCTION where you have defined OUTPUT as the variable.

</script>
</head>
<body onload="setupPage()">

<div id="output"> </div>

</body>

</html>
///////////////////////////////////////////////////////////////////////////////////


Note, the Salesforce will not be able to send data to IIS - Virtual Server, however to test the code I was able to send the data to an outside server. You can also try www.google.com or any other site that you can ping to.

Hope this helps and thanks for all your suggestions.

regards