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

Example:
 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="{!opportunity.name}"/>  
   <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:pageBlockButtons>  
       <apex:actionRegion >  
         <apex:pageBlockSection title="Basic Information" columns="1">  
           <apex:inputField value="{!opportunity.name}"/>  
           <apex:pageBlockSectionItem >  
             <apex:outputLabel value="Stage"/>  
             <apex:outputPanel >  
               <apex:inputField value="{!opportunity.stageName}">  
                 <apex:actionSupport event="onchange" rerender="thePanel"  
                           status="status"/>  
               </apex:inputField>  
               <apex:actionStatus startText="applying value..." id="status"/>  
             </apex:outputPanel>  
           </apex:pageBlockSectionItem>  
           <apex:inputField value="{!opportunity.amount}"/>  
           <apex:inputField value="{!opportunity.closedate}"/>  
         </apex:pageBlockSection>  
       </apex:actionRegion>  
       <apex:outputPanel id=”thePanel”>        
          <apex:pageBlockSection title="Closed Lost Information" columns="1"  
                   rendered="{!opportunity.stageName == 'Closed Lost'}">  
         <apex:inputField value="{!opportunity.Reason_Lost__c}"/>  
       </apex:pageBlockSection>  
      </apex:outputPanel>  
     </apex:pageBlock>  
   </apex:form>  
 </apex:page>  


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.
Ex:
 <apex:pageBlockSectionItem >  
 <apex:outputpanel id="panel 1">  
 <apex:outputLabel value="LabelName" rendered="{!oppty.field== 'Other'}"/>  
  </apex:outputpanel>  
  <apex:outputpanel id="panel 2">  
 <apex:inputfield value="{!field}" rendered="{!oppty.field == 'Other'}" />  
 </apex:outputpanel>  
 </apex:pageBlockSectionItem>  

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:
10MB.
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:
50MB


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:
100MB


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">
{!d}
</apex:column>
<apex:column headerValue="Values">
{!data[d]}
</apex:column>
</apex:pageBlockTable>



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.today();
Date todaydateinstance = date.newinstance(todaydate.year(), todaydate.month(), todaydate.day());

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:pageBlock>  
<apex:pageBlockSection>
<apex:pageBlockSectionItem helpText="{!$ObjectType.objectName__c.Fields.fieldName__C.inlineHelpText}" >
<apex:outputLabel> FieldName </apex:outputLabel>
<apex:inputText> FieldName__c </apex:inputText>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>