Search This Blog

Thursday 2 August 2012

Pagination in Visualforce Page

Salesforce.com introduced the StandardSetController in Winter ’09 . The new pagination feature is pretty powerful and easy to use with standard as well as custom objects. Even though Jon Mountjoy has a good blog post here, there appears to be very little documentation or examples for pagination.
I threw together a small demo that allows you to page through the query results and select multiple items to process. Instead of simply returning the list of sObjects from the query locator’s current set, a collection of wrapper objects are returned enabling the user to check items for processing.
You can run this demo on my developer site.
paging1
PagingController.cls – Custom Controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
public with sharing class PagingController {
 
 List<categoryWrapper> categories {get;set;}
 
 // instantiate the StandardSetController from a query locator
 public ApexPages.StandardSetController con {
  get {
   if(con == null) {
    con = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, Name FROM Cat3__c Order By Name limit 100]));
    // sets the number of records in each page set
    con.setPageSize(5);
   }
   return con;
  }
  set;
 }
 
 // returns a list of wrapper objects for the sObjects in the current page set
 public List<categoryWrapper> getCategories() {
  categories = new List<categoryWrapper>();
  for (Cat3__c category : (List<cat3__c>)con.getRecords())
   categories.add(new CategoryWrapper(category));
 
  return categories;
 }
 
 // displays the selected items
  public PageReference process() {
   for (CategoryWrapper cw : categories) {
    if (cw.checked)
     ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,cw.cat.name));
   }
   return null;
  }
 
 // indicates whether there are more records after the current page set.
 public Boolean hasNext {
  get {
   return con.getHasNext();
  }
  set;
 }
 
 // indicates whether there are more records before the current page set.
 public Boolean hasPrevious {
  get {
   return con.getHasPrevious();
  }
  set;
 }
 
 // returns the page number of the current page set
 public Integer pageNumber {
  get {
   return con.getPageNumber();
  }
  set;
 }
 
 // returns the first page of records
  public void first() {
   con.first();
  }
 
  // returns the last page of records
  public void last() {
   con.last();
  }
 
  // returns the previous page of records
  public void previous() {
   con.previous();
  }
 
  // returns the next page of records
  public void next() {
   con.next();
  }
 
  // returns the PageReference of the original page, if known, or the home page.
  public void cancel() {
   con.cancel();
  }
 
}
CategoryWrapper.cls – Wrapper class for the Categories
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class CategoryWrapper {
 
    public Boolean checked{ get; set; }
    public Cat3__c cat { get; set;}
 
    public CategoryWrapper(){
        cat = new Cat3__c();
        checked = false;
    }
 
    public CategoryWrapper(Cat3__c c){
        cat = c;
        checked = false;
    }
 
    public static testMethod void testMe() {
 
     CategoryWrapper cw = new CategoryWrapper();
     System.assertEquals(cw.checked,false);
 
     CategoryWrapper cw2 = new CategoryWrapper(new Cat3__c(name='Test1'));
     System.assertEquals(cw2.cat.name,'Test1');
     System.assertEquals(cw2.checked,false);
 
    }
 
}
Category_Paging.page – Visualforce page
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<apex:page controller="PagingController">
  <apex:form >
    <apex:pageBlock title="Paging through Categories of Stuff">
 
      <apex:pageBlockButtons location="top">
        <apex:commandButton action="{!process}" value="Process Selected"/>
        <apex:commandButton action="{!cancel}" value="Cancel"/>
      </apex:pageBlockButtons>
      <apex:pageMessages />
 
      <apex:pageBlockSection title="Category Results -  Page #{!pageNumber}" columns="1">
        <apex:pageBlockTable value="{!categories}" var="c">
          <apex:column width="25px">
            <apex:inputCheckbox value="{!c.checked}"/>
          </apex:column>
          <apex:column value="{!c.cat.Name}" headerValue="Name"/>
        </apex:pageBlockTable>
      </apex:pageBlockSection>
    </apex:pageBlock>
 
    <apex:panelGrid columns="4">
    <apex:commandLink action="{!first}">First</apex:commandlink>
    <apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
    <apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandlink>
    <apex:commandLink action="{!last}">Last</apex:commandlink>
    </apex:panelGrid>
 
  </apex:form>
</apex:page>

Wednesday 25 July 2012

Lock a Record in Salesforce

There are three ways to lock a record in Salesforce
1) Trigger
2) Record Types
3) Validation Rules
Trigger
This is the first option the would crawl in to peoples mind whenever there is record locking involved is Trigger. This might be an option to lock a record but writing lot of triggers might bring us close to governor limits. Hence, this would be the last option for me.
Record Types
Lets assume that the record has a check box call ‘Restrict’. Create a Read Only record and create a workflow rules that changes the record type to ‘Read Only’ if the ‘Restrict’ check box is checked.
Validation Rules
Write a validation rules saying (account.yourfield__c, true), then display an error message saying “Record is read only”

Tuesday 24 July 2012

Salesforce Interview Questions

1.What is the difference between map and set in  Salesforce  collections.
Map:
A map is a collection of key-value pairs where each unique key maps to a single value. Keys can be any primitive data type, while values can be a primitive, sObject, collection type or an Apex object. For example, the following table represents a map of countries and currencies:
Country (Key) 'United States' 'Japan' 'France' 'England' 'India'
Currency (Value) 'Dollar' 'Yen' 'Euro' 'Pound' 'Rupee'
Similar to lists, map values can contain any collection, and can be nested within one another. For example, you can have a map of Integers to maps, which, in turn, map Strings to lists. A map can only contain up to five levels of nested collections inside it.
Set:
A set is an unordered collection of primitives or sObjects that do not contain any duplicate elements. For example, the following table represents a set of String, that uses city names:
'San Francisco' 'New York' 'Paris' 'Tokyo'
To declare a set, use the Set keyword followed by the primitive data type name within <> characters. For example:
 Triggers are used to perform immediate actions based on previous action e.g. field update. An apex language is used to write triggers
3. What are different types of Triggers?
 There are two types of triggers
a) Before trigger- is used before database operation
 b) After trigger-is used  to access field values that are set by the database (such as a record's Id or lastUpdated field), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue.
4. Why Use Visual Force?
 Visual force is a Sales force UI markup language using visual force we can construct   WebPages. In Sales force MVC architecture Visual force represents View.
5. How do you handle errors?
 Error handing is done directly displaying in Visual force page using Apex Pages methods   to end users or displaying in debug logs.

 http://www.salesforce.com/us/developer/docs/pages/Content/apex_methods_system_apexpages.htm?SearchType=Stem
6. What is a Component?
In sales force component is a reusable piece of code developed using Visual force and Apex controller.

7.Tell me about a project with Salesforce in which you led the architecture that was not explicitly limited to the Salesforce infrastructure.


8.How long ago did you work on the above mentioned project?



9.Tell me about inner-department communications and your approach to communicating with non-technical team members.


10.What measures have you taken to make your software products more easily maintainable?

11.What development processes have you used in your recent projects? Were any tools used to support these processes? If so can you name some of the advantages and short comings of the tool/ tools?

12.What is an junction object?.

13.What is the difference between master detail relationship and look up relationship. ?
https://login.salesforce.com/help/doc/en/overview_of_custom_object_relationships.htm

14.How will you handle 10001 SOQL  problem in apex?


15. Define force.com and Salesforce.com?


16.Explain some examples  for work-flow rules?


17.What is the use of  future method?
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_future.htm

18.What is abstract class?.

19.Explain object oriented concepts in sales force how will you implement this?

20.How will you import and export data between two systems eg: SFDC and SQL? 


21.What is appexchange?


22.Explain about cloud?.


23.Explain about validation rules in salesforce?.


24.What is the difference between roles and profiles?.


25.What is the difference between record level ,field level, object level security?


26.How will you use web services in sales force.com?.


27.Can you explain about limitation why this limitation placed in sales force.


28.what is view state in sales force?.It there is any limitation after winter11?

29.What is the difference between Role and Profile?

30.How you handle part of page refresh using VF?

31.How you can define a trigger is executed successfully?

32.Explain about yours current project?

33.How do you rate yours self in apex,VF?

34.How you can  deal with project dead lines?

35.How you can use external WSDL files in Sales force?

36.What is difference between rest full and soap API?

37.What is yours Strong point Sales force?
38.How you can with SQL injections in Sales force?
39.Can you explain test methods in salesforce?
40.How did you display error messages in salesforce? On VF Pages?
Apex.Pages
41.Can you explain relationships in salesforce?
42.What are SOQL limitations?
43.How can bypass SOQL statement to fetch more 1000 records? 
44.What job did you like more in your carrier?
45.Can you explain a scenario integrating login method to third party application.
46.Can you explain about SFDC controllers?
47.Can you explain about sales process? 
48.What is a set in salesforce deployments?
49.How many ways you can invoke an work flow? 
50.What are governor limits can you explain about it?
51.East Sales Team can't access west sales team data how do you configure it?
52.What is Role,Profile,Security?
53.I have an contact .It phone number field is empty how you can get phone number from an related account?
54.How do you handle SOQL problem in Trigger?
55.How you can access JQuery in VF page?
56.What is change set in salesforce?
57. What is transient variable in salesforce?
58.How you can measure trigger performance?

Thursday 19 July 2012

Solution: List has no row assignment issue


We assumed set SOQL query returned values assign to object's instance. If SOQL query returned null values means the "List has no row assignment" causes arising. we can solved by object's list instance.

Method1: Account acc = [Select Name from Account];

Method2: List<Account> listacc = [Select Name from Account];

If query returned null value means Method1 raises the respective cause, but Method2 is doesn't raise any causes and 'listacc' have 0 values. So we can controlled by listacc.size( ) method.

Get result from Dynamic SOQL query

  If we want get result from dynamic soql query as per sample here

   sObject queryResult = Database.query('Select \''+field +'\' from sObject where id = \''+ recordid +'\'');
            
   String Text = (String)queryResult.get(field_Name));

   Using this we can get dynamic query result.