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
SF PracticeSF Practice 

Need to send cookie to external URL

Hello, I have a button click opening external URL. On the button click from Lightning controller, I am using e.force:navigateToURL to redirect user to external URL. I need to send cookie information to this external URL. I am using XMLHttpRequest.setRequestHeader but it is not sending cookie info in header while redirecting. Please let me know if this is feasible and what is the way of doing it.

It is working fine if I am adding the information in QueryString but this is not a preferred way.

Thanks,
Gaurav
AnudeepAnudeep (Salesforce Developers) 

You can try passing document.cookie which contains all information of the cookie as a parameter in your page reference. Additional parameters can be passed using the state parameter
 
createCookie :function(name, value, days) {
var expires;

if(days) {
var date = new Date(); 
date.setTime(date.getTime() + (days*24*60*60*1000)); 
expires = "; expires=" + date.toGMTSTring();
}
else {
 expires = "";
}
document.cookie = name + '=' + escape(value) + expires + "; path=/";
}
 
var pageReference = {
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Account',
                actionName: 'list'
            },
            state: {
                filterName: "MyAccounts"
            }
        };

Please mark this answer as Best if you find this information helpful. It may help others in the community. Thank You!

Anudeep

 
SF PracticeSF Practice
Hi Anudeep, Thanks for replying. I want to send this cookie to another domain which is non salesforce. Since it will be a separate redirection on button click and treated as separate document in javascript world so will it be possible to send cookie. Please specify how to pass cookie to another domain thru lightning javascript controller.