Ads

Tuesday 16 July 2013

Getter and setter methods - What are they?

Well, once you start using a controller or an extension you will get used to these words...

So, it is good that we understand what these methods really do..

Getter and setter methods are used to pass data from your visualforce page to your controller and vice versa..

Let's take a very simple scenario... Let's assume you want to display a textbox in your visualforce page.. When the user enters some value in this textbox and clicks on a button you want the value entered by the user in your Apex class (ie. basically your controller or extension)

So go ahead and create a simple visualforce page.. the code for this would be
<apex:page controller="simplegetset">
  <apex:form>
    <apex:outputlabel value="Enter your name here"/>
       <apex:inputtext value="{!userinput}"/>        
  </apex:form>  
</apex:page>


The Apex code for this page would be...

public class simplegetset
{
    public String userinput{get; set;}
}


Now, the variable "userinput" in your Apex class will store the value entered by the user....

Let's analyze the methods now...

Get

The "get" method is used to pass data from your Apex code to your Visualforce page.. In our example we are not passing any value.. hence, when your page loads initially the textbox will have a empty value...

So, lets modify our code and pass a default value to the textbox.. Change the Apex code as follows..

public class simplegetset
{
    public String userinput;
    public String getuserinput(){return 'John';}
 
    public void setuserinput(String userinput)
    {
        this.userinput = userinput;
    }  
}


You can now see that your page loads with a value 'John'...

Set

The "set" method is used to pass values from your visualforce page to the controller... In our example the variable "userinput" will be storing the value entered in the textbox..

Now modify your VF page as below..

<apex:page controller="simplegetset">
  <apex:form>
    <apex:outputlabel value="Enter your name here"/>
       <apex:inputtext value="{!userinput}">
           <apex:actionsupport event="onclick" rerender="display" />
       </apex:inputtext>                  
    <apex:outputpanel id="display">
        <apex:outputtext value="The name entered is {!userinput}"/>
    </apex:outputpanel>                  
  </apex:form>  
</apex:page>


The Apex code would be...

public class simplegetset
{
    public String userinput{get; set;}
}


In this example what happens is.. the variable "userinput" stores the value entered in visualforce page and passes it to the Apex code.. hence you are able to see the entered value in the visualforce page..

I guess you might understand what i am saying.. to make things simple now use the same visualforce page.. but modify the Apex code as below..

public class simplegetset
{
    public String userinput;
    public String getuserinput(){return 'John';}
 
    public void setuserinput(String userinput)
    {
        this.userinput = userinput;
    }  
}


Now, whatever value you enter the page always displays "The name entered is John".... This is because your get method always returns 'John'... but still your set method will store the value you entered in the variable "userinput"....

Monday 15 July 2013

Salesforce delete record: 'Insufficient privileges' error?

In some organisation, user allow to delete records, but sometimes user get error message 'Insufficient privileges' although Delete button exist. What's happening? Let us analyze:

1. User able to access the record
There are few scenario:

Organization-Wide Defaults sharing setting on that object is Public Read Only or Public Read/Write; or
Sharing rule added based on record owner or criteria for Public Groups or Roles; or
User is the owner of the records; or
User in higher role hierarchy of record owner; or
User profile have "View all Data" or "Modify all Data" permission

2. User see Delete button
This is happened because:

Delete button is added in record page layout; and
User profile have permission to delete that object

3. Error 'Insufficient privileges'
The ability to delete records in Salesforce is controlled by the role hierarchy. Setting a sharing model to "Public Read/Write" alone does not give users the right to delete others records. There are 2 scenarios in which a user can delete a record:

The user attempting to delete the record is a System Administrator.
The user attempting to delete the record is the owner, or higher on the role hierarchy than the record owner.
Any other user that attempts to delete a record will receive an "Insufficient Privilege" error message.

Those below you on the role hierarchy may have read/write privileges according to the sharing model rules, however, they may not delete information from those individuals above them in that hierarchy.

Sunday 14 July 2013

How to undelete records Using Apex?

Salesforce keep record you delete to Recycle Bin for 15 days with maximum record of 25 times the Megabytes (MBs) in your storage. For example, if your organization has 1 GB of storage then your limit is 25 times 1000 MB or 25,000 records. If your organization reaches its Recycle Bin limit, Salesforce automatically removes the oldest records if they have been in the Recycle Bin for at least two hours.

You can manually undelete records by click Recycle Bin icon in bottom left menu. Undelete account will include contact, case, opportunity tagged to that Account. But, if you have many records or you have specific record Id or you want to undelete records with some conditions, manually delete is not an option, use Apex code for it.

You not really need to be good developer to utilize Apex code. Here we go:

1. Open Developer Console
From your name in top right, look for Developer Console











2.From Developer Console
Go to Debug menu and select Open Execute Anonymous Window











3. Enter SOQL 
Example:
Account[] a = [SELECT Id FROM Account WHERE name = 'singtel' ALL ROWS];
undelete a;
Important to add ALL ROWS, otherwise deleted records will be not return on query.






4. Monitor Apex Debug Log
Go to Monitor - Logs - Debug Logs (in Salesforce menu, not Developer console)
Add user to monitor, default it will capture 20 log request for the users

5. Execute 
Click Execute button in Enter Apex Code window

6. Back to Debug Log
Select latest debug log and click View to monitor

Tuesday 9 July 2013

How to fetch Contact details from Opportunity using SOQL in Salesforce?

Query:

SELECT Id, Opportunity.Name, Opportunity.OrderNumber__c, Opportunity.Approval_Status__c, Contact.Email, Contact.Name FROM OpportunityContactRole

Output:


REQUIRED CHECKBOX VALIDATION?

So we now how to enable a required field in a record, but how about making a requried checkbox in your Salesforce form? well it's simple and here is how to do it:

Field 1: Lookup field called : "Account"
Field 2: Date field called "Date"
Field 3: Date field called "Expected Completion Date"
Field4: Checkbox called "Review""

AND(Review__c = FALSE,
OR(
NOT(ISBLANK(Account__c)),
NOT(ISBLANK(Date__c)),
NOT(ISBLANK(Expected_Completion__Date__c))))


and now, If the user enters data in to any one, any two, or all three fields, but does NOT check the box, an error will be thrown.

"SYSTEM.QUERYEXCEPTION: LIST HAS NO ROWS FOR ASSIGNMENT TO SOBJECT"

So I was quering Account a = [Select name from account where id=: strld];
and an exception was thrown: System.QueryExceptoin: List has no rows for assignment to SObject.

Here is the mistake I made and how to fix it:

List<Account>lst = [Select name from account where id=: strld];



WORKBENCH FOR SALESFORCE ?

What is WorkBench ?

Here is the another interesting GUI based tool for Salesforce though announced in Dreamforce before,  this is a free tool from Salesforce.com and not just limited for developers. Other than developer related offering, this tool leverage bunch of different features that you can access through web-based UI to work with Salesforce


Workbench pleasant features 


  • Query the results, download bulk XML and .csv files 
  • Pretty structured information is provided alike Data Loader
  • Offer more data load options than data loader
  • Offer tons of options for developers
  • Handy and web based tool, with easy access to both Production and Sandbox 
  • Provided with Chrome extension and Firefox Add-on to ease the life 


Some Limitations
  • Can't save the data load mapping, which is turn-off
  • Can't schedule, no scheduled options



Workbench is insanely powerful tool for admins and developer to interact with Salesforce.com. This purely web-based tool that uses oauth to connect your salesforce org. The WorkBench use force.com API and supports bulk api calls, rest call, streaming data and surprisingly these all do not end here. The best feature I like about this application is SOQL query calls from to Salesforce cloud right away with pretty interactive interface

You can query, manipulate data and the metadata in Salesforce org, this also leverage advance features like debug logs and backward compatibility testing and single sign-on integration




Query Results and Pagination 

Query results are shown there itself while in the old versions, the users were required to jump from page to page to retrieve records,  but now with pagination support, the row number on SOQL query results continue from page to page and make data view pretty easy







Browser Support 


Workbench comes with chrome extension that ease the access, and in no time work bench is ready to use and can be pulled up right inside your favorite chrome browser



How to get started with Workbench ?













Login with your org credentials and select objects







Pull up a data using Query