Posts Tagged: Javascript

Auto-Refresh Salesforce Dashboard every 5, 10 or 15 … seconds, minutes or hours …

Is there a way we can have Salesforce Dashboard Auto-Refresh every few seconds (not recommended) or every x minutes or every x hours …?

Standard Out of box functionality does allow to refresh dashboard, but the lowest level of refresh interval is DAILY or WEEKLY or MONTHLY.

Lets say you want to have(display) a Sales Monitor always LIVE showing real time data, how do you achieve that?

There’s no direct way of doing it, but JS (as always) comes to the rescue …

  1. Log in to Computer (which is streaming output to LIVE monitor) , open Chrome browser (will try to update the script later to make it work in all browsers)
  2. Login to Salesforce and Navigate to desired Dashboard
  3. Delete url from  address/url bar, type (or copy+paste) below code and hit enter in address/url bar. (Make sure javascript: at start is there, chrome does remove it when copy pasted)

    javascript:function autorefresh() {document.getElementById('refreshButton').click();setTimeout(autorefresh, 5000);}autorefresh();

  4. Now do a full screen and enjoy Dashboard Live Monitor :)

 Please Note:

  1. 5000 represents 5000 milli seconds ie 5 seconds, so change the same according to your requirements ie if you want it to refresh every minute change the value to 60000
  2. This is not a recommended way, as its a tweak and might not work in future. (But as of now its working great and helps in having real time monitor of your favorite dashboard, so enjoy!)

Might be this can be an admin tool or a chrome extension, will try to make one when I get time and chance :)

Make Salesforce calendar year drop-down to show earlier years

Problem:
The birthdate field on the Contact object doesn’t show previous year and neither does it allow to switch back and forth the Years part easily.

Solution:
Below example will show the last 100 years.

  1. Go to Setup -> App Setup -> Customize -> User Interface. Here make sure the ‘Show Custom Sidebar Components on All Pages’ is checked.
  2. Go to Setup -> App Setup -> Home Page Layouts. Make sure all your Home Page Layouts have the Messages & Alerts component checked.
  3. Go to Setup -> App Setup -> Home Page Components. Here, click edit for Messages & Alerts. In the textarea, copy and paste the javascript code below and save (it can just go below your normal Messages & Alerts, won’t show up on the actual page).
<script src="/js/dojo/0.4.1/dojo.js"></script>
<script src="/soap/ajax/11.1/connection.js" type="text/javascript"></script>
<script type="text/javascript">
dojo.require("dojo.collections.Store");
dojo.require("dojo.charting.Chart");
dojo.require('dojo.json');
var arYears = getYears();
function swapYears(){
	if(document.getElementById('calYearPicker') != null) {
		var select = document.getElementById('calYearPicker');
		var curValue = select.value;
		var parentx = select.parentNode;
		parentx.removeChild(select);
		select = document.createElement('select');
		select.size = 1;
		select.id = 'calYearPicker';
		select.name = 'calYearPicker';
		parentx.appendChild(select);
	}
	if(select != null) {
	for(x=0;x<100;x++) {
		select.options[x] = new Option(arYears[x], arYears[x], false, false);
	}
	}
}
function getYears() {
	sforce.sessionId = getCookie('sid');
	sforce.connection.sessionId=sforce.sessionId;
	var out = [];
	var currentTime = new Date()
	var year = currentTime.getFullYear()
	try {
		for(x=0;x<100;x++) {
			out[x] = x+year-99;
		}	

	} catch(error) {
		alert(error);
	}
	return out;
}
dojo.addOnLoad(swapYears);
</script>

Credits: Salesforce Community (http://boards.developerforce.com/t5/General-Development/Date-of-Birth-field-Calendar-years-don-t-go-back-before-this/td-p/120133)

Salesforce – Hide standard buttons display (Working version)

There are many a times where we want to remove few standard buttons from page layout, but there is no such option to remove such restricted buttons. Few such buttons are …

  • “New Note” or “Attach File” button on Google Docs, Notes, & Attachments Related list
  • “Save & Send Invitation” button on New Event page
  • Following is a solution which will hide “Save & Send Invitation” button on New Event page

    STEP1: JAVASCRIPT Code. Create a JS file with below code

    function hideBtns()
    {
    if(document.getElementsByName(“sendEmail”)[0]!=null)
    document.getElementsByName(“sendEmail”)[0].style.display = ‘none’;
    if(document.getElementsByName(“sendEmail”)[1]!=null)
    document.getElementsByName(“sendEmail”)[1].style.display = ‘none’;
    }
    if (window.addEventListener) {
    window.addEventListener(“load”, hideBtns, false);
    }
    else if (window.attachEvent) {
    window.attachEvent(“onload”, hideBtns);
    }

    STEP2: Upload the JS file as a DOCUMENT

    STEP3: Create a Home Page Component of type (HTML Area)

    <script src=”/servlet/servlet.FileDownload?file=01530000001OcDl”></script>

    Here 01530000001OcDl is ID of document created in STEP2

    STEP4: Add above created Home page component in your home page layout.

    That’s it you are all set, now open New Event and add invitees, automatically the “Save & Send Invitation” button will disappear.

    Please note : If you have some functionality which you want to work across all pages or on every form then navigate to Setup then go to Customize->User Interface, check “Show Custom Sidebar Components on All Pages” under the Sidebar section.

    YUI Library – The transparent mask turns black in IE 8

    Problem Statement
    I use the YUI dialog in my project and turn on the “modal”, the initializing code looks like:

    document.mainDialog = new YAHOO.widget.Dialog("mainPanel",
    { modal: false,
    width: "89em",
    height: "30em",
    fixedcenter: true,
    visible: false,
    constraintoviewport: true
    });

    The modal works fine in IE 7.0 and Fireforx 3.0, it generates a transparent mask to prevent activating any elements in the document before closing the modal. But in IE 8.0, the transparent mask turns to “black”, rather than “transparent”.

    Solution
    The cause is that IE8 does not support the CSS3 ‘opacity’ selector. But YUI uses this to style the mask as follows:

    .yui-skin-sam .mask {
    background-color:#000000;
    opacity:0.25;
    }

    To solve this problem – I have an IE specific stylesheet in which I override the above style as follows:

    .yui-skin-sam .mask{
    filter: alpha(opacity=30);
    }

    Read more about the bug @ YUI Library

    Submit a Form in Internet Explorer with Enter

    Ran into an (other) interesting Internet Explorer bug. Seems that if you have a form with only a single text input, hitting the enter button will not submit the form in IE.

    <form action="" method="post">
    <fieldset>
    <label for="user_name">User Name</label>
    <input type="text" name="user_name" id="user_name" />
    </fieldset>
    <fieldset class="button">
    <button type="submit" name="submit" id="submit" title="Verify User Name">Verify User Name</button>
    </fieldset>
    </form>

    The solution is to hide an additional disabled input for IE to find, using IE conditional Comments and hiding it from view with some CSS.

    <form action="" method="post">
    <fieldset>
    <!--[if IE]><input type="text" style="display: none;" disabled="disabled" size="1" /><![endif]-->
    <label for="user_name">User Name</label>
    <input type="text" name="user_name" id="user_name" />
    </fieldset>
    <fieldset class="button">
    <button type="submit" name="submit" id="submit" title="Verify User Name">Verify User Name</button>
    </fieldset>
    </form>

    Executing JavaScript on page load

    Following code helps you to define any JS function to be called on page load. The tricky part of the code is that it waits until page is completely loaded.


    <script>
    function init() {
    alert('Loaded');
    }

    var previousOnload = window.onload;
    window.onload = function() {
    if (previousOnload) {
    alert('...loading ...');
    previousOnload();
    }

    init();
    }
    </script>

    Javascript HtmlEncode/HtmlDecode

    Ack:- Lunar Media

    Have you ever needed to HtmlEncode a querystring using JavaScript? Yes you may say, and several people will claim that you should UrlEncode it using escape(str); For some unknown reason I tried doing exactly that and UrlDecode the variable in csharp with no luck. Instead I found a solution in the Prototype library, appropriately they call it escapeHTML:

    function escapeHTML (str)
    {
    var div = document.createElement(‘div’);
    var text = document.createTextNode(str);
    div.appendChild(text);
    return div.innerHTML;
    };

    Wonderful simple and effective trick using the browsers own automatic Html converter.

    Direct Population of Form Values using Javascript

    Type the following code in Address bar to fill the form fields named Ad1 and Ad2 wth certain text.

    javascript:function A(){document.getElementById(‘Ad1′).value =”sadAS1″;document.getElementById(‘Ad2′).value = “sadAS2″;}setTimeout(‘A()’); void(0)