Tuesday, July 12, 2011

Setting URL for Dynamic Lookup page using Dynamic Component


<apex:page controller="DynamicLookupClass1">
<apex:form >
<apex:pageBlock >
 <apex:pageBlockSection id="section" title="Send To" >

<apex:selectList size="1"  value="{!lookupType}" >
    <apex:actionSupport event="onchange" rerender="lookupSection" action="{!changeLookupType}" status="lookupStatus"/>  
    <apex:selectOption itemValue="Contact"/>
    <apex:selectOption itemValue="Opportunity"/>  
</apex:selectList>
</apex:pageBlockSection>
<apex:actionstatus id="lookupStatus" onstart="alert('HI')" onstop="alert('End')"/>
<apex:outputpanel id="lookupSection">
      <apex:dynamicComponent id="dynCom" componentValue="{!myFieldSetSection}"/>
</apex:outputpanel>
</apex:pageBlock>
</apex:form>
</apex:page>

public class DynamicLookupClass1 {
     
      public DynamicLookup__c lkp {get; set;}
    public String lookupType { get; set; }     
    public Component.Apex.pageBlockSection getMyFieldSetSection() {       
         Component.Apex.pageBlockSection rTag = new Component.Apex.pageBlockSection();      
         Component.Apex.inputField oppTotal = new Component.Apex.inputField();
         if(lookupType == 'Opportunity')
         oppTotal.expressions.value='{!lkp.Opportunity__c}';
         else if(lookupType == 'Contact')
         oppTotal.expressions.value='{!lkp.Contact__c}';
         else
         oppTotal.expressions.value='{!lkp.Opportunity__c}';        
         rTag.childComponents.add(oppTotal);
         return rTag;
    }
   
    public void changeLookupType(){            
    }   
}

Saturday, November 13, 2010

RecordType Mapping between Lead and Account object While LeadConversion


Objective: To map each record type on lead with the record type of account object while lead conversion. If the  mapped record type is not exists for Account object than default record type should be assigned.
There are 2 ways to map the record types of lead to account while lead conversion. Before starting create some record types on Lead object and Account object.
#1 way:
è  Create a before update trigger on lead to map the record types
#2 way:
è  Creating workflow rule for each record type mapping on account object
To achieve above objective using #1 way:
-          Create a custom object with name ‘Record Type Mapping’.

-          Create 2 custom formula fields of type text:
a)       Lead Record Type Name
b)       Account Record Type Name

-          Insert some record type mapping records which exists on Lead and Account object.

-          Now create a before update trigger on Lead object
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

trigger trigRecordTypeMapping on Lead (before update) {

    for(Lead lead:System.Trigger.new)
    {
         //Check whether convert  request coming for the first time
        if (Trigger.new[0].IsConverted == true && Trigger.old[0].isConverted == false)
        {
            //get lead object
            Lead leadobj = [SELECT Id,RecordType.name FROM Lead WHERE Id = :lead.Id];
          
            //get Account record type name mapped to Lead record type name in Record_Type_Mapping__c custom object  
            String AccRTypeName = [SELECT Account_Record_Type__c FROM Record_Type_Mapping__c WHERE Lead_Record_Type__c = :leadobj.RecordType.Name limit 1].Account_Record_Type__c;
         
            //Check wether record type exists for Account                  
            if([Select Id From RecordType where sobjecttype  = 'Account' and name = :AccRTypeName limit 1].size() > 0 )
            {
                   //get record type id using record type name
                   Id id = [Select Id From RecordType where sobjecttype  = 'Account' and name = :AccRTypeName limit 1].id;
               
                   //get account object to update      
                   Account acc = [SELECT Id,RecordType.Name FROM Account WHERE Id = :lead.ConvertedAccountId];
          
                   //assign record type id to Account
                   acc.RecordTypeId = id ;
             
                   //update Account
                   update acc;            
            }
      
        }
    }

}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

-          Below  is Test method for above trigger :
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

@isTest
private class TestLeadConversion
{

static testMethod void leadConversionTest()
{

Lead lead = new Lead();
lead.Email = 'test@test.com';
lead.lastname = 'Testerson';
lead.Company = 'Market Services';
lead.RecordTypeId = '01280000000Lq5E';//'Polaris';
//lead.Status = 'Fresh';
lead.OwnerId = '00580000001rP69';
insert lead;

//start testing
//Test.startTest();

//convert lead
Database.Leadconvert lc = new database.Leadconvert();
lc.setLeadId(lead.Id);
lc.setOwnerId('00580000001rP69');
//lc.setConvertedStatus('Closed - Converted');
LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
Database.Leadconvertresult lcr = database.convertLead(lc);

//verify lead successfully converted
system.assert(lcr.isSuccess()); //PASSED

//get Account
Account acc = [SELECT Id,RecordType.Name FROM Account WHERE Id = :lcr.getAccountId()];

//get Lead
Lead leadobj = [SELECT Id,RecordType.name FROM Lead WHERE Id = :lead.Id];

//get Account record type name mapped to Lead record type name in Record_Type_Mapping__c custom object  
String AccRTypeName = [SELECT Account_Record_Type__c FROM Record_Type_Mapping__c WHERE Lead_Record_Type__c = :leadobj.RecordType.Name limit 1].Account_Record_Type__c;

system.assertEquals(acc.recordType.Name,AccRTypeName);

//end testing
//Test.stopTest();

}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
To achieve above objective using #2 way:
1.  Create a formula field on the Lead and populate this with the Lead Record Type ID field
2.  Create a Large Text field on the Account with length = 1300 (needed to match length of formula field) called Lead Record Type
3.  Update Lead Mapping to map the Lead Record Type field with the Opportunity: Lead Record Type field
4.  Create a workflow rule on the Account for each record type with the criteria of Lead Record Type = record type id.
5.  Add a Field Update on the rule to set the Account Record Type to the appropriate record type matching Lead Record Type.
Note:  for each record type mapping you should create a workflow rule and field update action on that.