• Greg-inficience
  • NEWBIE
  • 25 Points
  • Member since 2009

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 5
    Replies

I created a simple view page that can be triggered from a contact list. The code is fully tested and uploaded as a package here (no password):

 

https://login.salesforce.com/?startURL=%2Fpackaging%2FinstallPackage.apexp%3Fp0%3D04t80000000YNFO

 

In order to use it, you need to add the Test list button in the package to the Contacts List View in the contact serach layout.

 

if you use a contact list such as "all contacts which last name starts with M" or "My contacts", it works fine.

 

When using a contact list with the list definition criteria Filter By Campaign (Optional): ,selecting any campaign, opening the view and pushing the button, the following error message appears:

 

SELECT id FROM Contact WHERE ((CampaignId = '70180000000F7kn')) ^ ERROR at Row:1:Column:32 No such column 'CampaignId' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

As if the system was trying to pass the campaign ID to the standardsetcontroller insteads of the resulting list of contacts.

 

Any  hint?

I am trying to write the test method for the following controller extension:

 

public class MassCreateLeadsFromContactsControllerExt {
    public string CampaignToAppendID;
    public list<contact> SelectedContacts;
    public boolean CampaignAlert = False;
    public boolean noContact = False;
    public boolean isContact = True;
    public boolean ProcessEnded = False;
    public integer ContactNumber =0;
    public integer ContactsProcessed = 0;

    public MassCreateLeadsFromContactsControllerExt(ApexPages.StandardSetController stdSetController) {
        list<contact> TheSelectedIDs;
        System.LoggingLevel level = LoggingLevel.FINEST;
//        system.debug ('Starting extension controller');
//        system.debug ('CampaignAlert: '+CampaignAlert);

        TheSelectedIDs = (list<Contact>)stdSetController.getselected();       
        if (TheSelectedIDs.size() > 0) {
            this.selectedcontacts = new list<Contact>();
            for (contact TheCurrentContact : TheSelectedIDs) {
                contact TheFullContact = ([SELECT id, name, firstname, lastname, title, accountid, email, phone, mobilephone, mailingcity, mailingcountry FROM contact where id = :TheCurrentContact.ID]);
                this.SelectedContacts.add(TheFullContact);
            }
//            system.debug ('SelectedContacts: '+this.SelectedContacts.size());
            this.contactnumber = this.SelectedContacts.size();
            this.noContact = False;
            this.isContact = True;
        } else {
            system.debug ('No Contact in list');
            this.ContactNumber = 0;
            this.noContact = True;
            this.isContact = False;
        }        
    }
    
    public pagereference CreateLeadsFromContacts() {
        Lead TheNewLead;
        Campaignmember TheNewCM;
        Campaignmember ContactCM;
        Contact TheFullContact;
        account TheAccount;
        
        if (CampaignToAppendID != '0') {
            Campaign TheCampaign = ([SELECT id, name FROM campaign where id = :CampaignToAppendID]);
//            system.debug ('Campaign to append: '+CampaignToAppendID+' - '+TheCampaign.name);
//            system.debug ('Nb of leads to create: '+SelectedContacts.size());

            for (contact TheCurrentContact : SelectedContacts) {
//                system.debug ('Contact to process: '+TheCurrentContact.ID);
                TheFullContact = ([SELECT id, firstname, lastname, salutation, title, accountid, email, phone, mobilephone, mailingstreet, mailingcity, mailingcountry, mailingpostalcode FROM contact where id = :TheCurrentContact.ID]);
                TheAccount = ([SELECT id, name, industry FROM account where id = :TheFullContact.accountid]);
                TheNewLead = new lead ();
                TheNewLead.firstname = TheFullContact.firstname;
                TheNewLead.lastname = TheFullContact.lastname;
                TheNewLead.title = TheFullContact.title;
                TheNewLead.Company = TheAccount.name;
                TheNewLead.email = TheFullContact.email;
                TheNewLead.phone = TheFullContact.phone;
                TheNewLead.mobilephone = TheFullContact.mobilephone;
                TheNewLead.industry = TheAccount.industry;
                TheNewLead.street = TheFullContact.mailingstreet;
                TheNewLead.city = TheFullContact.mailingcity;
                TheNewLead.country = TheFullContact.mailingcountry;
                TheNewLead.postalcode = TheFullContact.mailingpostalcode;
                TheNewLead.salutation = TheFullContact.salutation;
                
                try {
                    insert TheNewLead;
                    this.ContactsProcessed = this.ContactsProcessed +1;
                } catch (exception e) {
                }
//                system.debug ('New Lead insterted - ID: '+TheNewLead.ID);
                TheNewCM = new campaignMember();
                TheNewCM.leadid = TheNewLead.ID;
                TheNewCM.campaignid = TheCampaign.ID;
                try {
                    contactCM = ([SELECT contactid, status FROM campaignmember where campaignid = :TheCampaign.ID and contactid = :TheCurrentContact.id limit 1]);
                    if (contactCM != null) {
                        TheNewCM.status = contactCM.status;
                    } 
                } catch (exception e) {
//                    system.debug ('No campaign memeber status. default value will be used');
                }
                insert TheNewCM;
            }
//            system.debug('END OF PROCESSING');
            this.ProcessEnded = True;
            this.isContact = False;
            this.CampaignAlert = False;
            }
        else {
            this.CampaignAlert = True;
            system.debug('No campaign selected');
            }
        return null;
    }

    public List<SelectOption> getCampaignList() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('0','-- No Campaign --'));
        for (campaign c : [SELECT id, name FROM campaign where isactive = TRUE order by name]) {
            options.add(new SelectOption(c.id,c.name));    
        }
//        system.debug('getCampaignsToAppend - Campaigns To Append drop list: '+options.size());
        return options;
    }

    public integer getContactNumber() {
        if (this.ContactNumber == null) {
            this.ContactNumber = 0;
        }
//        system.debug ('getContactNumber - ContactNumber: '+this.ContactNumber);
        return this.ContactNumber;
    }

    public integer getContactsProcessed() {
        if (this.ContactsProcessed == null) {
            this.ContactsProcessed = 0;
        }
//        system.debug ('getContactsProcessed - ContactsProcessed: '+this.ContactsProcessed);
        return this.ContactsProcessed;
    }

    public list<contact> getSelectedContacts() {
        if (This.SelectedContacts == null) {
            This.SelectedContacts = new List<contact>();
//            system.debug ('getSelectedContacts - found SelectedContacts null');
        }
//        system.debug ('getSelectedContacts - TheContacts: '+this.SelectedContacts.size());
        return This.SelectedContacts;
    }

    public string getCampaignToAppendID() {
        if(this.CampaignToAppendID == null) {
            this.CampaignToAppendID = '0';
        }
//        system.debug('getCampaignToAppend - CampaignToAppendID: '+this.CampaignToAppendID);
        return this.CampaignToAppendID;
    }

    public void setCampaignToAppendID(string c) {
        this.CampaignToAppendID = c;
//        system.debug('setCampaignToAppendID - CampaignToAppendID: '+this.CampaignToAppendID);
        if (CampaignToAppendID != '0') {
            Campaign TheCampaign = ([SELECT id, name FROM campaign where id = :this.CampaignToAppendID]);
//            system.debug ('Campaign to append: '+this.CampaignToAppendID+' - '+TheCampaign.name);
            }
    }

    public boolean getProcessEnded() {
        if (this.ProcessEnded == null) {
            this.ProcessEnded = True;           
        }
        return this.ProcessEnded;
    }

    public boolean getnoContact() {
        if (this.noContact == null) {
            this.noContact = True;           
        }
        return this.noContact;
    }
    public boolean getisContact() {
        if (this.isContact == null) {
            this.isContact = False;
        }
        return this.isContact;
    }
    
    public boolean getCampaignAlert() {
        if (CampaignAlert == null) {
            CampaignAlert = False;
        }
        return CampaignAlert;
    }
}

 

This is being used by the following page:

 

<apex:page standardController="contact" extensions="MassCreateLeadsFromContactsControllerExt" recordSetVar="contacts">
<script>
function confirmCancel() {
var isCancel = confirm("Are you sure you wish to cancel?");
if (isCancel) return true;
return false;
}
</script>

<apex:sectionHeader title="Mass Create Leads from Contacts"></apex:sectionHeader>
<apex:form >
<apex:pageBlock mode="edit">
<apex:pageBlockButtons >
<apex:commandButton value="Create leads" action="{!CreateLeadsFromContacts}" id="CLFC"/>
<apex:commandButton value="Cancel" action="{!Cancel}"/>
</apex:pageBlockButtons>

<apex:pageBlockSection title="Lead creation parameters" collapsible="false" columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel for="CampaignToAppendID">Attach resulting leads to campaign:</apex:outputLabel>
<apex:selectlist value="{!CampaignToAppendID}" multiselect="false" size="1">
<apex:selectOptions value="{!CampaignList}"/>
</apex:selectlist>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem rendered="{!CampaignAlert}">
<apex:outputLabel ><h1>Please select a campaign</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem rendered="{!noContact}">
<apex:outputLabel ><h1>There is no contact to process</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem rendered="{!isContact}">
<apex:outputLabel ><h1>Nb contacts to process: {!ContactNumber}</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem rendered="{!ProcessEnded}">
<apex:outputLabel ><h1>{!ContactsProcessed} Contacts were processed</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection title="Contacts" columns="1" rendered="{!isContact}">
<apex:pageBlockSectionItem >
<apex:dataTable value="{!SelectedContacts}" var="TheContact" rowClasses="odd,even" styleClass="tableClass" width="100%">
<apex:column >
<apex:facet name="header">Name</apex:facet>
<apex:outputText value="{!TheContact.name}"/>
</apex:column>
<apex:column >
<apex:facet name="header">Title</apex:facet>
<apex:outputText value="{!TheContact.title}"/>
</apex:column>
<apex:column >
<apex:facet name="header">email</apex:facet>
<apex:outputText value="{!TheContact.email}"/>
</apex:column>
</apex:dataTable>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

The purpose is to select the contacts in a view and trigger a wizard that creates a lead for each contact and add the newly created leads to a campaign (which name is being prompted to the user).

 

I tried to write a unit test class that creates a list of contacts and instantiates the controller extension, passing this list of contacts but I have not been able to find out how I can do this.

 

Any hint?

 

My (useless, I am afraid) test code so far:

 

Public class TestLeadMassCreationWizard {

    public static testMethod void testLeadMassCreation() {
        System.Debug('Debugging...');
        System.Debug('Unit Test: lead Mass Creation and addition to campaign');
               
        list<contact> Clist = [select ID from contact where lastname like 'M%' limit 5];
        Campaign thecampaign = [select ID from Campaign where isactive = true limit 1];
       
        MassCreateLeadsFromContactsControllerExt TheController = new MassCreateLeadsFromContactsControllerExt<Clist>();
       
        string nextpage = ssc.CreateLeadsFromContacts().getURL();
    }
}

Hi all,

 

I created a VF page that is an extension controller for contacts. It is called from a contact view and the purpose is to enable people to create leads from a full list of contacts without having to go through an export/import.

 

If I create a contact view with criteria such as "Name starts with 'M'", and click the button, it works fine.

 

If I create a contact view using a "Filter By Campaign" criteria, I Get a error message: 

LastName, title FROM Contact WHERE ((CampaignId = '70180000000F7kn')) ^ ERROR at Row:1:Column:78 No such column 'CampaignId' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

 

Meaning that the controller does not receive the list of contacts but the criteria from the view. How Can I handle this?

 

Thanks in advance for your help.

 

VisualForce page Code is below:

 

<apex:page standardController="contact" extensions="MassCreateLeadsFromContactsControllerExt" recordSetVar="contacts">
<script>
function confirmCancel() {
var isCancel = confirm("Are you sure you wish to cancel?");
if (isCancel) return true;
return false;
}
</script>

<apex:sectionHeader title="Mass Create Leads from Contacts"></apex:sectionHeader>
<apex:form >
<apex:pageBlock mode="edit" id="block">
<apex:pageBlockButtons >
<apex:commandButton value="Create leads" action="{!CreateLeadsFromContacts}" immediate="true"/>
<apex:commandButton value="Cancel" action="{!Cancel}" onclick="return confirmCancel()" immediate="true"/>
</apex:pageBlockButtons>

<apex:pageBlockSection title="Creation parameters" collapsible="false" columns="2">
<apex:pageBlockSectionItem >
<apex:outputLabel for="CampaignToAppendID">Attach resulting leads to campaign:</apex:outputLabel>
<apex:selectlist value="{!CampaignToAppendID}" multiselect="false" size="1" required="True">
<apex:selectOptions value="{!CampaignsToAppend}"/>
</apex:selectlist>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel rendered="!{CampaignAlert}"><h1>Please enter a campaign</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false" columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel ><h1>Each contact in the list below will be copied to a lead.</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Contacts to be processed">
<apex:dataTable value="{!Contacts}" var="TheContact">
<apex:column title="Name" >
<apex:outputText value="{!TheContact.name}"/>
</apex:column>
<apex:column title="Title" >
<apex:outputText value="{!TheContact.Title}"/>
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:form>
</apex:page>

 

and controller code is here:

 

public class MassCreateLeadsFromContactsControllerExt {

string CampaignToAppendID;
public final list<contact> TheContacts;
boolean CampaignAlert = False;

public MassCreateLeadsFromContactsControllerExt(ApexPages.StandardSetController stdController) {
System.LoggingLevel level = LoggingLevel.FINEST;
system.debug ('Starting extension controller');
TheContacts = (list<Contact>:smileywink:stdController.getrecords();
system.debug ('TheContacts: '+Thecontacts.size());
system.debug ('CampaignAlert: '+CampaignAlert);
if (Thecontacts.size() > 0) {
system.debug ('First Contact: '+TheContacts[0].ID);
} else {
system.debug ('No Contact in list');
}
}

public List<SelectOption> getCampaignsToAppend() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('0','-- No Campaign --'));
for (campaign c : [SELECT id, name FROM campaign where isactive = TRUE order by name]) {
options.add(new SelectOption(c.id,c.name));
}
system.debug('getCampaignsToAppend - Campaigns To Append drop list: '+options.size());
return options;
}

public pagereference CreateLeadsFromContacts() {
if (CampaignToAppendID != '0') {
Campaign TheCampaign = ([SELECT id, name FROM campaign where id = :CampaignToAppendID]);
system.debug ('Campaign to append: '+CampaignToAppendID+' - '+TheCampaign.name);
system.debug ('Nb of leads to create: '+TheContacts.size());
for (contact TheCurrentContact : TheContacts ) {
system.debug ('Contact to process: '+TheCurrentContact.ID);
}
system.debug('END OF PROCESSING');
return page.CLFC_result;
}
else {
CampaignAlert = True;
system.debug('No campaign selected');
system.debug ('CampaignAlert: '+CampaignAlert);
return null;
}
}

public string getCampaignToAppendID() {
if(CampaignToAppendID == null) {
CampaignToAppendID = '0';
}
system.debug('getCampaignToAppend - CampaignToAppendID: '+CampaignToAppendID);
return CampaignToAppendID;
}

public void setCampaignToAppendID(string c) {
CampaignToAppendID = c;
system.debug('setCampaignToAppendID - CampaignToAppendID: '+CampaignToAppendID);
}

public boolean getCampaignAlert() {
if (CampaignAlert == null) {
CampaignAlert = False;
}
system.debug('getCampaignAlert - CampaignAlert: '+CampaignAlert);
return CampaignAlert;
}

public void setCampaignAlert(Boolean b) {
CampaignAlert = b;
system.debug('setCampaignAlert - CampaignAlert: '+CampaignAlert);
}
}

 

 Thanks for your help

 

I created a VisualForce Page if order to rapidly create leads from contacts. The purpose is for instance to enable to select all the contacts who attended a webinar and create leads for each of them in just a few clicks.
The page uses an extension controller to the contact standard controller.

In the extension controller, I created a variable called "CampaignToAppendID" which purpose is for the user to input the campaign to which the new leads have to be added.This variable is used in the VisualForce Page as a  droplist so gthat the users can select the campaign. Displaying and populating the droplist works OK. But the variable apparently does not change when the user selects different values for the droplist. The Debug log shows that the setCampaignToAppendID class setter never gets triggered. What I am missing?

 

VisualForce page Code is below:

 

<apex:page standardController="contact" extensions="MassCreateLeadsFromContactsControllerExt" recordSetVar="contacts">
<script>
function confirmCancel() {
var isCancel = confirm("Are you sure you wish to cancel?");
if (isCancel) return true;
return false;
}
</script>

<apex:sectionHeader title="Mass Create Leads from Contacts"></apex:sectionHeader>
<apex:form >
<apex:pageBlock mode="edit" id="block">
<apex:pageBlockButtons >
<apex:commandButton value="Create leads" action="{!CreateLeadsFromContacts}" immediate="true"/>
<apex:commandButton value="Cancel" action="{!Cancel}" onclick="return confirmCancel()" immediate="true"/>
</apex:pageBlockButtons>

<apex:pageBlockSection title="Creation parameters" collapsible="false" columns="2">
<apex:pageBlockSectionItem >
<apex:outputLabel for="CampaignToAppendID">Attach resulting leads to campaign:</apex:outputLabel>
<apex:selectlist value="{!CampaignToAppendID}" multiselect="false" size="1" required="True">
<apex:selectOptions value="{!CampaignsToAppend}"/>
</apex:selectlist>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel rendered="!{CampaignAlert}"><h1>Please enter a campaign</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false" columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel ><h1>Each contact in the list below will be copied to a lead.</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Contacts to be processed">
<apex:dataTable value="{!Contacts}" var="TheContact">
<apex:column title="Name" >
<apex:outputText value="{!TheContact.name}"/>
</apex:column>
<apex:column title="Title" >
<apex:outputText value="{!TheContact.Title}"/>
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:form>
</apex:page>

 

and controller code is here:

 

public class MassCreateLeadsFromContactsControllerExt {

string CampaignToAppendID;
public final list<contact> TheContacts;
boolean CampaignAlert = False;

public MassCreateLeadsFromContactsControllerExt(ApexPages.StandardSetController stdController) {
System.LoggingLevel level = LoggingLevel.FINEST;
system.debug ('Starting extension controller');
TheContacts = (list<Contact>)stdController.getrecords();
system.debug ('TheContacts: '+Thecontacts.size());
system.debug ('CampaignAlert: '+CampaignAlert);
if (Thecontacts.size() > 0) {
system.debug ('First Contact: '+TheContacts[0].ID);
} else {
system.debug ('No Contact in list');
}
}

public List<SelectOption> getCampaignsToAppend() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('0','-- No Campaign --'));
for (campaign c : [SELECT id, name FROM campaign where isactive = TRUE order by name]) {
options.add(new SelectOption(c.id,c.name));
}
system.debug('getCampaignsToAppend - Campaigns To Append drop list: '+options.size());
return options;
}

public pagereference CreateLeadsFromContacts() {
if (CampaignToAppendID != '0') {
Campaign TheCampaign = ([SELECT id, name FROM campaign where id = :CampaignToAppendID]);
system.debug ('Campaign to append: '+CampaignToAppendID+' - '+TheCampaign.name);
system.debug ('Nb of leads to create: '+TheContacts.size());
for (contact TheCurrentContact : TheContacts ) {
system.debug ('Contact to process: '+TheCurrentContact.ID);
}
system.debug('END OF PROCESSING');
return page.CLFC_result;
}
else {
CampaignAlert = True;
system.debug('No campaign selected');
system.debug ('CampaignAlert: '+CampaignAlert);
return null;
}
}

public string getCampaignToAppendID() {
if(CampaignToAppendID == null) {
CampaignToAppendID = '0';
}
system.debug('getCampaignToAppend - CampaignToAppendID: '+CampaignToAppendID);
return CampaignToAppendID;
}

public void setCampaignToAppendID(string c) {
CampaignToAppendID = c;
system.debug('setCampaignToAppendID - CampaignToAppendID: '+CampaignToAppendID);
}

public boolean getCampaignAlert() {
if (CampaignAlert == null) {
CampaignAlert = False;
}
system.debug('getCampaignAlert - CampaignAlert: '+CampaignAlert);
return CampaignAlert;
}

public void setCampaignAlert(Boolean b) {
CampaignAlert = b;
system.debug('setCampaignAlert - CampaignAlert: '+CampaignAlert);
}
}

 

 Thanks for your help

 

Message Edited by Greg-inficience on 06-15-2009 02:38 AM

I created a simple view page that can be triggered from a contact list. The code is fully tested and uploaded as a package here (no password):

 

https://login.salesforce.com/?startURL=%2Fpackaging%2FinstallPackage.apexp%3Fp0%3D04t80000000YNFO

 

In order to use it, you need to add the Test list button in the package to the Contacts List View in the contact serach layout.

 

if you use a contact list such as "all contacts which last name starts with M" or "My contacts", it works fine.

 

When using a contact list with the list definition criteria Filter By Campaign (Optional): ,selecting any campaign, opening the view and pushing the button, the following error message appears:

 

SELECT id FROM Contact WHERE ((CampaignId = '70180000000F7kn')) ^ ERROR at Row:1:Column:32 No such column 'CampaignId' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

As if the system was trying to pass the campaign ID to the standardsetcontroller insteads of the resulting list of contacts.

 

Any  hint?

I am trying to write the test method for the following controller extension:

 

public class MassCreateLeadsFromContactsControllerExt {
    public string CampaignToAppendID;
    public list<contact> SelectedContacts;
    public boolean CampaignAlert = False;
    public boolean noContact = False;
    public boolean isContact = True;
    public boolean ProcessEnded = False;
    public integer ContactNumber =0;
    public integer ContactsProcessed = 0;

    public MassCreateLeadsFromContactsControllerExt(ApexPages.StandardSetController stdSetController) {
        list<contact> TheSelectedIDs;
        System.LoggingLevel level = LoggingLevel.FINEST;
//        system.debug ('Starting extension controller');
//        system.debug ('CampaignAlert: '+CampaignAlert);

        TheSelectedIDs = (list<Contact>)stdSetController.getselected();       
        if (TheSelectedIDs.size() > 0) {
            this.selectedcontacts = new list<Contact>();
            for (contact TheCurrentContact : TheSelectedIDs) {
                contact TheFullContact = ([SELECT id, name, firstname, lastname, title, accountid, email, phone, mobilephone, mailingcity, mailingcountry FROM contact where id = :TheCurrentContact.ID]);
                this.SelectedContacts.add(TheFullContact);
            }
//            system.debug ('SelectedContacts: '+this.SelectedContacts.size());
            this.contactnumber = this.SelectedContacts.size();
            this.noContact = False;
            this.isContact = True;
        } else {
            system.debug ('No Contact in list');
            this.ContactNumber = 0;
            this.noContact = True;
            this.isContact = False;
        }        
    }
    
    public pagereference CreateLeadsFromContacts() {
        Lead TheNewLead;
        Campaignmember TheNewCM;
        Campaignmember ContactCM;
        Contact TheFullContact;
        account TheAccount;
        
        if (CampaignToAppendID != '0') {
            Campaign TheCampaign = ([SELECT id, name FROM campaign where id = :CampaignToAppendID]);
//            system.debug ('Campaign to append: '+CampaignToAppendID+' - '+TheCampaign.name);
//            system.debug ('Nb of leads to create: '+SelectedContacts.size());

            for (contact TheCurrentContact : SelectedContacts) {
//                system.debug ('Contact to process: '+TheCurrentContact.ID);
                TheFullContact = ([SELECT id, firstname, lastname, salutation, title, accountid, email, phone, mobilephone, mailingstreet, mailingcity, mailingcountry, mailingpostalcode FROM contact where id = :TheCurrentContact.ID]);
                TheAccount = ([SELECT id, name, industry FROM account where id = :TheFullContact.accountid]);
                TheNewLead = new lead ();
                TheNewLead.firstname = TheFullContact.firstname;
                TheNewLead.lastname = TheFullContact.lastname;
                TheNewLead.title = TheFullContact.title;
                TheNewLead.Company = TheAccount.name;
                TheNewLead.email = TheFullContact.email;
                TheNewLead.phone = TheFullContact.phone;
                TheNewLead.mobilephone = TheFullContact.mobilephone;
                TheNewLead.industry = TheAccount.industry;
                TheNewLead.street = TheFullContact.mailingstreet;
                TheNewLead.city = TheFullContact.mailingcity;
                TheNewLead.country = TheFullContact.mailingcountry;
                TheNewLead.postalcode = TheFullContact.mailingpostalcode;
                TheNewLead.salutation = TheFullContact.salutation;
                
                try {
                    insert TheNewLead;
                    this.ContactsProcessed = this.ContactsProcessed +1;
                } catch (exception e) {
                }
//                system.debug ('New Lead insterted - ID: '+TheNewLead.ID);
                TheNewCM = new campaignMember();
                TheNewCM.leadid = TheNewLead.ID;
                TheNewCM.campaignid = TheCampaign.ID;
                try {
                    contactCM = ([SELECT contactid, status FROM campaignmember where campaignid = :TheCampaign.ID and contactid = :TheCurrentContact.id limit 1]);
                    if (contactCM != null) {
                        TheNewCM.status = contactCM.status;
                    } 
                } catch (exception e) {
//                    system.debug ('No campaign memeber status. default value will be used');
                }
                insert TheNewCM;
            }
//            system.debug('END OF PROCESSING');
            this.ProcessEnded = True;
            this.isContact = False;
            this.CampaignAlert = False;
            }
        else {
            this.CampaignAlert = True;
            system.debug('No campaign selected');
            }
        return null;
    }

    public List<SelectOption> getCampaignList() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('0','-- No Campaign --'));
        for (campaign c : [SELECT id, name FROM campaign where isactive = TRUE order by name]) {
            options.add(new SelectOption(c.id,c.name));    
        }
//        system.debug('getCampaignsToAppend - Campaigns To Append drop list: '+options.size());
        return options;
    }

    public integer getContactNumber() {
        if (this.ContactNumber == null) {
            this.ContactNumber = 0;
        }
//        system.debug ('getContactNumber - ContactNumber: '+this.ContactNumber);
        return this.ContactNumber;
    }

    public integer getContactsProcessed() {
        if (this.ContactsProcessed == null) {
            this.ContactsProcessed = 0;
        }
//        system.debug ('getContactsProcessed - ContactsProcessed: '+this.ContactsProcessed);
        return this.ContactsProcessed;
    }

    public list<contact> getSelectedContacts() {
        if (This.SelectedContacts == null) {
            This.SelectedContacts = new List<contact>();
//            system.debug ('getSelectedContacts - found SelectedContacts null');
        }
//        system.debug ('getSelectedContacts - TheContacts: '+this.SelectedContacts.size());
        return This.SelectedContacts;
    }

    public string getCampaignToAppendID() {
        if(this.CampaignToAppendID == null) {
            this.CampaignToAppendID = '0';
        }
//        system.debug('getCampaignToAppend - CampaignToAppendID: '+this.CampaignToAppendID);
        return this.CampaignToAppendID;
    }

    public void setCampaignToAppendID(string c) {
        this.CampaignToAppendID = c;
//        system.debug('setCampaignToAppendID - CampaignToAppendID: '+this.CampaignToAppendID);
        if (CampaignToAppendID != '0') {
            Campaign TheCampaign = ([SELECT id, name FROM campaign where id = :this.CampaignToAppendID]);
//            system.debug ('Campaign to append: '+this.CampaignToAppendID+' - '+TheCampaign.name);
            }
    }

    public boolean getProcessEnded() {
        if (this.ProcessEnded == null) {
            this.ProcessEnded = True;           
        }
        return this.ProcessEnded;
    }

    public boolean getnoContact() {
        if (this.noContact == null) {
            this.noContact = True;           
        }
        return this.noContact;
    }
    public boolean getisContact() {
        if (this.isContact == null) {
            this.isContact = False;
        }
        return this.isContact;
    }
    
    public boolean getCampaignAlert() {
        if (CampaignAlert == null) {
            CampaignAlert = False;
        }
        return CampaignAlert;
    }
}

 

This is being used by the following page:

 

<apex:page standardController="contact" extensions="MassCreateLeadsFromContactsControllerExt" recordSetVar="contacts">
<script>
function confirmCancel() {
var isCancel = confirm("Are you sure you wish to cancel?");
if (isCancel) return true;
return false;
}
</script>

<apex:sectionHeader title="Mass Create Leads from Contacts"></apex:sectionHeader>
<apex:form >
<apex:pageBlock mode="edit">
<apex:pageBlockButtons >
<apex:commandButton value="Create leads" action="{!CreateLeadsFromContacts}" id="CLFC"/>
<apex:commandButton value="Cancel" action="{!Cancel}"/>
</apex:pageBlockButtons>

<apex:pageBlockSection title="Lead creation parameters" collapsible="false" columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel for="CampaignToAppendID">Attach resulting leads to campaign:</apex:outputLabel>
<apex:selectlist value="{!CampaignToAppendID}" multiselect="false" size="1">
<apex:selectOptions value="{!CampaignList}"/>
</apex:selectlist>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem rendered="{!CampaignAlert}">
<apex:outputLabel ><h1>Please select a campaign</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem rendered="{!noContact}">
<apex:outputLabel ><h1>There is no contact to process</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem rendered="{!isContact}">
<apex:outputLabel ><h1>Nb contacts to process: {!ContactNumber}</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem rendered="{!ProcessEnded}">
<apex:outputLabel ><h1>{!ContactsProcessed} Contacts were processed</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection title="Contacts" columns="1" rendered="{!isContact}">
<apex:pageBlockSectionItem >
<apex:dataTable value="{!SelectedContacts}" var="TheContact" rowClasses="odd,even" styleClass="tableClass" width="100%">
<apex:column >
<apex:facet name="header">Name</apex:facet>
<apex:outputText value="{!TheContact.name}"/>
</apex:column>
<apex:column >
<apex:facet name="header">Title</apex:facet>
<apex:outputText value="{!TheContact.title}"/>
</apex:column>
<apex:column >
<apex:facet name="header">email</apex:facet>
<apex:outputText value="{!TheContact.email}"/>
</apex:column>
</apex:dataTable>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

The purpose is to select the contacts in a view and trigger a wizard that creates a lead for each contact and add the newly created leads to a campaign (which name is being prompted to the user).

 

I tried to write a unit test class that creates a list of contacts and instantiates the controller extension, passing this list of contacts but I have not been able to find out how I can do this.

 

Any hint?

 

My (useless, I am afraid) test code so far:

 

Public class TestLeadMassCreationWizard {

    public static testMethod void testLeadMassCreation() {
        System.Debug('Debugging...');
        System.Debug('Unit Test: lead Mass Creation and addition to campaign');
               
        list<contact> Clist = [select ID from contact where lastname like 'M%' limit 5];
        Campaign thecampaign = [select ID from Campaign where isactive = true limit 1];
       
        MassCreateLeadsFromContactsControllerExt TheController = new MassCreateLeadsFromContactsControllerExt<Clist>();
       
        string nextpage = ssc.CreateLeadsFromContacts().getURL();
    }
}

I created a VisualForce Page if order to rapidly create leads from contacts. The purpose is for instance to enable to select all the contacts who attended a webinar and create leads for each of them in just a few clicks.
The page uses an extension controller to the contact standard controller.

In the extension controller, I created a variable called "CampaignToAppendID" which purpose is for the user to input the campaign to which the new leads have to be added.This variable is used in the VisualForce Page as a  droplist so gthat the users can select the campaign. Displaying and populating the droplist works OK. But the variable apparently does not change when the user selects different values for the droplist. The Debug log shows that the setCampaignToAppendID class setter never gets triggered. What I am missing?

 

VisualForce page Code is below:

 

<apex:page standardController="contact" extensions="MassCreateLeadsFromContactsControllerExt" recordSetVar="contacts">
<script>
function confirmCancel() {
var isCancel = confirm("Are you sure you wish to cancel?");
if (isCancel) return true;
return false;
}
</script>

<apex:sectionHeader title="Mass Create Leads from Contacts"></apex:sectionHeader>
<apex:form >
<apex:pageBlock mode="edit" id="block">
<apex:pageBlockButtons >
<apex:commandButton value="Create leads" action="{!CreateLeadsFromContacts}" immediate="true"/>
<apex:commandButton value="Cancel" action="{!Cancel}" onclick="return confirmCancel()" immediate="true"/>
</apex:pageBlockButtons>

<apex:pageBlockSection title="Creation parameters" collapsible="false" columns="2">
<apex:pageBlockSectionItem >
<apex:outputLabel for="CampaignToAppendID">Attach resulting leads to campaign:</apex:outputLabel>
<apex:selectlist value="{!CampaignToAppendID}" multiselect="false" size="1" required="True">
<apex:selectOptions value="{!CampaignsToAppend}"/>
</apex:selectlist>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel rendered="!{CampaignAlert}"><h1>Please enter a campaign</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false" columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel ><h1>Each contact in the list below will be copied to a lead.</h1></apex:outputLabel>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Contacts to be processed">
<apex:dataTable value="{!Contacts}" var="TheContact">
<apex:column title="Name" >
<apex:outputText value="{!TheContact.name}"/>
</apex:column>
<apex:column title="Title" >
<apex:outputText value="{!TheContact.Title}"/>
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:form>
</apex:page>

 

and controller code is here:

 

public class MassCreateLeadsFromContactsControllerExt {

string CampaignToAppendID;
public final list<contact> TheContacts;
boolean CampaignAlert = False;

public MassCreateLeadsFromContactsControllerExt(ApexPages.StandardSetController stdController) {
System.LoggingLevel level = LoggingLevel.FINEST;
system.debug ('Starting extension controller');
TheContacts = (list<Contact>)stdController.getrecords();
system.debug ('TheContacts: '+Thecontacts.size());
system.debug ('CampaignAlert: '+CampaignAlert);
if (Thecontacts.size() > 0) {
system.debug ('First Contact: '+TheContacts[0].ID);
} else {
system.debug ('No Contact in list');
}
}

public List<SelectOption> getCampaignsToAppend() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('0','-- No Campaign --'));
for (campaign c : [SELECT id, name FROM campaign where isactive = TRUE order by name]) {
options.add(new SelectOption(c.id,c.name));
}
system.debug('getCampaignsToAppend - Campaigns To Append drop list: '+options.size());
return options;
}

public pagereference CreateLeadsFromContacts() {
if (CampaignToAppendID != '0') {
Campaign TheCampaign = ([SELECT id, name FROM campaign where id = :CampaignToAppendID]);
system.debug ('Campaign to append: '+CampaignToAppendID+' - '+TheCampaign.name);
system.debug ('Nb of leads to create: '+TheContacts.size());
for (contact TheCurrentContact : TheContacts ) {
system.debug ('Contact to process: '+TheCurrentContact.ID);
}
system.debug('END OF PROCESSING');
return page.CLFC_result;
}
else {
CampaignAlert = True;
system.debug('No campaign selected');
system.debug ('CampaignAlert: '+CampaignAlert);
return null;
}
}

public string getCampaignToAppendID() {
if(CampaignToAppendID == null) {
CampaignToAppendID = '0';
}
system.debug('getCampaignToAppend - CampaignToAppendID: '+CampaignToAppendID);
return CampaignToAppendID;
}

public void setCampaignToAppendID(string c) {
CampaignToAppendID = c;
system.debug('setCampaignToAppendID - CampaignToAppendID: '+CampaignToAppendID);
}

public boolean getCampaignAlert() {
if (CampaignAlert == null) {
CampaignAlert = False;
}
system.debug('getCampaignAlert - CampaignAlert: '+CampaignAlert);
return CampaignAlert;
}

public void setCampaignAlert(Boolean b) {
CampaignAlert = b;
system.debug('setCampaignAlert - CampaignAlert: '+CampaignAlert);
}
}

 

 Thanks for your help

 

Message Edited by Greg-inficience on 06-15-2009 02:38 AM
Hi, I'm trying to put together a month over month and year over year comparison report.  I have the formula fields in which to achieve this however  I am encountering a syntax problem with CloseData (see below).  Sf keeps telling me that there is no such field as CloseDate.  However Close Date is a standard field in Opportunities.

Any help would be appreciated, thanks.

CASE( MONTH ( {!CloseDate} ) , 1, "01 - January", 2, "02 - February", 3, "03 - March", 4, "04 - April", 5, "05 - May", 6, "06 - June", 7, "07 - July", 8, "08 - August", 9, "09 - September", 10, "10 - October", 11, "11 - November", "12 - December")

David
  • April 13, 2007
  • Like
  • 0