Here's a simple JS code that can be converted into a bookmarklet to quickly open an Assembla ticket.
Insights and lessons learned from designing and implementing cloud solutions on the Salesforce1 Platform. Side adventures into Heroku with Ruby on Rails apps also included.
Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts
Wednesday, May 4, 2016
Wednesday, April 30, 2014
Custom Button connection.js: INVALID_SESSION_ID
After deploying a custom button from a sandbox to Production, I noticed that the button stopped working. Puzzled, I opened the Web Console in Firefox to find the following error message: INVALID_SESSION_ID.
I was not doing anything special with sforce.connection.sessionId in my code. All the button does is {!REQUIRESCRIPT('/soap/ajax/30.0/connection.js')} and move on to query and update the database. The same code was working fine in my sandbox still, so I figured the next best thing would be to actually look at sessionId to see what's wrong.
At first glance, sessionId looked like a perfectly fine mess of characters that are reasonable for a Session ID value.
00DG0000000krVL!ARQAQP3vNkwdt8OU3WVIjlAwQvWtGcFcIjWq.0jb7A5wYNljj6Oog50.wDma4uUiB.PeDMqx1mLwkMyGj78gyMGLRLGTxic1
Then, I compared sessionId to the sid cookie, and I noticed that the two values were very different.
00DG0000000krVL!ARQAQLWR697R.eqoAy4178kYPxh2kJGv6BOpv_wsHc6KBjY6UPY01.Rfncn6WAMS2klSm.gLvDOWicNKujfE4smTPBLJvyS5
Refreshing the page in my browser did nothing for the problem. The same two values consistently appeared, and the INVALID_SESSION_ID error consistently broke my code.
Finally, the fix was implemented in the form of additional JavaScript in a static resource. This resource, added using REQUIRESCRIPT() after connection.js, simply maps the cookies in the current document and then stores the sid value in sforce.connection.sessionId. The addition of this step resolved the INVALID_SESSION_ID error as far as I could tell.
Has anyone else ever encountered this issue, in Spring '14 or earlier? I feel like I've stumbled upon a bug, because as far as I know connection.js used in a custom button should not need this kind of "initialization".
Labels:
API,
hack,
JavaScript,
Salesforce
Monday, February 17, 2014
AJAX Toolkit 29.0 Home Page Component
For orgs trying to push the limits of the standard Home tab in Salesforce, using JavaScript is essential. However, trying to pull in the AJAX Toolkit is not as easy as adding a merge field to a new home page component, because merge fields don't work here. Fortunately, making an assumption about where the toolkit resides allows us to create a home page component that's equivalent to {!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}.
This 1-minute video shows you how to create the component and add it to your home page layout, using the sample code from "Home Page Component: AJAX Toolkit 29.0". The only real "trick" that's needed to make the component work is to extract the session ID cookie and pass it to the sforce object.
Now all of your home page components can be Ajax-enabled.
This 1-minute video shows you how to create the component and add it to your home page layout, using the sample code from "Home Page Component: AJAX Toolkit 29.0". The only real "trick" that's needed to make the component work is to extract the session ID cookie and pass it to the sforce object.
Now all of your home page components can be Ajax-enabled.
Labels:
Ajax,
API,
hack,
JavaScript,
Salesforce,
UI
Friday, January 31, 2014
JS for Salesforce Home Page Component
While it's not possible to add a Visualforce page or component to the standard Salesforce home page, it is possible to inject straight HTML and JavaScript into the home page via an HTML Area component. Furthermore, you can use Salesforce's AJAX Toolkit to connect to the API, which opens up a world of dynamic possibilities for your Home tab.
There's only one trick to connecting to the API: You need the current user's session ID. Luckily, this information is stored in the sid cookie and can be retrieved and easily stored in the sforce.connection.sessionId property.
From here, you can use all of the tools in the AJAX Toolkit to bring your data to life on the Home tab.
There's only one trick to connecting to the API: You need the current user's session ID. Luckily, this information is stored in the sid cookie and can be retrieved and easily stored in the sforce.connection.sessionId property.
From here, you can use all of the tools in the AJAX Toolkit to bring your data to life on the Home tab.
Labels:
HTML,
JavaScript,
Salesforce,
UI
Wednesday, December 4, 2013
Mark Read Checkbox in Field Permissions for Salesforce Profile with Bookmarklet
Below is a functional bookmarklet that you can use to mark the Read checkbox for every single field in the Field Permissions section of an object's settings within a Salesforce Profile.
To use the bookmarklet:
- Drag the link ("bookmarklet") on to your browser's bookmarks bar
- Open an object settings page within a Salesforce Profile
- Click the bookmarklet in your browser's bookmarks bar
Note: This was tested with Chrome on Windows 8 in Salesforce Winter '14.
Labels:
hack,
JavaScript,
Salesforce
Monday, September 2, 2013
Custom CSS for Chatter Communities
Chatter Communities allows an organization using Salesforce to quickly deploy a functional, branded portal experience for partners and customers. Communities conveniently allows for a custom HTML header and footer to be added to a community, but it appears that in the Summer '13 release of Salesforce there is no support for using merge fields or to easily pull in custom CSS styles into a community. If you're used to using the URLFOR() function with the apex:stylesheet element in Visualforce, you may feel like something's missing.
Without too much hacking, there are two HTML5-compliant ways to approach this problem in Summer '13:
Without too much hacking, there are two HTML5-compliant ways to approach this problem in Summer '13:
- Include a style element with the scoped attribute
- Use JavaScript in a script to insert link elements to stylesheets stored in static resources
<style scoped>...</style>
This approach is simple, and any styles defined should be applied to the div id="contentWrapper" element into which the community's HTML header and footer are inserted. The parent div pretty much includes all of the visual content on the page, which makes this approach fairly comprehensive in the scope of its effects.
JavaScript script to insert link elements
Typically in Visualforce, styles are applied through dynamic links to CSS stylesheets stored as static resources. However, to build that dynamic URL in static HTML requires manual construction based on knowledge of Salesforce's URL conventions.
A relative URL to a CSS stylesheet stored within a .zip file loaded as a static resource has the following components:
- CommunityName: The keyword appended after ServerName, if applicable. This may be blank for a particular community.
- ResourceNumber: A unique number assigned to a static resource by Salesforce
- ResourceName: The name (case-sensitive) of the static resource
- InsidePath: The path inside the .zip file to the CSS stylesheet
The full URL is then constructed by concatenating the following: '/' + CommunityName + '/resource/' + ResourceNumber + '/' + ResourceName + '/' + InsidePath
A few sample URL's are shown below:
- /cirruscommunity/resource/1378136101000/CirrusStaticResource/styles/layout.css
- /slalomcommunity/resource/1379138201002/SlalomStaticResource/css/common.css
ResourceNumber can be ascertained by opening the static resource and looking for the string of numbers in the "View file" link's URL. Right-clicking the "View file" link and copying the link address should easily grab the ResourceNumber for you.
Lastly, the following JavaScript script can be edited to add all of the stylesheets you want to the community.
<script type="text/javascript">
function appendLinkToHead(path) {
var link = document.createElement("link");
link.setAttribute("rel", "stylesheet");
link.setAttribute("type", "text/css");
link.setAttribute("href", path);
var head = document.getElementsByTagName("head")[0];
head.appendChild(link);
} // function appendCommunityLink(String)
var communityName = "community";
var resourceNo = "1378136101000";
var resourceName = "CommunityResource";
var resourcePath = "/" + communityName + "/resource/"
+ resourceNo + "/" + resourceName + "/";
var stylesheets = [
"library/css/brand.css",
"library/css/layout.css",
"library/css/reset.css"];
for (var i = 0; i < stylesheets.length; i++)
appendLinkToHead(resourcePath + stylesheets[i]);
</script>
Labels:
Chatter,
Communities,
CSS,
hack,
HTML,
JavaScript,
Salesforce
Subscribe to:
Posts (Atom)