Tuesday, 24 June 2014

How to pass lookup field id to controller?

Lets take an example. Suppose we want pass Accountid(look up field from contact to account) from visual force page to controller. If we choose one account from look up then automatically account industry will be populated.

Here is the page:-

<apex:page controller="passingLookupIdController">
    function doSomething(test) {
        // lkfield.value now has the selected ID value
 <apex:form >
  <apex:actionFunction name="PassingParameter1" action="{!PassingParameter}" reRender="field1">
  <apex:param value="" name="recordId"/>

   <apex:pageBlock >
          <apex:pageBlockSection id="someArea">

               <apex:inputField value="{!con.AccountId}" id="field">
                  <apex:actionSupport event="onchange" reRender="field"oncomplete="doSomething('{!con.AccountId}');"/>
               <apex:inputField value="{!account.Industry}" id="field1" />

Here is controller:-

public with sharing class passingLookupIdController {
    public Contact con{get;set;}
     public Account account{get;set;}
    public passingLookupIdController (){
        con =  new Contact();       
    public PageReference PassingParameter(){
        Id recordId = ApexPages.currentPage().getParameters().get('recordId');
        System.debug('Print--->'+recordId );
        account = [SELECT ID,Industry FROM Account WHERE id=:recordId ]; 
        return null; 

How to use required attribute in picklist as like salesforce standard?

There is an attribute called Required which can be used in almost all visual-force component.If we use that attribute then red color mark will be appended with the field and if we don't enter any value ,try to save,then it shows one error(You must enter some value) just below to that field. 

For example:-
<apex:inputField value="{!account.Industry}" id="field1" required="true"/> 

save image Here Industry is a standard pick-list, if we write required = true, red mark is coming,however if we are making pick-list in controller and displaying using <apex:selectlist> and <apex:selectoptions> then red won't be displayed. 
<apex:selectList label="Picklist" multiselect="false" size="1" required = "true">
               <apex:selectOptions value="{!options }">


options = new list<SelectOption>();
        options.add(new SelectOption('-None-','-None-'));
        options.add(new SelectOption('A','A'));
        options.add(new SelectOption('b','b'));
        options.add(new SelectOption('c','c'));
        options.add(new SelectOption('d','d'));

It will display like below image

save image 
How can we display like standard pick-list?
Below is some code for this.
<apex:pageBlockSection id="someArea">
          <apex:pageBlockSectionItem >
               <apex:outputLabel value="picklistLabel"/>
                 <apex:outputPanel layout="block" styleClass="requiredInput">
                     <apex:outputpanel layout="block" styleClass="requiredBlock"/>
                     <apex:selectList label="Picklist" multiselect="false" size="1" >
                            <apex:selectOptions value="{!options }"/>

save image

Wednesday, 18 June 2014

How to permanently delete record from data base.

As we all know if we delete records from our org. It will be stored in recycle bin for 15 days. We can go to recycle bin and delete manually. 
Suppose the requirement is when we delete records those records should be deleted from recycle bin.
For this requirement we have to write a trigger on object. 

trigger todelteRecordPermanent on Account (after delete) {
    list<Id> Ids = new list<Id>();
    for(Account  account: Trigger.old){

How to get deleted records from recylebin and restore them to database

We need to use ALL ROWS keyword in soql query.
ALL ROWS keywords need to be used to query all records in an organization, including deleted records( records in your organization's Recycle Bin) and archived activities. 

You cannot use the ALL ROWS keywords with the FOR UPDATE keywords. 

list<Account> listOfAccounts = [SELECT id,Name FROM Account WHERE isDeleted = true ALL ROWS];

undelete listOfAccounts ;

isDeleted:- It is a standard field of account used to differentiate whether account is in Recycle Bin or not. 

undelete:- Its a DML Operation.

FOR UPDATE:Keywords used in SOQL to lock an sobject records and preventing from being updated. 
P.S.:- ORDER BY and FOR UPDATE keywords in any SOQL can not be used together.

Tuesday, 17 June 2014

How to add validation through trigger in salesforce

Hi All,
This post is regarding to add validation and to display error message through trigger. 
Suppose we want to give validation is no account should be created having name as 'asish'
We can do this by validation rule easily,in some case we want through trigger. Keep in mind trigger should be ours last choice.We should use standard functionality as much as possible.

Here is trigger for above validation:-

trigger ValidateAccount on Account (before insert,before update) {
    for(Account account:Trigger.new){
        if(account.Name == 'asish'){
            account.Name.addError('You can not create account having name as Asish');

Here is the output showing error message while creating account.

save image

Friday, 13 June 2014

Displaying Image In Standard Layout

Hi All,
This post is regarding to store image in a field of an Object or to displaying image in standard layout. 

  • Create an URL field in any object say Image_Url__c
  • Create an Formula Field in that object and refer the new url field .
  • Image = IMAGE( Image_Url__c , 'Company Logo',200,200).
  • Create Static Resource and store all the image in the static resource.
  • Click on View File link of each Static Resource and copy the Url.
  • Now create Visualforce page and controller.
  • In controller page paste link which you have copied from Static resource.
  • Run page and enter account name ,choose image and then click on save button.
This is the page
<apex:page controller="addImageTorecordsController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
               <apex:inputField value="{!account.Name}"/>
               <apex:selectList label="Image URL" value="{!url}" size="1" multiselect="false">
                   <apex:selectoptions value="{!options }">
               <apex:commandButton value="Save" action="{!savingaccount}"/>

This is the Controller:-

public with sharing class addImageTorecordsController {
    public Account account{get;set;}
    public List<SelectOption> options{get;set;}
    public String url{get;set;}
    public addImageTorecordsController (){
        account = new Account();
        options = new List<SelectOption>();
        options.add(new SelectOption('-None-','-None-'));
        options.add(new SelectOption('https://ap1.salesforce.com/resource/1323960689000/asish','Lord Hanuman'));
        options.add(new SelectOption('https://ap1.salesforce.com/resource/1380178695000/logo','Invoice It'));
        options.add(new SelectOption('https://ap1.salesforce.com/resource/1324022123000/Library','Library'));
        options.add(new SelectOption('https://ap1.salesforce.com/resource/1402657479000/Asishhndsome','Asish'));
    public PageReference savingaccount(){
       account.Image_Url__c = url;
       System.debug('printing Image-->'+url);
        insert account;
        return (new PageReference('/'+account.id));

Red Color text are static resource image link which will differ from Org to org.For you link will be different.

Thursday, 5 June 2014

Renaming Label Of a Field Of Managed Package.

Hi All, 

My previous post has explained how to change field labels of a standard field as well as custom fields of all objects.

 Changing abel of standard field as well as custom field lebel
However this post is regard to change field label of an object which is a part of a managed package.

save image

  • Click  Override in Setup page.
  • Choose package prefix.
  • Choose language according to your needs.
  • Choose object.
  • Choose Aspect as Field label and Related list label.
save image

    • You will find inline editing symbol in the Field label Override column and double click on that column.
    • Double click on  that column and write new label 
    • Click on Save.
    save image