Sunday, October 16, 2011

How to query using date time field in Salesforce

There are many instances where we need to query on base of createddate or any other datetime field to find some specific records for particular day or for that month or for that year etc.

We cannot directly query with datetime field in where condition.

So you will need to use Date functions in SOQL to query using datetime field in where condition

 Opportunity =[SELECT Id, Name FROM Opportunity WHERE DAY_ONLY(CreatedDate)= '2011-10-16'];  

Refer other date functions which are available so that you can query easily using datefield in where condition.
Date functions

Tuesday, September 13, 2011

Rerender using Action Support in Visualforce pages

There are many instances when it looks simple that a component needs to be displayed only when there is a specific value in a picklist.

We tend to use actionsupport and rerender for such cases where we use actionsupport for some event like onchange or onclick etc and rerender to a specific component.

But many times we find that the rerender is not working properly and we need to struggle around for getting it work.

So it is necessary to understand this when you are using rerender for a component that has rendered attribute

If the component has a rendered attribute then wrap it with outputPanel or pageBlockSection or any other component and rerender to this wrapped component rather than the component that is rendered.

A simple example :

You want to render a field reason lost only when the Opportunity stage is Closed Lost

 <apex:page standardController="Opportunity" sidebar="false">  
   <apex:sectionHeader title="Edit Opportunity" subtitle="{!}"/>  
   <apex:form >  
     <apex:pageBlock title="Edit Opportunity" mode="edit">  
       <apex:pageMessages />  
       <apex:pageBlockButtons >  
         <apex:commandButton value="Save" action="{!save}"/>  
         <apex:commandButton value="Cancel" action="{!cancel}"/>          
       <apex:actionRegion >  
         <apex:pageBlockSection title="Basic Information" columns="1">  
           <apex:inputField value="{!}"/>  
           <apex:pageBlockSectionItem >  
             <apex:outputLabel value="Stage"/>  
             <apex:outputPanel >  
               <apex:inputField value="{!opportunity.stageName}">  
                 <apex:actionSupport event="onchange" rerender="thePanel"  
               <apex:actionStatus startText="applying value..." id="status"/>  
           <apex:inputField value="{!opportunity.amount}"/>  
           <apex:inputField value="{!opportunity.closedate}"/>  
       <apex:outputPanel id=”thePanel”>        
          <apex:pageBlockSection title="Closed Lost Information" columns="1"  
                   rendered="{!opportunity.stageName == 'Closed Lost'}">  
         <apex:inputField value="{!opportunity.Reason_Lost__c}"/>  

refer this link from community actionSupport rerender problem for details.

Hope this will help you if you are struggling with action support rerender issues.

Also many times due to Output Panel, fields will be distorted:
Try using outputPanel inside pageBlockSection.
 <apex:pageBlockSectionItem >  
 <apex:outputpanel id="panel 1">  
 <apex:outputLabel value="LabelName" rendered="{!oppty.field== 'Other'}"/>  
  <apex:outputpanel id="panel 2">  
 <apex:inputfield value="{!field}" rendered="{!oppty.field == 'Other'}" />  

Friday, June 3, 2011

Excellence is an inner force

A man once visited a temple under construction where he saw a sculptor making an idol of God. Suddenly he noticed a similar idol lying nearby. Surprised, he asked the sculptor, "Do you need two statues of the same idol?" "No," said the sculptor without looking up, "We need only one, but the first one got damaged at the last stage." The gentleman examined the idol and found no apparent damage. "Where is the damage?" he asked. "There is a scratch on the nose of the idol." said the sculptor, still busy with his work. "Where are you going to install the idol?"

The sculptor replied that it would be installed on a pillar twenty feet high. "If the idol is that far, who is going to know that there is a scratch on the nose?" the gentleman asked. The sculptor stopped his work, looked up at the gentleman, smiled and said, "I will know it."
The desire to excel is exclusive of the fact whether someone else appreciates it or not.

"Excellence" is a drive from inside, not outside. Excellence is not for someone else to notice but for your own satisfaction and efficiency.!!

Monday, May 30, 2011

Uploading documents in Salesforce

Uploading documents in Salesforce

Following are the ways to upload the documents in Salesforce:

1) Documents tab : Uses document object
This is generally used to store Web resources, such as, logos, files, some kind of templates and other Visualforce materials in folders. Also note that you cannot associate the documents with any record.

Maximum file size:
5 MB
20 KB for a custom-app logo

2) Notes and Attachments : Uses Attachment object
Attach files to records from the Attachments related list on selected detail pages. You can add a file to a specific record, like an event, contact, or case or any custom object by attaching it on the Attachments related list. You can also insert the file into Attachment object using API. On the visualforce page you need to use tag and using Attachment Object in class and you can associate it with record by using Parent Id field of Attachment object. The attachment inserted will be visible in Notes and Attachment related list of that record.

Maximum file size
5 MB for file attachments

3) Google Docs
Salesforce and Google Apps allow you to integrate Google Docs with Salesforce.
Firstly create a Google Apps account for your organization. It can be done by going to setup| Google Apps| Settings.
After following those steps activate add Google docs to Salesforce. It just requires simple activation from Salesforce Admin.

You can associate Google docs with Salesforce records even when you are not working in Salesforce by using the Add Google Doc to Salesforce browser button just by click of a button.
Add Google docs to Salesforce CRM Content, allows you to manage all types of content in a centralized location.

The docs can be associated with the record. This can be done by going to the record, adding the doc by using Google docs and attachment related list.

Google docs can be also published to Salesforce CRM Content library. When you save or publish you get the dialog box of Salesforce CRM content. Select the library and you can share the doc with members of that library or you can keep it private and store in your personal library.

Maximum file size:
To use a Google Apps service in Salesforce, you must be logged in to your Google Apps business account. Business accounts use your organization's domain. So a User (Google Apps Administrative Contact) will be present who will act as admin and have to register username and password. This becomes tedious to have user name password registered for all users and maintain them.

4) Salesforce Workspaces: Uses Content Version and Content document object.
Create, clone, or modify docs and save it so only you can see it and work on it. When you're ready with final copy, publish it so other users in company have access to it. You can create content delivery and send the link to customers so they can access the document.

Documents are stored in content document object and associated with Workspaces using Content Workspace. Content Workspace Doc is the junction object which stores Content Document ID and Content Workspace ID.
You can go to workspace and publish document there. You can use API as well for same. Use Create Version Object and create a document and insert it. The document will be created as Content Document. It can be associated with records and will be visible in Content documents related list. You can delete the document by deleting content document created using content document object.

Maximum file size:

5) MyFiles in chatter
Allows you to easily upload, store, find with Salesforce Global Search, and follow files you own or have access to from Salesforce Content or Chatter. The Files tab supports all file types up to 100MB each. You can share files here with Chatter or by creating groups. You also have a private section so no other users in the Salesforce CRM system can see them. The Files tab requires no admin setup and is ready to use when Chatter is enabled. Since this integrates with Chatter and Content, think of this as a central repository where all files from those two can be accessed if you have the proper permissions.
You can store the document in myFiles using API as well. You need to store the content as content document in personal workspace and provide ‘origin’ field in content version object as ‘H’. (origin is a picklist field in content version object and has two values ‘C’ and ‘H’. C denotes the file is in users personal workspace and ‘H’ denotes Chatter files from User’s myFiles.). This will insert the document in myFiles.
You can also share the file later on with any record by posting the file into content of the record by using contentPost.

Maximum file size:

Note: Limitation for storing the documents becomes 10MB for Salesforce Workspace and myFiles if you use visualforce page for uploading the document. This is due to the limitation on the visualforce page side.

Monday, May 16, 2011

Using Map in Pageblock table in Visualforce pages

I was not aware that we can use Map in pageblock table in Visualforce pages.
Whenever I had came across something which had duplicate results then I used to prepare Map and then extract List as values from Map and return that List to Page Block Table.

Now after I came across this post then i came to know that Map can be used in Visualforce pages.
Just prepare the Map and return the Map. Use the Map in following ways in Page Block Table in VF page.

 <apex:pageBlockTable value="{!data}" var="d">  
<apex:column headerValue="Key Values">
<apex:column headerValue="Values">

refer original post at following link

Monday, April 4, 2011

Calculate number of week from a date in Apex

I had to calculate number of week of the year based on a date field so i came across this and thought of sharing it

I saw this Salesforce community forum and got idea from there. The solution is for formula field.
I have used that in Apex using math methods and date methods

Formula: MOD(FLOOR( (Date__c -DATEVALUE("2006-01-01" ))/7),52)+1

I had to calculate number of week from the start date of year.
For the substraction part we cannot directly substract two dates so need to prepare instance of two dates and use daysbetween to find the difference.

Date todaydate =;
Date todaydateinstance = date.newinstance(todaydate.year(), todaydate.month(),;

Integer currentyear = todaydate.year();

Date startDate = date.newinstance(currentyear, 01, 01);

integer numberDaysDue = startDate.daysBetween(todaydateinstance);

so by using this formula we can calulate in Apex

Integer numberOfWeek = math.MOD(math.FLOOR( ( numberDaysDue )/7),52)+1;

Saturday, March 26, 2011

How to show help text on Visualforce Page?

Just came across this so thought of sharing it

When we use or tag in visualforce page then the help text comes automatically but when you are using or then use the following to get the help text :

<apex:pageBlockSectionItem helpText="{!$ObjectType.objectName__c.Fields.fieldName__C.inlineHelpText}" >
<apex:outputLabel> FieldName </apex:outputLabel>
<apex:inputText> FieldName__c </apex:inputText>

Wednesday, February 2, 2011


1) Heavy rains remind us of challenges in life. Never ask for a lighter rain. Just pray for a better umbrella. That is attitude.

2) When flood comes, fish eat ants & when flood recedes, ants eat fish. Only time matters. Just hold on, God gives opportunity to everyone!

3) Life is not about finding the right person, but creating the right relationship, it's not how we care in the beginning, but how much we care till ending.

4) Some people always throw stones in your path. It depends on you what you make with them, Wall or Bridge?
Remember you are the architect of your life.

5) Every problem has (n+1) solutions, where n is the number of solutions that you have tried and 1 is that you have not tried. That’s life.

6) It’s not important to hold all the good cards in life. But it’s important how well you play with the cards which you hold.

7) Often when we lose all hope & think this is the end, God smiles from above and says, `relax dear its just a bend. Not the end. Have Faith and have a successful life.

8) When you feel sad, to cheer up just go to the mirror and say, `Damn I am really so cute` and you will overcome your sadness. But don’t make this a habit coz liars go to hell.

9) One of the basic differences between God and human is, God gives, gives and forgives. But human gets, gets, gets and forgets. Be thankful in life!

10) Only two types of persons are happy in this world. 1st is Mad and 2nd is Child. Be Mad to achieve what you desire and be a Child to enjoy what you have achieved! cheers.............!!!

Sunday, January 23, 2011

Why it is necessary to know your weakness and guard it

No matter what our many strengths, just one weakness can prove more powerful than all of them!

A person’s single weakness is stronger than all his strengths! so true! Think about it. Just as a chain is only as strong as its weakest link, our strength is defined by our vulnerabilities, not our strong points. Not all the preparation in the world or intelligence can help you with any exam if you have an indecipherable handwriting that the examiner cannot figure out! Here your handwriting rather than your intelligence and hard work decide your future.

Similarly, you may have worked out a superb project with deep research and smart inputs, but if your timelines are not worked out and you cross the deadline, it is those couple of hours of delay that become the decisive factor! Similarly, you may be a great worker and a superb professional, but your loose tongue at an inappropriate moment may put paid to the rest of your capabilities! You will always find plenty of people around you not just willing, but eager to exploit your negative potential as displayed through that one weak link, that one vulnerability!

There is a category of people who get info after putting you at ease and assuring you they are totally with you. Most of us have in office a pseudo sympathizer who always emerges soon as he smells your moment of insecurity or misery. He always knows, for he is on the lookout. And sure enough, there he appears with oodles of sympathy and a smile, pumping you subtly for information. He gives subtle hints and encourages you to speak up. In a moment of weakness, you are most prone to let go and spill it all out. Satisfied, he runs back where he came from and uses the information to his own advantage before you realise! It is only later you realise he got you to bare all but never ever shared any of his or her own troubles! Your one weak moment could have done you a lifetime of harm!

Consider the power of weakness! Even the power of a relationship it is rightly said, lies with the one who cares less…never with the one who cares more and so has more at stake! So the weaker your love, the more powerful you are to mould or manoeuvre the relationship and your partner as and how you wish to! And yet, what a miserable power when the partner’s weakest link, his/her strong love for you, would be the biggest weapon you would use against them!

It is indeed our moments of weakness and weak points that we need to guard. It’s when you are most comfortable that you should be most guarded too. We are born with strengths that we can at best hone and understand, but an equally important task is to understand our weaknesses and work hard upon those. For these, our greatest weaknesses, if guarded and worked upon, have the potential of becoming our greatest strengths! Remember, the great Achilles was only as strong as his weak heel! Had that not been known, who could have killed him?

Thursday, January 13, 2011

Salesforce Spring 11 Release

Check out the features of Spring 11 here

one of the most exciting feature is Revised Apex Governor limit. I am looking forward to it.
Some of the features going to be are:

Automated Process for chatter posts using Chatter triggers

Private chatter File sharing

Customizable error messages

Closer look at code's performance - System Log console execution summary

Revised Apex governor Limits

Visual workflows

Criteria based sharing - Data dependent sharing using clicks

Visual force page inline editing and Dynamic binding

I am very excited about Revised Apex Governor Limits:
2 important increase in limits are:
150 DML updates for a process
50000 records can be retrieved instead of 10000 records using SOQL query

Also Criteria based sharing is a feature I am looking forward to.

To refer to the release notes click on Release Notes Spring 11

Thursday, January 6, 2011

Remove the date link (beside input field)

When you use input field for date field then along with the input date picker box it also provides a link of today's date on the right side of input field.
This occupies the space and at times does not look good on your beautifully designed visualforce page.

We can remove that date link. I saw a post on cookbook about that and thought of sharing it.
They have used a component which removes the link using css.
Here's the link for that and its clearly explained in it:
Hide the Current Date Link on an Inputfield

Saturday, January 1, 2011

Scheduling Apex Class using System.schedule method

To schedule an Apex class, implements the interface Schedulable in your Apex class

 global class newScheduledClass implements Schedulable  


now use the following system.schedule method for above class
open system log (click Your Name | Setup) and execute the below mentioned method so it will get scheduled.

 newScheduledClass m = new newScheduledClass();  
String s = '1 30 1 10 2 ?';
system.schedule('Job Name', s, m);

here string s is in following pattern:
seconds, minutes, hours, dayofMonth, month, dayofWeek, year(optional)

Values for expression:
Name Values Special Characters
Seconds 0–59 None
Minutes 0–59 None
Hours 0–23 , - * /
Day_of_month 1–31 , - * ? / L W
Month 1–12 , - * /
Day_of_week 1–7 , - * ? / L #
optional_year null or 1970–2099 , - * /

The special characters are defined as follows:
Special Character Description
, Delimits values. For example, use JAN, MAR, APR to specify more than one month.

- Specifies a range. For example, use JAN-MAR to specify more than one month.

* Specifies all values. For example, if Month is specified as *, the job is scheduled for every month.

? Specifies no specific value. This is only available for Day_of_month and Day_of_week, and is generally used when specifying a value for one and not the other.

/ Specifies increments. The number before the slash specifies when the intervals should begin, and the number after the slash is the interval amount. For example, if you specify 1/5 for Day_of_month, the Apex class runs every fifth day of the month, starting on the first of the month.

L Specifies the end of a range (last). This is only available for Day_of_month and Day_of_week. When used with Day of month, L always means the last day of the month, such as January 31, February 28 for leap years, and so on. When used with Day_of_week by itself, it always means 7 or SAT. When used with a Day_of_week value, it means the last of that type of day in the month. For example, if you specify 2L, you are specifying the last Monday of the month. Do not use a range of values with L as the results may be unexpected.

W Specifies the nearest weekday (Monday-Friday) of the given day. This is only available for Day_of_month. For example, if you specify 20W, and the 20th is a Saturday, the class runs on the 19th. If you specify 1W, and the first is a Saturday, the class does not run in the previous month, but on the third, which is the following Monday. Tip: Use the L and W together to specify the last weekday of the month.

# Specifies the nth day of the month, in the format weekday#day_of_month. This is only available for Day_of_week. The number before the # specifies weekday (SUN-SAT). The number after the # specifies the day of the month. For example, specifying 2#2 means the class runs on the second Monday of every month.

you can monitor the scheduled Apex job by going to
click Your Name | Setup | Monitoring | Scheduled Jobs

for more info:
Salesforce link