Showing posts with label Lead Conversion. Show all posts
Showing posts with label Lead Conversion. Show all posts

Sunday, January 3, 2016

How to Restrict creation of Contact on Lead Conversion

Here I'm trying to avoid the creation of Contact on Lead conversion.Here the thing is that we can't stop the contact creation on lead conversion because it's standard process .So to achieve this we have an alternative solution is to delete the contact after the lead got converted.

To Delete I have a written a trigger on Lead like below

 trigger noContactOnLeadConversion on Lead (after update)   
 {  
  list<contact> conlead=new list<contact>();  
  for(lead leadRec:Trigger.new)  
  {  
   if(trigger.oldMap.get(leadRec.id)isconverted==false && leadRec.isconverted && leadRec.ConvertedContactId != null)  
   {  
       contact con= new contact(Id=leadRec.ConvertedContactId);  
       conlead.add(con);  
   }  
  }  
  delete conlead;  
 }  





















When I start converting the lead I'm ending up with an error System.DmlException: Delete failed. First exception on row 0 with id 0039000001pvmhtAAA; first error: ENTITY_IS_DELETED, entity is deleted: [] .





















Then I added the logic to stop recursion trigger and tried with before trigger also but still I'm getting the same error with different screen but error is same







WorkAround for the Error 

Move the deletion of contact part to the @future method(i.e.,Asynchronous execution )and use Boolean variable to stop Recursion trigger.Below is the modified code snippet.

Trigger code:
 trigger noContactOnLeadConversion on Lead (after update)   
 {  
 if(!LeadUpdateHandler.stopleadUpdateTrigger)  
 {  
  LeadUpdateHandler.stopleadUpdateTrigger= true;  
  list<contact> conlead=new list<contact>();  
  for(lead leadRec:Trigger.new)  
  {  
   if(trigger.oldMap.get(leadRec.id).isconverted==false && leadRec.isconverted && leadRec.ConvertedContactId != null)  
   {  
    contact con= new contact(Id=leadRec.ConvertedContactId);  
    conlead.add(con);  
   }  
  }  
  LeadUpdateHandler.AsyncExecution(JSON.serialize(conlead));  
  }  
 }  

Handler Class Code:
 global class LeadUpdateHandler  
 {  
   public static boolean stopleadUpdateTrigger = false;  
   @future  
   public static void AsyncExecution(String JsonStr)  
   {  
    List<Contact> conList = new List<Contact>();  
     conList = (List<Contact>)JSON.deserialize(JsonStr, List<Contact>.class);  
     delete conList;  
   }  
 }  

For more update please join this site or just like my Facebook page I Love Coding..You? 

Enjoy.....!

Friday, May 3, 2013

Dynamic Columns in Salesforces Visualforce Tables

As we already know that in Production we can not modify the Apex classes and Trigger.So we hate to think always in such way that how to control the apex from the configuration things/From Setup.

Recently we came across a situation where we need to control the number of columns displayed in visual force table.To control the number of columns from the set up we have multiple options available.

Solutions

1.Make use of the Field sets to control the columns in table.

2. Create a custom setting for filed that you want to handle as a Boolean variable to decide whether to add part of the table or not.

Using Custom Settings

1. Create a list custom setting along with 3 fields in it with data type as boolean (In my case I want to handle only this 3 fields for multiple tables)


2. Goto manage and create a one record for one table and select that check box true if you want to show in table vice versa
VisaulForce page
<apex:page controller="DynamicColums">
<apex:form >
<apex:pageBlock mode="edit" title="Configurable DataTable Demo">
  <apex:pageBlockSection >
    Select <apex:selectList value="{!selPicklist}"> 
             <apex:selectOptions value="{!picklistval}" />
    </apex:selectList>
     <apex:pageBlockTable value="{!accounts}" var="account" >
       <apex:column headerValue="Name" rendered="{!showName}">
     <apex:outputField value="{!account.name}"/>
     </apex:column>
     <apex:column headervalue="Type" rendered="{!showType}">
       <apex:outputField value="{!account.type}" />
     </apex:column>
     <apex:column headervalue="Phone" rendered="{!showPhone}">
       <apex:outputField value="{!account.Phone}" />
     </apex:column>
   </apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Controller:
public class DynamicColums {

  public Boolean showPhone {get; set;}
  public Boolean showType {get; set;}
  public Boolean showName {get; set;}
  public String selPicklist{get;set;}

  public List<Account> accounts {get; set;}

  public DynamicColums(){
    //Get reference to the custom setting
    ColumDisplay__c columnSettings = ColumDisplay__c.getInstance('cs2');

    //Set individual variables based on custom setting fields
    showPhone = columnSettings.showPhone__c;
    showType = columnSettings.showType__c;
    showName = columnSettings.showName__c;
    accounts = [SELECT Name, Type, Phone from Account];
    
  }
  
   Public List<SelectOption> getpicklistval()
   {
    List<SelectOption> temp = new List<SelectOption>();
    for(Account a : [SELECT Name, Type, Phone from Account where type!=null] )
    {
     temp.add(new SelectOption(a.type,a.type));
    }
   return temp;
}
}


OutPut:





Thanks for visiting enjoy...!