2

I have the following Visualforce page:

    <apex:page standardController="Lead" extensions="myExtension" showHeader="false" sidebar="false">
<apex:form >
<apex:commandLink action="{!URLFOR($Action.Lead.Convert, lead.id, [retURL=$CurrentPage.parameters.retURL, nooppti=1], true)}" value="Convert" target="_parent"/>
</apex:form>
</apex:page>

I need to write an Apex controller extension which sets a value (checkbox) before converting the lead. So I have now created a controller extension "myExtension". The checkbox on the lead object is called: lead_ok__c and it must be set to true, before converting the lead.

This is my controller so far:

        public class myExtension{
            public Lead currentLead {get;set;}

            public myExtension() {
            }

            public PageReference setCheckbox(){
                currentLead = [SELECT Id, lead_ok__c FROM Lead WHERE
                                       Id =: ApexPages.currentPage().getParameters().get('id')];

                //then I need to set the checkbox to true: 
                lead_ok__c = true;

                //then I need to update the record on Salesforce
                update lead with lead_ok__c where Id = CurrentLeadIdFromVFPage;
            }

            //then I need to invoke the lead convert action, ie 
            <apex:commandLink action="{!URLFOR($Action.Lead.Convert, lead.id, [retURL=$CurrentPage.parameters.retURL, nooppti=1], true)}" value="Convert" target="_parent"/>

         }

Tia.

2

The controller pattern to use here is a controller extension that has a constructor that takes an instance of the standard controller. There is also an Apex API for converting leads. Combining the two:

public with sharing class myExtension {

    private ApexPages.StandardController sc;

    public myExtension(ApexPages.StandardController sc) {
        this.sc = sc;
    }

    public PageReference convert() {

        update new Lead(Id = sc.getId(), lead_ok__c = true);

        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(sc.getId());

        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus
                WHERE IsConverted=true LIMIT 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);

        Database.LeadConvertResult lcr = Database.convertLead(lc);
        if (lcr.isSuccess()) {
            return new PageReference('/' + lcr.getContactId());
        } else {
            for (Database.Error e : lcr.getErrors()) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,
                        e.getMessage()));
            }
            return null;
        }
    }
}

And the Visualforce would change to:

<apex:commandButton action="{!convert}" value="Convert"/>
3
  • Thanks @Keith C. I am trying this now. So, on my Apex Page, instead of having that long URL action, I assume I will just write {!convert}, right? I now get a compile error for the invalid type in the line where it says: for (Error e : lcr.getErrors()) { Tia.
    – Lily
    Nov 24 '14 at 11:06
  • @Lily There were a couple of mistakes in the code - try the current version. and see the comment about the Visualforce too.
    – Keith C
    Nov 24 '14 at 11:12
  • Excellent. Thank you very much. This works very well now!!
    – Lily
    Nov 24 '14 at 11:16

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.