1

I have the following visualforce page:

<apex:pageBlock>
        <apex:pageBlockSection>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Convert Leads to Contacts" />
                <apex:commandButton value="Convert Leads to Contacts" action="{!convertLead}" onclick="if(!confirm('Do you really want to convert all Lead coincidences to contacts?\nRemember that this process will not create a new opporunity and/or new account. If you are completely sure about following this process, please press on OK button to execute the converting process. ')){return false};"/>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>

Controller method

public List<Lead> leadsToConvert;
public  void convertLead(){
    // https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_lead.htm
    //List<Lead> leadsToConvert;
    try
    {
        leadsToConvert = [Select id, FirstName, Lastname, Company FROM Lead];
        Set<String> accNames = new Set<String>();
        for(Lead l : leadsToConvert){
            accNames.add(l.Company);
        }

        //Create your map using Name as the key
        Map<String,Account> accMap = new Map<String,Account>();
        for(Account a : [Select Id, Name From Account Where Name = :accNames]){
            accMap.put(a.Name,a);
        }

        //Assemble List<Database.LeadConvert>
        List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
        for(Lead l : leadsToConvert)
        {
            //if(leadsToConvert.size() > 0)
            //{
                Database.LeadConvert lcc = new Database.LeadConvert();
                lcc.setLeadId(l.Id);
                //if there is an existing account with the same name
                //convert the lead into that account by grabbing the
                //id from the map above
                if(accMap.containsKey(l.Company))
                {
                    lcc.setDoNotCreateOpportunity(True);
                    lcc.setAccountId(accMap.get(l.Company).Id);
                    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true];
                    lcc.setConvertedStatus(convertStatus.MasterLabel);
                    Database.LeadConvertResult lcr = Database.convertLead(lcc);
                }
                //set other values
                leadConverts.add(lcc);
            //}
            //else
            //{
                //showMessage = true;
                //message = 'There are no more leads to convert.';
            //}

        }//End for
        showMessage = true;
        message = 'Your converting leads to contacts process has been finished successfully, please press OK button to follow with your processes. Thank you for using Lanshore Cleansing App.';

    }
    catch(Exception e)
    {
        showMessage = true;
        message = 'The following error has ocurred: ' + e.getMessage();
    }   
}

At pressing on button, it does nothing. Can someone tell me what I am doing wrong?

6
  • Did you open the JS console? Is there an error message there? Feb 11, 2016 at 18:22
  • Nope, there is no error. :( Feb 11, 2016 at 18:25
  • 1
    Hard to fix a problem via comment. Here's what I would do. Remove all JS. Ensure that your Apex code works. If it does, then concentrate on fixing your JS (start by moving it into its own function). If it doesn't, then concentrate on reviewing your APEX code. Once you figure out which one is failing, update your post and I can help more. Feb 11, 2016 at 18:32
  • Are you seeing the confirmation dialog from js at least? Feb 11, 2016 at 18:34
  • I will try that! Feb 11, 2016 at 18:34

1 Answer 1

1

Please try by using JS..

<apex:commandButton value="Convert Leads to Contacts"  onclick="confirmLeadConvert(event)"/>

<apex:actionFunction action="{!convertLead}" name="leadConvert"/>

function confirmLeadConvert(eve){
        if(window.confirm("Do you really want to convert all Lead coincidences to contacts?\nRemember that this process will not create a new opporunity and/or new account. If you are completely sure about following this process, please press on OK button to execute the converting process. ")){
                leadConvert();      
        }else{
                eve.preventDefault();
        }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.