Thursday, October 25, 2012

Enhanced list views on a Visualforce page

Enhanced lists are used when you want to display a specific list view for a standard or custom object, rather than having all list views available for the user to select.
Step 1 – Create the list view
Go to the standard or custom object and click on the Create New View next to the Go button. Create the view and click on Save. When the page has reloaded, you will notice in the url a 15 digit id has been created for your view. Copy or make a note of the id.
 
 
 
 
 
 
 
 
 
 
Step 2 – Add code to your visualforce page
Add the following bit of code to your visualforce page, replacing the listid with your id:
<apex:enhancedlist type=”Lead” height=”730″ customizable=”false” rowsPerPage=”25″ Listid=”00B30000007wesH” />
Change customizable to equal true if you want your users to be able to edit the filters on the view. Once saved, you should find your visualforce page displays an enhanced list similar in appearance to the image below.

Change Password using apex code

setPasswordID userID
String password
VoidSets the password for the specified user. When the user logs in with this password, they are not prompted to create a new password. Use resetPassword if you want the user to go through the reset process and create their own password.
Warning
Be careful with this method, and do not expose this functionality to end-users.

e.g. System.setPassword('006A123ahjklkk','password123');
 

Hide Salesforce Logo

You can hide the logo from top using the javascript below.


<script language="JavaScript">try{var pa=document.getElementById("phHeaderLogoImage"); pa.style.display = "none";}catch(err){alert('There was an error on this webpage='+err.description);}</script>


You can either use it in visualforce page or add it in homepage components.

Check if inputTextField contains numbers only

Many times you want to check if the inputText contains numbers of decimals only e.g. getting amount in input.


public boolean isValidNumber(String inputNumber){
     Pattern p = Pattern.compile('\\d+(\\.\\d+)?');
     Matcher m =p.matcher(inputNumber);
     return m.Matches();
}

The above method return true if it is a valid number else return false.

Saturday, April 28, 2012

Copy related list from Lead to Account/Contact/Opportunity


There is a salesforce limitation that when a Lead is converted to Account, all the related objects records are lost. E.g. for a Lead, there is a related object named products in which a lead is interested. So for a lead, we can add multiple products in the Lead related list. But when we convert lead to Account, all the products related to Lead are gone!!

So for this, I have written a small code which explains how using a small trigger and few fields, we can retain the products by linking them to account, opportunities and contact. 

I have used a general term CustomObject__c(replace it with you real related object name).

Step 1)For each custom object related to Lead we need to introduce 3 fields.
  -Account__c
  -Contact__c
  -Opportunity__c

Step 2) Write a trigger on before update event on Lead viz. LeadBeforeUpdate
trigger LeadBeforeUpdate on Lead (before update) {
//This trigger will associate a Custom Object record with the new contact and account when Lead is converted
//The Custom Object is associated to an opportunity only if an opportunity record exist on the Lead.
   
    Set<Id> leadIds = new Set<Id>();
    Map<Id,Id> LeadIdContactId = new Map<Id,Id>();
    Map<Id,Id> LeadIdAccountId = new Map<Id,Id>();
    Map<Id,Id> LeadIdOpportunityId = new Map<Id,Id>();


    for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
             leadIds.add(Trigger.new[i].id);
             LeadIdContactId.put(Trigger.new[i].id,Trigger.new[i].ConvertedContactId);
             LeadIdAccountId.put(Trigger.new[i].id,Trigger.new[i].ConvertedAccountId);
             LeadIdOpportunityId.put(Trigger.new[i].id,Trigger.new[i].ConvertedOpportunityId);


        }
    }
   
    //Create a list of CustomObject__c to be updated
    List<CustomObject__c> CustomObjectsToUpdate = new List<CustomObject__c>();
       
           
    List<CustomObject__c> customObjectEntries = [select Contact__c, Opportunity__c, Account__c, Lead__c from CustomObject__c where lead__c in :leadIds];       
   

    for (Lead lead : [select id,ConvertedContactId,ConvertedOpportunityId,ConvertedAccountId from Lead where id in: leadIds])  {
             for (CustomObject__c CustomObject : customObjectEntries) {
                if (CustomObject.Lead__c == lead.Id) {
                    CustomObject.contact__c = LeadIdContactId.get(lead.id);
                    CustomObject.opportunity__c = LeadIdOpportunityId.get(lead.id);
                    CustomObject.account__c = LeadIdAccountId.get(lead.id);
                    CustomObjectsToUpdate.add(CustomObject);
                 }
            }
     }

     if(CustomObjectsToUpdate.size()>0)
         update CustomObjectsToUpdate;
      
} //End of trigger


Note: This trigger will work for multiple records to be copied and is bulk efficient.