+ Start a Discussion
SrinuSrinu 

trigger on the Lead (prospect) on save or update to change the OwnerID of the Lead

Hello Everyone,

Below is requirement:

 

(1)First Obtain the RecordTypeId when saving or updating the Lead. 

  Select RecordTypeId from Lead where ID = :Id

 

(2)If RecordTypeId = 01260000000Q32MAAS

 Query = [Select Owner__c from Prospect_Assignment__c where Active__c = True  and Country__c INCLUDES (:ld.Country) and Prospect_Record_Type__c = 'MCI' and Postal_Code_Prefix__c INCLUDES (:ld.Postal_Code_Prefix__c) and State_Province__c INCLUDES(:ld.State) and  Stratus_Product__c =: ld.Stratus_Product_s__c and Prospect_Type__c =:ld.Prospect_Type__c];

 

(3)And then update the lead so the OwnerId =  Query.Owner__c

 

To satisfy the above requirement, I wrote a Trigger on Lead (Prospect).

trigger UpdateLeadOwner on Lead (before insert, before update) {
  
  
    System.debug('----------------------------Trigger');
    
    
    String lCountryStr = '';   
    
    String lPCPrefixStr = '';
    
    String lStateStr = '';

    Set<String> lStratusProductSet = new Set<String>();

    Set<String> lProspectTypeSet = new Set<String>();

    for(Lead ld: Trigger.new) {

        if(ld.Country != null && ld.Country != '') {
            
            System.debug('------------------------------ld.Country:'+ld.Country);

            lCountryStr += ld.Country;
        }
        
        if(ld.Postal_Code_Prefix__c != null && ld.Postal_Code_Prefix__c != '') {

            System.debug('------------------------------ld.Postal_Code_Prefix__c:'+ld.Postal_Code_Prefix__c);

            lPCPrefixStr += ld.Postal_Code_Prefix__c; //','+
        }

        if(ld.State != null && ld.State != '') {

            System.debug('----------------------------------ld.State:'+ld.State);

            lStateStr += ld.State;  //','+
        }

        if(ld.Stratus_Product_s__c != null && ld.Stratus_Product_s__c != '') {

            System.debug('---------------------------------ld.Stratus_Product_s__c:'+ld.Stratus_Product_s__c);

            lStratusProductSet.add(ld.Stratus_Product_s__c);
        }

        if(ld.Prospect_Type__c != null && ld.Prospect_Type__c != '') {

            System.debug('---------------------------------ld.Prospect_Type__c:'+ld.Prospect_Type__c);

            lProspectTypeSet.add(ld.Prospect_Type__c);
        }
    }

    System.debug('----------------------------------lCountryStr:'+lCountryStr);
    System.debug('----------------------------------lPCPrefixStr:'+lPCPrefixStr);
    System.debug('-----------------------------------lStateStr:'+lStateStr);
    System.debug('-----------------------------------lStratusProductSet:'+lStratusProductSet);
    System.debug('------------------------------------lProspectTypeSet:'+lProspectTypeSet);
    List<Prospect_Assignment__c> prosAssList =  [Select Owner__c, Country__c, Prospect_Record_Type__c, Postal_Code_Prefix__c, State_Province__c, Stratus_Product__c, Prospect_Type__c from            

Prospect_Assignment__c where Active__c = True and Country__c INCLUDES (:lCountryStr) and Prospect_Type__c IN:lProspectTypeSet and Stratus_Product__c IN: lStratusProductSet and            

Postal_Code_Prefix__c INCLUDES (:lPCPrefixStr) and Prospect_Record_Type__c = 'MCI'  and State_Province__c INCLUDES(:lStateStr)];   
    
     for(Lead ld:Trigger.new) {
        for(Prospect_Assignment__c prosAss:prosAssList) {

            if(ld.Country.contains(prosAss.Country__c)) {
                System.debug('----------------ld:'+ld);
                ld.OwnerId = prosAss.Owner__c;
                break;
            }

            if(ld.Postal_Code_Prefix__c.contains(prosAss.Postal_Code_Prefix__c)) {
                System.debug('----------------ld:'+ld);
                ld.OwnerId = prosAss.Owner__c;
                break;
            }

            if(ld.State.contains(prosAss.State_Province__c)) {
                System.debug('----------------ld:'+ld);
                ld.OwnerId = prosAss.Owner__c;
                break;
            }

            if(ld.Stratus_Product_s__c.contains(prosAss.Stratus_Product__c)) {
                System.debug('----------------ld:'+ld);
                ld.OwnerId = prosAss.Owner__c;
                break;
            }

            if(ld.Prospect_Type__c.contains(prosAss.Prospect_Type__c)) {
                System.debug('----------------ld:'+ld);
                ld.OwnerId = prosAss.Owner__c;
                break;
            }
        }
    }

Debug Log:

 

23.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
09:48:48.290 (290354000)|EXECUTION_STARTED
09:48:48.290 (290386000)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
09:48:48.290 (290406000)|CODE_UNIT_STARTED|[EXTERNAL]|01qP00000008f7J|UpdateLeadOwner on Lead trigger event BeforeUpdate for [00QP0000002dhMR]
09:48:48.291 (291257000)|SYSTEM_METHOD_ENTRY|[7]|System.debug(ANY)
09:48:48.291 (291308000)|USER_DEBUG|[7]|DEBUG|----------------------------Trigger
09:48:48.291 (291318000)|SYSTEM_METHOD_EXIT|[7]|System.debug(ANY)
09:48:48.291 (291786000)|SYSTEM_METHOD_ENTRY|[17]|LIST.iterator()
09:48:48.291 (291922000)|SYSTEM_METHOD_EXIT|[17]|LIST.iterator()
09:48:48.291 (291949000)|SYSTEM_METHOD_ENTRY|[17]|system.ListIterator.hasNext()
09:48:48.292 (292138000)|SYSTEM_METHOD_EXIT|[17]|system.ListIterator.hasNext()
09:48:48.292 (292166000)|SYSTEM_METHOD_ENTRY|[17]|system.ListIterator.next()
09:48:48.292 (292193000)|SYSTEM_METHOD_EXIT|[17]|system.ListIterator.next()
09:48:48.292 (292315000)|SYSTEM_METHOD_ENTRY|[20]|System.debug(ANY)
09:48:48.292 (292347000)|USER_DEBUG|[20]|DEBUG|------------------------------ld.Country:United States
09:48:48.292 (292354000)|SYSTEM_METHOD_EXIT|[20]|System.debug(ANY)
09:48:48.292 (292422000)|SYSTEM_METHOD_ENTRY|[24]|System.debug(ANY)
09:48:48.292 (292451000)|USER_DEBUG|[24]|DEBUG|------------------------------ld.Postal_Code_Prefix__c:901
09:48:48.292 (292459000)|SYSTEM_METHOD_EXIT|[24]|System.debug(ANY)
09:48:48.292 (292535000)|SYSTEM_METHOD_ENTRY|[28]|System.debug(ANY)
09:48:48.292 (292564000)|USER_DEBUG|[28]|DEBUG|----------------------------------ld.State:AR
09:48:48.292 (292571000)|SYSTEM_METHOD_EXIT|[28]|System.debug(ANY)
09:48:48.292 (292628000)|SYSTEM_METHOD_ENTRY|[32]|System.debug(ANY)
09:48:48.292 (292656000)|USER_DEBUG|[32]|DEBUG|---------------------------------ld.Stratus_Product_s__c:Avance
09:48:48.292 (292663000)|SYSTEM_METHOD_EXIT|[32]|System.debug(ANY)
09:48:48.292 (292692000)|SYSTEM_METHOD_ENTRY|[33]|SET.add(ANY)
09:48:48.292 (292733000)|SYSTEM_METHOD_EXIT|[33]|SET.add(ANY)
09:48:48.292 (292779000)|SYSTEM_METHOD_ENTRY|[36]|System.debug(ANY)
09:48:48.292 (292807000)|USER_DEBUG|[36]|DEBUG|---------------------------------ld.Prospect_Type__c:End User
09:48:48.292 (292814000)|SYSTEM_METHOD_EXIT|[36]|System.debug(ANY)
09:48:48.292 (292841000)|SYSTEM_METHOD_ENTRY|[37]|SET.add(ANY)
09:48:48.292 (292873000)|SYSTEM_METHOD_EXIT|[37]|SET.add(ANY)
09:48:48.292 (292881000)|SYSTEM_METHOD_ENTRY|[17]|system.ListIterator.hasNext()
09:48:48.292 (292891000)|SYSTEM_METHOD_EXIT|[17]|system.ListIterator.hasNext()
09:48:48.292 (292917000)|SYSTEM_METHOD_ENTRY|[40]|System.debug(ANY)
09:48:48.292 (292943000)|USER_DEBUG|[40]|DEBUG|----------------------------------lCountryStr:United States
09:48:48.292 (292950000)|SYSTEM_METHOD_EXIT|[40]|System.debug(ANY)
09:48:48.292 (292970000)|SYSTEM_METHOD_ENTRY|[41]|System.debug(ANY)
09:48:48.292 (292994000)|USER_DEBUG|[41]|DEBUG|----------------------------------lPCPrefixStr:901
09:48:48.293 (293001000)|SYSTEM_METHOD_EXIT|[41]|System.debug(ANY)
09:48:48.293 (293021000)|SYSTEM_METHOD_ENTRY|[42]|System.debug(ANY)
09:48:48.293 (293048000)|USER_DEBUG|[42]|DEBUG|-----------------------------------lStateStr:AR
09:48:48.293 (293055000)|SYSTEM_METHOD_EXIT|[42]|System.debug(ANY)
09:48:48.293 (293098000)|SYSTEM_METHOD_ENTRY|[43]|String.valueOf(Object)
09:48:48.293 (293160000)|SYSTEM_METHOD_EXIT|[43]|String.valueOf(Object)
09:48:48.293 (293183000)|SYSTEM_METHOD_ENTRY|[43]|System.debug(ANY)
09:48:48.293 (293192000)|USER_DEBUG|[43]|DEBUG|-----------------------------------lStratusProductSet:{Avance}
09:48:48.293 (293199000)|SYSTEM_METHOD_EXIT|[43]|System.debug(ANY)
09:48:48.293 (293234000)|SYSTEM_METHOD_ENTRY|[44]|String.valueOf(Object)
09:48:48.293 (293270000)|SYSTEM_METHOD_EXIT|[44]|String.valueOf(Object)
09:48:48.293 (293284000)|SYSTEM_METHOD_ENTRY|[44]|System.debug(ANY)
09:48:48.293 (293289000)|USER_DEBUG|[44]|DEBUG|------------------------------------lProspectTypeSet:{End User}
09:48:48.293 (293294000)|SYSTEM_METHOD_EXIT|[44]|System.debug(ANY)
09:48:48.294 (294011000)|SOQL_EXECUTE_BEGIN|[45]|Aggregations:0|select Owner__c, Country__c, Prospect_Record_Type__c, Postal_Code_Prefix__c, State_Province__c, Stratus_Product__c, Prospect_Type__c from Prospect_Assignment__c where (Active__c = true and Country__c includes (:tmpVar1) and Prospect_Type__c = :tmpVar2 and Stratus_Product__c = :tmpVar3)
09:48:48.304 (304107000)|SOQL_EXECUTE_END|[45]|Rows:2
09:48:48.304 (304237000)|SYSTEM_METHOD_ENTRY|[46]|LIST.iterator()
09:48:48.304 (304383000)|SYSTEM_METHOD_EXIT|[46]|LIST.iterator()
09:48:48.304 (304406000)|SYSTEM_METHOD_ENTRY|[46]|system.ListIterator.hasNext()
09:48:48.304 (304419000)|SYSTEM_METHOD_EXIT|[46]|system.ListIterator.hasNext()
09:48:48.304 (304431000)|SYSTEM_METHOD_ENTRY|[46]|system.ListIterator.next()
09:48:48.304 (304442000)|SYSTEM_METHOD_EXIT|[46]|system.ListIterator.next()
09:48:48.304 (304465000)|SYSTEM_METHOD_ENTRY|[47]|LIST.iterator()
09:48:48.304 (304559000)|SYSTEM_METHOD_EXIT|[47]|LIST.iterator()
09:48:48.304 (304578000)|SYSTEM_METHOD_ENTRY|[47]|system.ListIterator.hasNext()
09:48:48.304 (304589000)|SYSTEM_METHOD_EXIT|[47]|system.ListIterator.hasNext()
09:48:48.304 (304599000)|SYSTEM_METHOD_ENTRY|[47]|system.ListIterator.next()
09:48:48.304 (304624000)|SYSTEM_METHOD_EXIT|[47]|system.ListIterator.next()
09:48:48.304 (304731000)|SYSTEM_METHOD_ENTRY|[64]|String.valueOf(Object)
09:48:48.305 (305134000)|SYSTEM_METHOD_EXIT|[64]|String.valueOf(Object)
09:48:48.305 (305155000)|SYSTEM_METHOD_ENTRY|[64]|System.debug(ANY)
09:48:48.305 (305162000)|USER_DEBUG|[64]|DEBUG|----------------ld:Lead:{View_Terms_and_Conditions__c=_HL_ENCODED_/resource/1283538857000/DealRegistrationTermsandConditions_HL_Click to view_HL__Blank_HL_, RecordTypeId=01260000000Q32MAAS, LastModifiedByID=005600000018Vhm, LastTransferDate=2011-11-04 00:00:00, Stratus_Product_s__c=Avance, jigsaw_clean__Sync_Status_Indicator__c=_IM1_/resource/jigsaw_clean__unmatched_IM2_U_IM3_ Not Found, LastModifiedDate=2011-11-04 13:47:37, Prospect_Type__c=End User, LastName=adminnn, Days_Since_Last_Acitivity__c=0.0, DoNotCall=false, CreatedByID=005600000018Vhm, jigsaw_clean__Jigsaw_Sourced__c=0.0, jigsaw_clean__Sync_Status_Summary__c=Not Found, Decision_Time_Frame_Followup_date__c=2011-11-04 13:48:48, Hidden_Is_Converted_Opportunity__c=Yes, CurrencyIsoCode=USD, Sales_Region__c=AMER, jigsaw_clean__Automatic_Updates__c=Unlocked, Postal_Code_Prefix__c=901, HasOptedOutOfEmail=false, jigsaw_clean__Automatic_Updates_Disabled__c=false, Sales_Territory_US__c=SOUTH, Country=United States, jigsaw_clean__Fresher__c=N, Lead_Status_Grouping__c=Qualified, Last_Activity__c=2011-11-04 00:00:00, Lead_ID__c=003030, jigsaw_clean__Jigsaw_Managed__c=Unmanaged, Converted_Default_Stage__c=Opportunities Identified, CreatedById=005600000018VhmAAE, IsDeleted=false, Id=00QP0000002dhMRMAY, IsConverted=false, jigsaw_clean__Graveyard__c=false, HasOptedOutOfFax=false, OwnerId=005600000018VhmAAE, Do_Not_Send_Snail_Mail__c=false, Interest__c=false, IsUnreadByOwner=false, Active_Business_Plan__c=_IM1_/img/samples/color_red.gif_IM2_Yes_IM3__15_IM4_15_IM5_, jigsaw_clean__Duplicate__c=None Found, SystemModstamp=2011-11-04 13:47:37, FirstName=Serviceee, jigsaw_clean__Jigsaw_Managed_Backend__c=0.0, Lead_Status_Changed_Date__c=2011-10-31 00:00:00, Company=prospective partner 2, jigsaw_clean__CRM_Last_Modified__c=2011-11-04 13:47:37, jigsaw_clean__Silent_Update__c=false, LastModifiedById=005600000018VhmAAE, Title=title, Status=Marketing Qualified, Marketing_Permission_Granted__c=false, PostalCode=90121, State=AR, CreatedDate=2011-10-31 13:08:43, Lead_Score__c=0.0, I_agree_to_the_Terms_and_Conditions__c=false, Hidden_Is_Converted_Account__c=Yes, Lead_Marketability__c=2.5}
09:48:48.305 (305181000)|SYSTEM_METHOD_EXIT|[64]|System.debug(ANY)
09:48:48.305 (305256000)|SYSTEM_METHOD_ENTRY|[46]|system.ListIterator.hasNext()
09:48:48.305 (305270000)|SYSTEM_METHOD_EXIT|[46]|system.ListIterator.hasNext()
09:48:48.369 (305294000)|CUMULATIVE_LIMIT_USAGE
09:48:48.369|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 25 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

 

I can see all the field values before the SOQL, we can see the debug messages. 
But I could not iterate those ones in the for loop.
When testing with SOQl, I put one field and tested. 3 fields are iterated over for loop, they are Country, Stratus_Product__c and Prospect_Type__c.
But I would like to see 2 more fields like this, they are 'State' & 'Postal_Code_Prefix__c'.
They are coming infront of SOQL, but not Iterating through for loop and not updating Lead Owner.

I thouht, is there any problem with if conditions in for loop?

Any Help would be appreciated...

 



aebenaeben

One obvious issue I can see is in the parameter format for the INCLUDES clsuse. The values you are using the includes clause need to use a sem-colon (;) as a delimeter.

 

For example in the first FOR loop

for (Lead ld : Trigger.new) {

if(ld.Country != null && ld.Country != '')

lCountryStr += ld.Country + ';';

}

 

Hope this helps.