3

I am working on integrating third party application with Salesforce. As part of integration to the third party, we have an anchor tag( tag) on visualforce page that makes a POST call. The way we do that is:

<a href="javascript:function1(a,b);">Click here</a>

<script>
function function1(a,b){
        var form = document.createElement("form");
        var element1 = document.createElement("input"); 
        form.method = "POST";
        form.action = uri;
        form.target ="_top";

        element1.type="hidden";
        element1.name="Token";
        element1.value=token;
        form.appendChild(element1);  
        document.body.appendChild(form);
        form.submit();
}
</script>

When the request hits the URI mentioned above as form action, it would internally redirect to the other Uri (which is handled by third party app entirely) - say URI2. In the first request's response, you get a validationtoken cookie which is to be sent with the next request.

This flow works completely fine on Salesforce Web as well as emulating Salesforce1 mobile theme in the browser using /one/one.app. Whereas while testing Salesforce1 on real devices, I am facing an issue wrt Android mobiles. When I hit the anchor tag on iOS device, a child window opens up within Salesforce that performs the above operation and passes on the validationtoken cookie from one response to the next request. When I hit the anchor tag on Android device, the link opens up in a mobile browser, outside of Salesforce1 app. As part of hitting the first URI, it receives a cookie in its response but doesn't send the received cookie along with its next request (tracked using Fiddler).

Any help would be appreciated. Thanks

3

This is basically because how Salesforce1 is implemented on Android. The main UI is provided by the OS (a WebView component), so when you navigate to a normal URL in an app that is outside of Salesforce1's registered URL handlers, it will be routed to the default browser. You'll need to instead use an XMLHttpRequest to simulate the form submit, and depending on the third-party you're contacting, you might also need to use the AJAX Proxy.

  • Thanks for that. My concern isn't much about url opening in the browser in case of android. It's about cookies not getting passed on from one request's response to the next request. – Harsha V May 21 '17 at 6:44
  • When I try to submit the form using XMLHttpRequest, the browser is blocking it as it usually allows a request in the same origin for security reasons. As the URI I am trying to hit is on another domain, I get the following error: Redirect from URI1 to URI2 has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. – Harsha V May 21 '17 at 7:28
  • @HarshaV If you can configure the server you're calling, add the CORS header to allow access to the resource. If not, use the AJAX Proxy that I linked to at the end of this answer. – sfdcfox May 21 '17 at 11:57
  • I don't have handle over server that I am calling. Also, I cannot use the AJAX proxy as I cannot install the Salesforce certificate on the server. My concern is more about why the cookies are not getting passed on from one request's response to the next request. – Harsha V May 21 '17 at 17:46
  • Could you help me with an alternate way to approach this issue? – Harsha V May 29 '17 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.