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
BenPBenP 

Set cookie on site to get debug logs

I have a force.com site that sometimes has an unhandled execption, it happens.  Normally I'd just turn on logging for that guest user and see what the people were doing wrong.  After winter '17, users will have to have a special cookie for logs to be created.

My thought was to set this special cookie whenever the user accesses the site.  That way I can just turn on logging when needed.

I see the setCookies method, but that adds the apex__ prefix to the name.  Is there another way?  I'm just an admin code hacker, so forgive me.
Jayamaruthyraman JaganathJayamaruthyraman Jaganath
Hi BenP,

I found the following and thought it might be helpful!

https://help.salesforce.com/apex/HTViewSolution?id=000247187&language=en_US
BenPBenP
Right, that's how to set a cookie manually.  I'd like to have this happen when they load the page, or click a button on the page.
JSingh9JSingh9

To Set it Manually

First you need to navigate to the Site Url In ur Browser and then Click on Developer tools and then Execute below script through Developer console.

document.cookie ="debug_logs=debug_logs;domain=.force.com";


To Set it through Code

Create an instance of Cookie Class https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_sites_cookie.htm

and then use setCookie Method https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_System_PageReference_setCookies.htm

BenPBenP
The setCookies method adds the prefix “apex__” to the cookie names.  Wouldn't that cause an issue?
JSingh9JSingh9
Not Sure, but we can try, as it seems like the only option available for now within Apex
BenPBenP
I managed to get my class to set a cookie, but it doesn't cause a log entry.  The reason is that the name and domain do not match what salesforce wants.  They want the name to be debug_logs and the domain to be force.com.  The set cookie method prefixes "apex__" to the name and I don't see where you can set the domain at all.

I welcome feedback.
BenPBenP
I found that adding this line of code in the java section of my vf page works. document.cookie = "debug_logs=debug_logs; path=/; domain=.force.com; value=debug_logs" ;