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
XexiXexi 

Automatic login via a custom button to another Web

I need a custom button in Salesforce that It redirect in other web

 

I need a custom button that redirects to another web and will automatically log. The external web only reveives by POST.

 

*** I'm trying to do the following. This returns the HTML of the web. But do not know how to paint.

 

public pagereference contactSend2() {
            HttpRequest req= new HttpRequest();
            HttpResponse res = new HttpResponse();
            Http http = new Http();
            req.setEndpoint('http://extranet.groupalia.com/user/login'); 
            req.setMethod('POST');
            req.setBody('username=***&password=***'); 
            res = http.send(req);
            return null;
   }

 

*** Also I'm trying this:

 

public static HTTPResponse contactSend() {
      HttpRequest req = new HttpRequest();
      req.setMethod('POST');
      req.setEndpoint('http://extranet.groupalia.com/user/login'); 
      req.setBody('username=***&password=***');
      req.setTimeout(60000);
      
      Http http = new Http();
      HTTPResponse res = http.send(req);
      return res;
    }

 

Please anyone have any idea? I'm going crazy.

 

Sergio.

Best Answer chosen by Admin (Salesforce Developers) 
_Prasu__Prasu_

You can try something following from JavaScript:

 

<script>
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"http://extranet.groupalia.com/user/login");

var i = document.createElement("input"); //input element, text
i.setAttribute('type',"text");
i.setAttribute('name',"username");

var s = document.createElement("input"); //input element, Submit button
s.setAttribute('type',"text");
s.setAttribute('value',"password");

f.appendChild(i);
f.appendChild(s);

//and some more input elements here
//and dont forget to add a submit button

document.getElementsByTagName('body')[0].appendChild(f);

</script>

Another reference: http://en.allexperts.com/q/Javascript-1520/create-form-submit-fly.htm

All Answers

_Prasu__Prasu_
Are you trying to have that button on standard Page layout? Code you are using is mainly for calling the web-services. If you see the the content of the response it should be showing the html content of the login success page you might be having.

one option can be creating the html form in the JavaScript and setting its method as Post, action URL as your endpoint.
XexiXexi

Thanks Prasanna.

 

So I understand that the code is incorrect to use what I try to do. What do I need to use methods to redirect to an external website, and to authenticate? Is that possible?

 

Sergio.

_Prasu__Prasu_

You can try something following from JavaScript&colon;

 

<script>
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"http://extranet.groupalia.com/user/login");

var i = document.createElement("input"); //input element, text
i.setAttribute('type',"text");
i.setAttribute('name',"username");

var s = document.createElement("input"); //input element, Submit button
s.setAttribute('type',"text");
s.setAttribute('value',"password");

f.appendChild(i);
f.appendChild(s);

//and some more input elements here
//and dont forget to add a submit button

document.getElementsByTagName('body')[0].appendChild(f);

</script>

Another reference: http://en.allexperts.com/q/Javascript-1520/create-form-submit-fly.htm

This was selected as the best answer
XexiXexi

I love you, Prasana! Thanks a lot!

 

IN JavaScript Button;

 

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")}

 

var submitForm = document.createElement("FORM");
document.body.appendChild(submitForm);
submitForm.method = "POST";

 

var newElement = document.createElement("input");
newElement.setAttribute('type',"text");
newElement.setAttribute('name','username');
submitForm.appendChild(newElement);
newElement.value = '{!Co.email}';

 

var newElement = document.createElement("input");
newElement.setAttribute('type',"text");
newElement.setAttribute('name',password);
submitForm.appendChild(newElement);
newElement.value = '{!Co.Password__c}';

 

submitForm.action= "http://extranet.tictac.com/user/login";
submitForm.submit();