Thursday, August 14, 2014

How to get date based on fiscal year,quarter or month

Date startDate = [Select StartDate From Period Where type = 'Quarter' and StartDate = THIS_FISCAL_QUARTER].StartDate;

Date startDate = [Select StartDate From Period Where type = 'Month' and StartDate = THIS_FISCAL_Month].StartDate;

Date startDate = [Select StartDate From Period Where type = 'Year' and StartDate = THIS_FISCAL_Year].StartDate;

The above will get you the current start date for the custom fiscal year.
Note: Custom fiscal year has to be setup if you want any results.

Thursday, August 7, 2014

How to play video in visualforce from attachment

So we have developed many data driven pages but when it comes to multimedia then many of us didn't worked on it much. We are thing "Ok its just a video or a image I will display it by getting its path" but the problem arise when its(the MIME file) is an attachment. Attachments are stored in database unlikely if compared from static resource which has a direct access URL. So how can I display a multimedia file if its type is BLOB.

The answer is here, we can use <object> tag or we can have <video> tag of HTML5.
for <object> tag, here the {!crwAtt.id} is Attachment object.
hor <video> tag, remember to add "docType="html-5.0"" to page.

Tuesday, August 5, 2014

How to use escape sequences with SOQL

So you are trying to pass some quoted string in a SOQL query and hits a error as some thing like this "line break not allowed". Now to over come this you can use the escape sequence provided by salesforce. What it really does is that when ever a string ends by single quote then salesforce mark it as a non break sequence, but when you introduce another quote then it breaks the single string into two if "+" is not used. Ok fare enough but if we need more then one quote then it will be very diffycult to add all those "+" signs. So to do this we can use the below methods:

\n or \N New line
\r or \R Carriage return
\t or \T Tab
\b or \B Bell
\f or \F Form feed
\" One double-quote character
\' One single-quote character
\\ Backslash
LIKE expression only: \_ Matches a single underscore character ( _ )
LIKE expression only:\% Matches a single percent sign character ( % )

Example: database.query('Select Id from Account where Id IN ('0019000000txvCQ','0019000000txvCQ')'); wont work

database.query('Select Id from Account where Id IN (\'0019000000txvCQ\',\'0019000000txvCQ\')'); will work

Thursday, July 24, 2014

How to use sObject in Apex salesforce

Hi so today I was searching around the net to get some example to use sObjects of salesforce but I was not able to find some, so I decided to make one here with very easy approach.


  • Declaring a sObject - sOject objAcc = new sObject (); // without type
                                     sOject objAcc = new Account (); // with type
  • Getting field values - sOject objAcc = new Account ();
                                    Id oId = objAcc.Id;
                                    String nameAcc = objAcc.get('Name'); // "Name" is field's API name
  • Setting field values - sOject objAcc = new Account ();
                                    objAcc.put('Name','TestName');
Hope this helps someone.

How to solve cross domain parent URL access

So you are here because you were trying to access parent page's url with the child iframe and ran into this error:

"SecurityError: Blocked a frame with origin https://c.ap1.visual.force.com from accessing a frame with origin https://ap1.salesforce.com. Protocols, domains, and ports must match."

In simple words what this means that the site is not allowing you to gain access to the parent frame because it can be used in many hack attacks and is trying to safe guard the application. But if you still need this there is an alternative approach insted of doing this
window.parent.location.href()
You can do this:
var hurl = document.referrer;
So now you can access the cross domain parent url.
Hope this helps some one.