+ Start a Discussion
phantom1982phantom1982 

Simple question

Hi Code Gurus,

 

Can someone plesae tell if we can use a List of standard objects with all its available methods like size() etc in a constructor?

 

 

this.email_c = parameters.get('PersonEmail');
        this.dup = [SELECT id from Account WHERE PersonEmail =: email_c];
        if(dup.size()>0){	
        	this.duplicatelead = true;
        }

 this code should set the duplicatelead to true everytime there's a duplicate record on the basis of email. But its not happening, the list size stays 0 always. Any idea why?

 

Ankit AroraAnkit Arora

Yes, we can use all methods. But make sure your query is returning  the result over which you are writing a loop. Use System.Debug() to check it.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

phantom1982phantom1982

Well query is returning 20 records but still the duplicate account is being created.

phantom1982phantom1982

Let me put all the details here. It's web to lead generation code, Leads are coming in from web pages and a standard controller class has all the code for lead generation, its below:

 

 

public with sharing class LeadCaptureController {
    
    private String ownerId {get; set;}
    private String recordTypeId {get; set;}

    private String email_c {get; set;}
    private Map<String, String> fieldValueMap {get; set;}
    private String fail_page {get; set;}
    private String success_page {get; set;} 
    private Boolean return_error {get; set;}
    private list <Account>dup;
    public sObject account {get; set;}
    
    private Boolean exceptionThrown = false;
    private Boolean duplicateLead = false;
    
    /**
     * Standard controller
     */ 
    public LeadCaptureController() {
        
        try {
        
        // Get Parameters from HTTP
        Map<String, String> parameters = ApexPages.currentPage().getParameters();

        // Remove the 'submit' parameter and save the success and error page
        parameters.remove('submit');
        this.fail_page = parameters.remove('fail_page');
        this.success_page = parameters.remove('success_page');
        
        if (parameters.remove('return_error').equals('true')) {
            return_error = true;
        } else {
            return_error = false;
        }

   		
        
        this.email_c = parameters.get('PersonEmail');
        this.dup = [SELECT id from Account WHERE PersonEmail =: this.email_c];
        if(dup.size()>0){	
        	this.duplicatelead = true;
        }
        
        
                
        if (parameters.get('record_type') != null) { 
            String recordTypeString = String.escapeSingleQuotes(parameters.remove('record_type'));
            String query_string = 'SELECT Id FROM RecordType WHERE SobjectType = \'Account\' AND Name = \'' + recordTypeString + '\' LIMIT 1';
            RecordType recordType = Database.query(query_string);
            this.recordTypeId = recordType.Id;
        } else {
            String recordTypeString = [Select pb__Value__c From pb__leanParameters__c Where Name = 'XMLWebservices_DefaultAccountRecordType'].pb__Value__c;
            String query_string = 'SELECT Id FROM RecordType WHERE SobjectType = \'Account\' AND Name = \'' + recordTypeString + '\' LIMIT 1';
            RecordType recordType = Database.query(query_string);
            this.recordTypeId = recordType.Id;
        }
        
        // Create new Map for reorganized parameters (with multi-picklist values)
        fieldValueMap = new Map<String, String>();
        
        // Search through parameters, clean and organize them
        for (String field : parameters.keySet()) {
            
            // Get current field value
            String fieldValue = parameters.get(field);
            
            if (field.contains('[]')) {
                fieldValue = null;
            }
            
            // Set map value and ignore multi-picklist array fields
            if (fieldValue != null) {
                this.fieldValueMap.put(field, fieldValue);
                System.debug('Get Parameter: ' + field + ': ' + fieldValue);
            }
        }
        } catch (System.Exception ex) {
            System.debug(System.Logginglevel.ERROR,'###### Web2Propspect Exception ' + ex);
            exceptionThrown = true;
        }
    }
    
  

    public PageReference createAccount() {
       
        if (exceptionThrown || duplicatelead) {
            PageReference p = new PageReference(this.fail_page);
            p.setRedirect(true);
            return p;
        }
        
        
        try {
        // Get Account token
        this.account = new Account();
            
        // further code for account creation..
        }
    }       
}

 

Now I have tried various scenarios but duplicate account is being created. Only when i disable the dup.size() check and set the value of duplicatelead flag to true everytime, it works fine and doesnt create account. but if the check is enabled, it still creates. The list is returning more than 20 records so its fine as far as the size is concerned.

 

Jon Mountjoy_Jon Mountjoy_

I would try a few things.  

a) Print out the value of this.email_c (ie. System.debug('[' + this.email__c + ']') either to log or elsewhere).  Note I suggest putting hte result in brackets. Perhaps you're getting whitespace that is causing the query to fail.

b) Similarly, dump the result of "SELECT id from Account WHERE PersonEmail =: this.email_c".  In fact, I'd also move the colon, so it reads "SELECT id from Account WHERE PersonEmail = :this.email_c"

 

That way you'll understand exactly why it's failing etc.

phantom1982phantom1982

Jon,

 

I have tried and everything is perfect with the code. The test gives the right results but when the lead is created from the web page, it generates duplicate. Below is the debug log and it has everything working fine i.e. returns to fail page when a duplicate record is found:

 

 

15:46:09.363|SOQL_EXECUTE_BEGIN|[104]|Aggregations:0|Select pb__Value__c From pb__leanParameters__c Where Name = 'XMLWebservices_LeadQualifierUsernameForNewAccounts2'
15:46:09.375|SOQL_EXECUTE_END|[104]|Rows:1
15:46:09.375|SOQL_EXECUTE_BEGIN|[105]|Aggregations:0|Select Id From User Where Username = :username
15:46:09.378|SOQL_EXECUTE_END|[105]|Rows:1
15:46:09.378|METHOD_ENTRY|[110]|MAP.get(ANY)
15:46:09.379|METHOD_EXIT|[110]|MAP.get(ANY)
15:46:09.379|SOQL_EXECUTE_BEGIN|[112]|Aggregations:0|SELECT id, FirstName from Account WHERE PersonEmail = :this.email_c
15:46:09.421|SOQL_EXECUTE_END|[112]|Rows:3
15:46:09.421|METHOD_ENTRY|[113]|System.debug(ANY)
15:46:09.421|USER_DEBUG|[113]|DEBUG|this.email_c: teasing@test.com
15:46:09.421|METHOD_EXIT|[113]|System.debug(ANY)
15:46:09.421|METHOD_ENTRY|[114]|System.debug(ANY)
15:46:09.421|METHOD_ENTRY|[114]|LIST.size()
15:46:09.421|METHOD_EXIT|[114]|LIST.size()
15:46:09.421|USER_DEBUG|[114]|DEBUG|dup.size: 3
15:46:09.421|METHOD_EXIT|[114]|System.debug(ANY)
15:46:09.421|METHOD_ENTRY|[115]|LIST.size()
15:46:09.421|METHOD_EXIT|[115]|LIST.size()
15:46:09.421|METHOD_ENTRY|[118]|System.debug(ANY)
15:46:09.421|USER_DEBUG|[118]|DEBUG|duplicatelead final value: true
15:46:09.421|METHOD_EXIT|[118]|System.debug(ANY)
15:46:09.421|METHOD_ENTRY|[122]|MAP.get(ANY)
15:46:09.421|METHOD_EXIT|[122]|MAP.get(ANY)
15:46:09.421|METHOD_ENTRY|[123]|String.escapeSingleQuotes(String)
15:46:09.421|METHOD_ENTRY|[123]|MAP.remove(ANY)
15:46:09.422|METHOD_EXIT|[123]|MAP.remove(ANY)
15:46:09.422|METHOD_EXIT|[123]|String.escapeSingleQuotes(String)
15:46:09.422|METHOD_ENTRY|[125]|Database.query(String)
15:46:09.422|SOQL_EXECUTE_BEGIN|[125]|Aggregations:0|SELECT Id FROM RecordType WHERE SobjectType = 'Account' AND Name = 'Buyer Prospect' LIMIT 1
15:46:09.428|SOQL_EXECUTE_END|[125]|Rows:1
15:46:09.428|METHOD_EXIT|[125]|Database.query(String)
15:46:09.428|METHOD_ENTRY|[138]|MAP.keySet()
15:46:09.428|METHOD_EXIT|[138]|MAP.keySet()
15:46:09.428|METHOD_ENTRY|[141]|MAP.get(ANY)
15:46:09.428|METHOD_EXIT|[141]|MAP.get(ANY)
15:46:09.428|METHOD_ENTRY|[143]|String.contains(String)
15:46:09.428|METHOD_EXIT|[143]|String.contains(String)
15:46:09.429|METHOD_ENTRY|[149]|MAP.put(ANY, ANY)
15:46:09.429|METHOD_EXIT|[149]|MAP.put(ANY, ANY)
15:46:09.429|METHOD_ENTRY|[150]|System.debug(ANY)
15:46:09.429|USER_DEBUG|[150]|DEBUG|Get Parameter: FirstName: Vorname
15:46:09.429|METHOD_EXIT|[150]|System.debug(ANY)
15:46:09.429|METHOD_ENTRY|[141]|MAP.get(ANY)
15:46:09.429|METHOD_EXIT|[141]|MAP.get(ANY)
15:46:09.429|METHOD_ENTRY|[143]|String.contains(String)
15:46:09.429|METHOD_EXIT|[143]|String.contains(String)
15:46:09.429|METHOD_ENTRY|[149]|MAP.put(ANY, ANY)
15:46:09.429|METHOD_EXIT|[149]|MAP.put(ANY, ANY)
15:46:09.429|METHOD_ENTRY|[150]|System.debug(ANY)
15:46:09.429|USER_DEBUG|[150]|DEBUG|Get Parameter: InterestBuyBudgetFrom__c: 10000
15:46:09.429|METHOD_EXIT|[150]|System.debug(ANY)
15:46:09.429|METHOD_ENTRY|[141]|MAP.get(ANY)
15:46:09.429|METHOD_EXIT|[141]|MAP.get(ANY)
15:46:09.429|METHOD_ENTRY|[143]|String.contains(String)
15:46:09.429|METHOD_EXIT|[143]|String.contains(String)
15:46:09.429|METHOD_ENTRY|[149]|MAP.put(ANY, ANY)
15:46:09.429|METHOD_EXIT|[149]|MAP.put(ANY, ANY)
15:46:09.429|METHOD_ENTRY|[150]|System.debug(ANY)
15:46:09.429|USER_DEBUG|[150]|DEBUG|Get Parameter: InterestPurchaseUnitType__c: Apartment; Office
15:46:09.429|METHOD_EXIT|[150]|System.debug(ANY)
15:46:09.429|METHOD_ENTRY|[141]|MAP.get(ANY)
15:46:09.429|METHOD_EXIT|[141]|MAP.get(ANY)
15:46:09.429|METHOD_ENTRY|[143]|String.contains(String)
15:46:09.430|METHOD_EXIT|[143]|String.contains(String)
15:46:09.430|METHOD_ENTRY|[141]|MAP.get(ANY)
15:46:09.430|METHOD_EXIT|[141]|MAP.get(ANY)
15:46:09.430|METHOD_ENTRY|[143]|String.contains(String)
15:46:09.430|METHOD_EXIT|[143]|String.contains(String)
15:46:09.430|METHOD_ENTRY|[149]|MAP.put(ANY, ANY)
15:46:09.430|METHOD_EXIT|[149]|MAP.put(ANY, ANY)
15:46:09.430|METHOD_ENTRY|[150]|System.debug(ANY)
15:46:09.430|USER_DEBUG|[150]|DEBUG|Get Parameter: LastName: Nachname
15:46:09.430|METHOD_EXIT|[150]|System.debug(ANY)
15:46:09.430|METHOD_ENTRY|[141]|MAP.get(ANY)
15:46:09.430|METHOD_EXIT|[141]|MAP.get(ANY)
15:46:09.430|METHOD_ENTRY|[143]|String.contains(String)
15:46:09.430|METHOD_EXIT|[143]|String.contains(String)
15:46:09.430|METHOD_ENTRY|[149]|MAP.put(ANY, ANY)
15:46:09.430|METHOD_EXIT|[149]|MAP.put(ANY, ANY)
15:46:09.430|METHOD_ENTRY|[150]|System.debug(ANY)
15:46:09.430|USER_DEBUG|[150]|DEBUG|Get Parameter: PersonEmail: teasing@test.com
15:46:09.430|METHOD_EXIT|[150]|System.debug(ANY)
15:46:09.430|METHOD_ENTRY|[141]|MAP.get(ANY)
15:46:09.430|METHOD_EXIT|[141]|MAP.get(ANY)
15:46:09.430|METHOD_ENTRY|[143]|String.contains(String)
15:46:09.430|METHOD_EXIT|[143]|String.contains(String)
15:46:09.430|METHOD_ENTRY|[149]|MAP.put(ANY, ANY)
15:46:09.430|METHOD_EXIT|[149]|MAP.put(ANY, ANY)
15:46:09.431|METHOD_ENTRY|[150]|System.debug(ANY)
15:46:09.431|USER_DEBUG|[150]|DEBUG|Get Parameter: PersonLeadSource: Web
15:46:09.431|METHOD_EXIT|[150]|System.debug(ANY)
15:46:09.431|METHOD_ENTRY|[141]|MAP.get(ANY)
15:46:09.431|METHOD_EXIT|[141]|MAP.get(ANY)
15:46:09.431|METHOD_ENTRY|[143]|String.contains(String)
15:46:09.431|METHOD_EXIT|[143]|String.contains(String)
15:46:09.431|METHOD_ENTRY|[149]|MAP.put(ANY, ANY)
15:46:09.431|METHOD_EXIT|[149]|MAP.put(ANY, ANY)
15:46:09.431|METHOD_ENTRY|[150]|System.debug(ANY)
15:46:09.431|USER_DEBUG|[150]|DEBUG|Get Parameter: Phone: 123456789
15:46:09.431|METHOD_EXIT|[150]|System.debug(ANY)
15:46:09.431|CONSTRUCTOR_EXIT|[44]|<init>()
15:46:09.431|METHOD_ENTRY|[45]|01pA0000000IsTo|XMLSitesLeadCaptureController.createAccount()
15:46:09.431|METHOD_ENTRY|[165]|System.PageReference.setRedirect(Boolean)
15:46:09.431|METHOD_EXIT|[165]|System.PageReference.setRedirect(Boolean)
15:46:09.431|METHOD_ENTRY|[166]|System.debug(ANY)
15:46:09.431|USER_DEBUG|[166]|DEBUG|returning early to fail page: http://leadcapture.local/lead_fail.php
15:46:09.431|METHOD_EXIT|[166]|System.debug(ANY)
15:46:09.431|METHOD_EXIT|[45]|XMLSitesLeadCaptureController.createAccount()
15:46:09.359|CUMULATIVE_LIMIT_USAGE

But when lead is generated with the Web page, it still creates a duplicate.