function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TackTack 

Duplication check & mass convert records in custom object to lead module

Dear Developers,

I would like to implement mass convert custom object to lead page on APEX.

As first, I wrote following program.
It is not able to show error message when you don't select any records from a list view.

>ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Please select one record at least');
<apex:page StandardController="CustomObj__c" extensions="CheckDupClass" recordSetVar="DupRecords" id="Page">
    <apex:form id="form">
        	<apex:pageBlock id="step1" title="Selected Records" mode="edit" >
      			<apex:pageBlockTable value="{!selectedItems}" var="m">
          			<apex:column value="{!m.Name}" />
          			<apex:column value="{!m.LastName__c}" />
          			<apex:column value="{!m.FirstName__c}" />
                    <apex:column value="{!m.EmailAddress__c}" />
      			</apex:pageBlockTable>
            	<apex:pageBlockButtons location="bottom">   
               		<apex:commandButton value="Execute"  action="{!Ex_CheckDupClass}"/>
               		<apex:commandButton value="Cancel" action="{!cancel}"/>
        		</apex:pageBlockButtons>
     		</apex:pageBlock>
    </apex:form>
</apex:page>
public with sharing class CheckDupClass {

	public List<CustomObj__c> selectedItems{get;set;}
	
    public CheckDupClass(ApexPages.StandardSetController controller) {
       	List<CustomObj__c> ubId = (List<CustomObj__c>)controller.getSelected();
       	this.selectedItems=[select Id, Name, LastName__c, FirstName__c, EmailAddress__c, OwnerId 
                            		  from CustomObj__c where Id IN :ubId ORDER BY Name ASC LIMIT 10];
        	if (selectedItems.size() <= 0)
			{
			 //Want to show error message when user didn't select any records and display the page.
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Please select one record at least');
        	}
    }
	
    //Insert records in lead module
    public void Ex_CheckDupClass(){
    	Map<String,CustomObj__c> targetEmailMap = new Map<String,CustomObj__c>();
    
        for(CustomObj__c selectedItem : selectedItems){
            if(selectedItem.EmailAddress__c != null){
                if(!targetEmailMap.containsKey(selectedItem.EmailAddress__c)){
                    targetEmailMap.put(selectedItem.EmailAddress__c,selectedItem);
                }else{
                    targetEmailMap.get(selectedItem.EmailAddress__c).addError('The email address exists in leads module')
                }
            }
        }
        
	    if(targetEmailMap.size() !=0){
        	List<Lead> leads = [SELECT id, email FROM Lead WHERE email in :targetEmailMap.keyset()];
        if (leads.size() > 0) {
            for(Lead lead: leads){
               targetEmailMap.get(lead.email).addError('The email address exists in leads module');
             }
        }else{
            //Execute insert operation when no duplication
            for (Integer i = 0; i < selectedItems.size(); i++){
            Lead lc1 = new Lead();
            		lc1.Company = selectedItems[i].Name;
            		lc1.LastName = selectedItems[i].LastName__c;
            		lc1.FirstName = selectedItems[i].FirstName__c;
            		lc1.email = selectedItems[i].EmailAddress__c;
            		lc1.Status = 'Open - Not Contacted';
            		lc1.OwnerId = selectedItems[i].OwnerId;
            	insert lc1;
        	}       
        }
    }
}
    
    
    public PageReference cancel() {
		String retUrl = Apexpages.currentPage().getParameters().get('retUrl');
		if (retUrl == '')
		{
			return new PageReference('/');
		}
		else
		{
			return new PageReference(retUrl);
		}	
	}      
}

In above code, you can insert all selected records if emails are not duplicated.
It means that you can not insert any records if there are the email address in lead module already.

I can not find the information I wanted in help page and past topic.
(I am so sorry my searching way is not good)
Could you please support me to resolve above issues?

Could you please let me know if you need more details?

Best Regards,
Tack 

Best Answer chosen by Tack
ShashForceShashForce
Hi Tack,

You should use the apex:messages component in your visualforce page. Here is some info:
http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_messages.htm
http://www.infallibletechie.com/2012/10/how-to-display-error-messages-in.html

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank

All Answers

ShashForceShashForce
Hi Tack,

You should use the apex:messages component in your visualforce page. Here is some info:
http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_messages.htm
http://www.infallibletechie.com/2012/10/how-to-display-error-messages-in.html

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
This was selected as the best answer
TackTack
Hi Shashank,

I can implement one of what I wanted!! Thank so much!
I didn't check visualforce page...

Regarding mass convert operation without duplication,
I still have some trouble...( I'll check the website you indicated. )
If possible, could you please give your valuable comment??

Best Regards,
Takumi

 
TackTack
Finally, I wrote below code in order to get list without non duplicated custom records between custom object and lead object.
Thank for supporting me, Shashank!
 
public List<Lead> ExistingLeads{get;set;}
    public List<CustomObject__c> NonDuplicatedItems{get;set;}

         ExistingLeads=[SELECT Email,FirstName, LastName, Company FROM Lead];
          //              
          Set<String> My_list= new Set<String>();
          List<Lead> lList = ExistingLeads;
            for (Lead l : lList) {
              My_list.add(l.Email);
            }
          //
         NonDuplicatedItems = [select id, name, LastName__c, FirstName__c, EmailAddress__c, OwnerId from CustomObject__c where EmailAddress__c NOT IN :My_list];
        
         List<CustomObject__c> hogeLists =  [select id, name, LastName__c, FirstName__c, EmailAddress__c, OwnerId from CustomObject__c where EmailAddress__c NOT IN :My_list];