+ Start a Discussion
RAAMRAAM 

Need help building a wizard similar to the mass add contacts to campaign wizard.

I have built a custom object similar to campaign say "A" and also built a junction object which links object A and contacts. Next i built a two page wizard to mass add selected contacts into the junction object. In the first page of wizard i select the name and status and in the second page i need to display a list of contacts with their name and a checkbox. So when i select a few checkboxes to choose contacts and click a button to add those contacts into my junction object.

I need a sample code of how to display a list of contacts with checkboxes and sample code of how to add the selected contacts into the juction object. Plz help me...
RAAMRAAM
Yes i followed that code and was able to build a table of contacts with checkboxes,my problem is i need to select a few contacts using the checkboxes and mass add them into my juction object when i click a button called "Add selected contacts" it would be very helpful if anyone could paste the sample apex code of how to iterate through the selected contacts(using a for loop) and insert them into my junction object.
SuzanyuSuzanyu
Just example
in your apex controller you need create several things
 
step 1 : create wrapper class 
------- I think you already made that in your code
 
public class ContactSelect {
            private Contact theContact;
            private boolean selected = false;
            public ContactSelect(Contact a) {
                theContact= a;
            }

            public Contact getTheContact() {
                return theContact;
            }
           
            public void setTheContact(Contact a) {
                theContact = a;
            }
           
                      
            public boolean getSelected() {
                return selected;
            }
           
            public void setSelected(boolean b) {
                this.selected = b;
            }   
        }
Step 2: create an array or list to hold your contact records (selected / unselected)
 
List<ContactSelect> contacts = new  List<ContactSelect>();
 
Step 3: a loop to populate your list above
 
public List<ContactSelect> getContacts () {
          
            contacts = new List<ContactSelect>();
            Contact[] original_contacts = [select Id, Name, ......... from Contac where ......];
            for(Contact a:original_contacts ) {
                ContactSelect cs = new ContactSelect(a);
                contacts.add(cs);                  
            }
       
        return contacts ;
    }
  
 
Step 4: in your VF page, there should some code like
<apex:form>
<apex:pageBlockTable value ="{!contacts}" var="c">
......
<apex:inputCheckbox value="{!c.selected}"  id="theCheckbox"></apex:inputCheckbox>
......
</apex:pageBlockTable >
</apex:commandButton action="{!createJunctionRec}" >
</apex:form>
Step 5: in your controller, you should put your action code here to insert your Junction object records
 
 public PageReference createJunctionRec() {
      Your_Junction_Object[] JOs = new   Your_Junction_Object[]{};
      for(ContactSelect c: contacts )
      {
             if(c.getSelected())
             {Contact con = c.getTheContact();
                            .........// your code to link contact information into the junction object
 
                                     // eg.  Your_Junction_Object JO = new Your_Junction_Object(Name= con.Name........);
                                     //        JOs.add(JO);
             }
      }
      insert JOs;
}
 
done
RAAMRAAM
Thank you very very much for the code,i will try it out :)
RAAMRAAM
I tried it using the code you provided but when i select a few contacts using checkbox and when i click "Add contacts" am getting an error

System.StringException: Invalid id: Stella Pavlova

Class.customController2.save: line 119, column 77
External entry point
This is my apex code...

public class customController2
{

    public class mcontact
    {
        contact c;
        boolean selected=false;
       
        public mcontact( contact con)
        {
            c=con;
        }
      
        public Contact getContact()
        {
            return c;
        }
       
        public void setContact(Contact con)
        {
            c=con;
        }
           
        public boolean getSelected()
        {
          return selected;
        }
     
      public void setSelected(boolean sb)
      {
          this.selected = sb;
      }
    }
   
    Contact contact;
    MailList__c maillist;
    MailList_History__c mailhistory;
 
  
   
   public Contact getContact()
   {
    if(contact == null)
    contact = new Contact();
    return contact;
   }
  
    public MailList__c getMaillist()
   {
    if(maillist == null)
    maillist = new MailList__c();
    return maillist;
   }
  
   public MailList_History__c getMailhistory()
   {
       if(mailhistory == null)
       mailhistory = new MailList_History__c();
       return mailhistory;
   }
  
 
   List<mContact> contacts = new  List<mContact>();

    public List<mContact> getContacts ()
    {
         
            contacts = new List<mContact>();
            Contact[] original_contacts = [select Name, Phone, MailingCity, Email from Contact];
            for(Contact a : original_contacts)
            {
                mContact cs = new mContact(a);
                contacts.add(cs);                 
            }
      
        return contacts ;
    }

  
    public PageReference step1()
   {
       return Page.Wizard1;
   }
  
   public PageReference step2()
   {
       return Page.Wizard2;
   }
  
  public PageReference save()
  {
     MailList_History__c [] mh = new MailList_History__c []{};
      for(mContact c: contacts )
      {
             if(c.getSelected())
             {Contact con = c.getContact();
                
                 MailList_History__c m = new MailList_History__c(Contact__c=con.name);
                 mh.add(m);              
             }
      }
      insert mh;
      return Page.Wizard2;
  }
}


Plz help...


Message Edited by RAAM on 12-08-2008 06:47 AM
SuzanyuSuzanyu
The error should take place here. Let me explain
-- MailList_History__c is your custom Object where there is a lookup relationship for Contact Object-- Contact__c. The problem is you can not use Contact Name to build up the connection between two objects, instead you need to use Contact Id here.
 
-- Contact[] original_contacts = [select Id, Name, Phone, MailingCity, Email from Contact];  -- you need add Id as well in query

-- correct expression should like

MailList_History__c m = new MailList_History__c(Contact__c=con.Id);

 
public PageReference save()
  {
     MailList_History__c [] mh = new MailList_History__c []{};
      for(mContact c: contacts )
      {
             if(c.getSelected())
             {Contact con = c.getContact();
                
                 MailList_History__c m = new MailList_History__c(Contact__c=con.name);
                 mh.add(m);              
             }
      }
      insert mh;
      return Page.Wizard2;
  }
RAAMRAAM
But there is no field such as Contact ID in a contacts object which i can use to build a lookup relationship all its shows is contact name and contact name itself is used to add campaigns to contacts and my junction object is similar to campaign.... sorry for so many questions am very new to force.com. If i do as u said in the previous step the error i mentioned is gone but no records are entered into the maillist history junction object when i click add contacts.


Message Edited by RAAM on 12-08-2008 04:23 PM

Message Edited by RAAM on 12-08-2008 05:39 PM

Message Edited by RAAM on 12-08-2008 05:40 PM
SuzanyuSuzanyu
Any relationship between two object/records is based on Id which is unique in Salesforce.com. You just see the name on page because that is prepopulated by salesforce.com via Id. In database there is an ID field actually. What you need to do is to show me your objects involved in this coding. Object details not clear for me. Please clarify them including the lookup fields api name on all objects so as to provide advice to you.
RAAMRAAM
Hi i have sent u a private message which containg login name and password for my DE plz login to it and see the MailList object and the MailList History junction object (similar to Campaign and Campaign History) and also the wizard i created which will open when u click Manage Members button in MailList detail page. Check my apex code "customController2" and see what happens when u select a a few contacts and try to insert them into the MailList History junction object using the wizard. Plz help...
RAAMRAAM
Thank you very much for your help i solved the issue so no need to login to the DE i sent, thanks again.
SuzanyuSuzanyu
Well done!!
Cheers
You r welcome
Any technical issue goes to suzan_yu@yahoo.com.au 
RAAMRAAM
Ok now that when i can select a few contacts and mass add them to my junction object,how to prevent from adding dulicates values ie contacts into my junction object??

this is my final working code

public class customController2
{

    public class mcontact
    {
        contact c;
        boolean selected=false;
       
        public mcontact( contact con)
        {
            c=con;
        }
      
        public Contact getContact()
        {
            return c;
        }
       
        public void setContact(Contact con)
        {
            c=con;
        }
           
        public boolean getSelected()
        {
          return selected;
        }
     
      public void setSelected(boolean sb)
      {
          this.selected = sb;
      }
    }
   
    Contact contact;
    MailList__c maillist;
    MailList_History__c mailhistory;
   // Integer i;
    String[] conts = new String[]{};
   
   public Contact getContact()
   {
    if(contact == null)
    contact = new Contact();
    return contact;
   }
  
    public MailList__c getMaillist()
   {
    if(maillist == null)
    maillist = new MailList__c();
    return maillist;
   }
  
   public MailList_History__c getMailhistory()
   {
       if(mailhistory == null)
       mailhistory = new MailList_History__c();
       return mailhistory;
   }
  
   //mContact [] Contacts;
   List<mContact> contacts = new  List<mContact>();
   //MailList_History__c [] mh = [select id,contact__c,MailList_Name__c from MailList_History__c];
   
    public List<mContact> getContacts ()
    {
         
            contacts = new List<mContact>();
            Contact[] original_contacts;
         
            original_contacts = [select Id, Name, Phone, MailingCity, Email from Contact];
     
            for(Contact a : original_contacts)
            {
                mContact cs = new mContact(a);
                contacts.add(cs);                 
            }
           
        return contacts ;
    }
        
    public PageReference step1()
   {
       return Page.Wizard1;
   }
  
   public PageReference step2()
   {
       return Page.Wizard2;
   }
  
  public PageReference save()
  {
          for(mContact c: contacts )
      {
             if(c.getSelected())
             {Contact con = c.getContact();
            
                 ID conid=con.ID;
                 MailList_History__c m = new MailList_History__c(Contact__c=conid,MailList_Name__c=mailhistory.MailList_Name__c,Status__c=mailhistory.Status__c);
                 insert m;
                 //i++;           
             }
      }
    
      return Page.Wizard2;
  }
}


Message Edited by RAAM on 12-10-2008 05:31 AM
SuzanyuSuzanyu

two ways

MailList_History__c is juction object?

1. you can create custom field on contact- checkbox. First time you select it and insert it into junction object, you set that True (mass update contact via list or array)

    Next  time you check each contact you selected in save() .if (selected and checkbox_field != true ) add into m array. finally insert the array with contact selected and not in your junction object.

 

2. in your save(), you can do query first to get all records in your junction object into a Map. Using Map method "containsKey" to test whether or not your selected contacts in the map. If yes, that mean duplicate. You don't need to add them into m array.  

 

RAAMRAAM
In the previous example i created a list of contacts in a visualforce page and was able to mass add them to my junction object but now i need to display all the records in my junction object ie MailList_History__c and display its three fields ie Contact__c, MailList_Name__c and Status__c. The problem when i create the list in a visualforce page same way as i did for contacts in previous example am getting ids in place of names as shown in the img below this is my new code


public class customController4
{

    public class mhistory
    {
        MailList_History__c mhis;
        boolean selected=false;
       
        public mhistory(MailList_History__c mcon)
        {
            mhis=mcon;
        }
      
        public MailList_History__c getMailList_History()
        {
            return mhis;
        }
       
        public void setMailList_History(MailList_History__c mcon)
        {
            mhis=mcon;
        }
           
        public boolean getSelected()
        {
          return selected;
        }
     
      public void setSelected(boolean sb)
      {
          this.selected = sb;
      }
    }
   
    Contact contact;
    MailList__c maillist;
    MailList_History__c mailhistory;
   // Integer i;
    String[] conts = new String[]{};
    //MailList__c mid = [select Name from MailList__c where id = : System.currentPageReference().getParameters().get('id')];
    String query = System.currentPageReference().getParameters().get('q');
   
    public string getQuery()
    {
        return query;
    }
   
   public Contact getContact()
   {
    if(contact == null)
    contact = new Contact();
    return contact;
   }
  
    public MailList__c getMaillist()
   {
    if(maillist == null)
    maillist = new MailList__c();
    return maillist;
   }
  
   public MailList_History__c getMailhistory()
   {
       if(mailhistory == null)
       mailhistory = new MailList_History__c();
       return mailhistory;
   }
  
   //mHistory [] historys;
  // String mname = mailhistory.MailList_Name__c;
   List<mHistory> historys = new  List<mHistory>();
   //MailList_History__c [] mh = [select id,contact__c,MailList_Name__c from MailList_History__c];
   
    public List<mHistory> getHistorys ()
    {
         
            historys = new List<mHistory>();
            MailList_History__c[] original_historys;
         
            original_historys = [select Id, Contact__c, MailList_Name__c, Status__c from MailList_History__c];
     
            for(MailList_History__c a : original_historys)
            {
                mHistory ms = new mHistory(a);
                historys.add(ms);                 
            }
           
        return historys ;
    }
   
   /* public List<MailList_History__c> getMstatus()
    {
        mstatus = new List<MailList_History__c>();
        original_mstatus = [select Id,status__c from MailList_History__c];
         */
    public PageReference step1()
   {
       return Page.Wizard1;
   }
  
   public PageReference step2()
   {
       return Page.Wizard4;
   }
  
 /* public PageReference save()
  {
          for(mContact c: contacts )
      {
             if(c.getSelected())
             {Contact con = c.getContact();
            
                 ID conid=con.ID;
                 MailList_History__c m = new MailList_History__c(Contact__c=conid,MailList_Name__c=mailhistory.MailList_Name__c,Status__c=mailhistory.Status__c);
                 insert m;
                 //i++;           
             }
      }
    
      return Page.Wizard2;
  }*/
}


<apex:page controller="customController4" tabstyle="MailList__c">
<apex:sectionHeader title="Select Contacts To Add"
subtitle="Step 2 of 2"/>
        <apex:panelGrid columns="1" id="theGrid" width="100%">
        <apex:form >
        <apex:pageBlock >
       
     
        <apex:dataTable value="{!Historys}" var="mcon" styleClass="list">
                <apex:column >
                        <apex:facet name="header">CheckBox
                        </apex:facet>
                        <apex:inputCheckbox value="{!mcon.selected}" />
                </apex:column>
                <apex:column >
                        <apex:facet name="header">Contact Name</apex:facet>
                        <apex:outputText value="{!mcon.MailList_History.Contact__c}"/>
                </apex:column>
                <apex:column >
                        <apex:facet name="header">MailList Name</apex:facet>
                        <apex:outputText value="{!mcon.MailList_History.MailList_Name__c}"/>
                </apex:column>
                <apex:column >
                        <apex:facet name="header">Status</apex:facet>
                        <apex:outputText value="{!mcon.MailList_History.Status__c}"/>
                </apex:column>
               
        </apex:dataTable>
        <apex:pageblockButtons location="both">
        <apex:commandButton Value="Previous">
        </apex:commandButton>
        <apex:commandButton value="Add Selected Contacts">
        </apex:commandButton>
        </apex:pageblockButtons>
      
       
       
        </apex:pageBlock>
        </apex:form>
        </apex:panelGrid>
</apex:page>






Message Edited by RAAM on 12-12-2008 03:07 AM

Message Edited by RAAM on 12-12-2008 10:25 AM
SuzanyuSuzanyu
try
 
public List<mHistory> getHistorys ()
    {
         
            historys = new List<mHistory>();
            MailList_History__c[] original_historys;
         
            original_historys = [select Id, Contact__c, Contact__r.Name, MailList_Name__c,MailList_Name__r.Name, Status__c from MailList_History__c];
     
            for(MailList_History__c a : original_historys)
            {
                mHistory ms = new mHistory(a);
                historys.add(ms);                 
            }
           
        return historys ;
    }
Contact__r.Name--- to get contact name
MailList_Name__r.Name -- to get mailist name, I am not sure what you want to display for mail list. If Name is not correct please change to other api name.
 
<apex:column >
                        <apex:facet name="header">Contact Name</apex:facet>
                        <apexutputText value="{!mcon.MailList_History.Contact__r.Name}"/>
                </apex:column>
RAAMRAAM
Hi thanks for ur previous help with my code it works fine, now i need to update the value of Status__c in the records already present in my MailList_History__c junction object. In my first visualforce wizard page i change the Status__c value and in the second visualforce page i select the MailList records to which the status needs to be updated. I already created the first visualforce page which lets us select Status__c and the second visualforce page which displays the list of records already present in MailList_History__c junction object, now i need to select a few records using checkbox and update their status value to the value selected in the first visualforce page but the status value is not being updated. Here is the code and screenshot, Plz help...

VF page1 code:-

<apex:page controller="customController4" tabstyle="MailList__c">
<apex:sectionHeader title="Update Members Status From a List of Contacts"
subtitle="Step 1 of 2"/>
<apex:form >
<apex:pageBlock title="MailList Information">
<apex:pageBlockSection >
<apex:panelGrid columns="2">
<apex:outputLabel value="MailListName: " for="maillistname">
</apex:outputLabel>
<apex:outputText style="font-style:bold"  value="{!query}">
</apex:outputText>
<apex:outputLabel value="Status" for="status">
</apex:outputLabel>
<apex:inputField id="status" value="{!mailhistory.Status__c}" required="true">
</apex:inputField>
</apex:panelGrid>
</apex:pageBlockSection>
<apex:pageBlockButtons location="both">
<apex:commandButton value="Next" action="{!step2}">
<apex:param name="q2" value="{!Mailhistory.Status__c}">
</apex:param>
</apex:commandButton>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

VF page1 image:-


VF page 2 code:-

<apex:page controller="customController4" tabstyle="MailList__c">
<apex:sectionHeader title="Select Contacts To Add"
subtitle="Step 2 of 2"/>
        <apex:panelGrid columns="1" id="theGrid" width="100%">
        <apex:form >
        <apex:pageBlock >
        <apex:pageBlockSection >
        <apex:outputLabel value="Update Status to... : " for="theStatus">
        </apex:outputLabel>
        <apex:outputText id="theStatus" value="{!query2}">
        </apex:outputText>
        </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock >
       
     
        <apex:dataTable value="{!Historys}" var="mcon" styleClass="list">
                <apex:column >
                        <apex:facet name="header">CheckBox
                        </apex:facet>
                        <apex:inputCheckbox value="{!mcon.selected}" />
                </apex:column>
                <apex:column >
                        <apex:facet name="header">Contact Name</apex:facet>
                        <apex:outputText value="{!mcon.MailList_History.Contact__r.Name}"/>
                </apex:column>
                <apex:column >
                        <apex:facet name="header">MailList Name</apex:facet>
                        <apex:outputText value="{!mcon.MailList_History.MailList_Name__r.Name}"/>
                </apex:column>
                <apex:column >
                        <apex:facet name="header">Status</apex:facet>
                        <apex:outputText value="{!mcon.MailList_History.Status__c}"/>
                </apex:column>
               
        </apex:dataTable>
        <apex:pageblockButtons location="both">
        <apex:commandButton Value="Previous">
        </apex:commandButton>
        <apex:commandButton value="Update Selected Contacts">
        </apex:commandButton>
        </apex:pageblockButtons>
      
       
       
        </apex:pageBlock>
        </apex:form>
        </apex:panelGrid>
</apex:page>


VF page2 image:-




Controller code:-

public class customController4
{

    public class mhistory
    {
        MailList_History__c mhis;
        boolean selected=false;
       
        public mhistory(MailList_History__c mcon)
        {
            mhis=mcon;
        }
      
        public MailList_History__c getMailList_History()
        {
            return mhis;
        }
       
        public void setMailList_History(MailList_History__c mcon)
        {
            mhis=mcon;
        }
           
        public boolean getSelected()
        {
          return selected;
        }
     
      public void setSelected(boolean sb)
      {
          this.selected = sb;
      }
    }
   
    Contact contact;
    MailList__c maillist;
    MailList_History__c mailhistory;
   // Integer i;
    String[] conts = new String[]{};
    //MailList__c mid = [select Name from MailList__c where id = : System.currentPageReference().getParameters().get('id')];
    String query = System.currentPageReference().getParameters().get('q');
    String query2 = System.currentPageReference().getParameters().get('q2');
   
    public string getQuery()
    {
        return query;
    }
   
    public string getQuery2()
    {
        return query2;
    }
   
   public Contact getContact()
   {
    if(contact == null)
    contact = new Contact();
    return contact;
   }
  
    public MailList__c getMaillist()
   {
    if(maillist == null)
    maillist = new MailList__c();
    return maillist;
   }
  
   public MailList_History__c getMailhistory()
   {
       if(mailhistory == null)
       mailhistory = new MailList_History__c();
       return mailhistory;
   }
  
   //mHistory [] historys;
  // String mname = mailhistory.MailList_Name__c;
   List<mHistory> historys = new  List<mHistory>();
   //MailList_History__c [] mh = [select id,contact__c,MailList_Name__c from MailList_History__c];
   
    public List<mHistory> getHistorys ()
    {
         
            historys = new List<mHistory>();
            MailList_History__c[] original_historys;
         
            original_historys = [select Id, Contact__c, Contact__r.Name, MailList_Name__r.Name, Status__c from MailList_History__c where MailList_Name__r.Name = :query];
     
            for(MailList_History__c a : original_historys)
            {
                mHistory ms = new mHistory(a);
                historys.add(ms);                 
            }
           
        return historys ;
    }
   
   /* public List<MailList_History__c> getMstatus()
    {
        mstatus = new List<MailList_History__c>();
        original_mstatus = [select Id,status__c from MailList_History__c];
         */
    public PageReference step1()
   {
       return Page.Wizard3;
   }
  
   public PageReference step2()
   {
       return Page.Wizard4;
   }
  
  public PageReference save()
  {
          for(mHistory m: historys )
      {
             if(m.getSelected())
             {MailList_History__c mcon = m.getMailList_History();
            
                // ID mconid=mcon.ID;
               // MailList_History__c mh = [select status__c from MailList_History__c];
                 mcon.Status__c = mailhistory.Status__c;
                 update mcon;
                 //i++;           
             }
      }
    
      return Page.Wizard2;
  }
}


SuzanyuSuzanyu
I did not see any SET method for status__c.
I show you a way that is used to generate status drop-down selection and get new value for status in your page 1.
 
-----put the code below into your controller.
String status='';  // that is for holding the selected value from the dropdown selection 
public void setStatus(String s) {
        this.status= s;
    }
    public String getStatus() {
        return status ;
    }
public List<SelectOption> getStatusPicklist() {
      List<SelectOption> optionList = new List<SelectOption>();

      Schema.DescribeFieldResult F;
      
      F = MailList_History__c.Status__c.getDescribe();// for the field name if not matches with your api name, change that. I just show example here
       
      List<Schema.PicklistEntry> P = F.getPicklistValues();
      for(Schema.PicklistEntry plValue : P) {
            optionList.add(new SelectOption(plValue.getValue(), plValue.getLabel())); 
      }      
      return optionList;
    }
   -----
 
put code below in your VF page
<apex:outputLabel for="status" >Status:</apex:outputLabel>

<apex:selectList id="status" value="{!status}" size="1" >
<apex:selectOptions value="{!StatusPicklist}"/>
</apex:selectList>
 
once you finish this part, I will tell you how to do other else if you still have issues.
RAAMRAAM
Adding values to the Status picklist is fine i need u to have look at update code plz ie change the value of the status say from "Sent" to "Received" for the selected records in the second page i have shown. I have added update code within my controller but its not working plz take a look at it and let me know whats wrong ASAP, thank u......

Also plz help me with this topic


Message Edited by RAAM on 12-16-2008 02:55 AM
SuzanyuSuzanyu

if you see my previous post and understand the purpose that I added status variable.

 

public PageReference save()
  {

      MailList_History__c[] ms = new MailList_History__c[]{};
          for(mHistory m: historys )
      {
             if(m.getSelected())
             {   MailList_History__c mcon = m.getMailList_History();
                
                 mcon.Status__c = status;

                 ms.add(mcon);
                 
                           
             }
      }
     update ms;
      return Page.Wizard2;
  }

RAAMRAAM
Thank u very much again for ur continued responses i got the idea of update code. Now am facing a new problem as u may remember initially i was displaying a list of contacts in a table with checkboxes in each row to select and insert into my junction object which ofcourse i did with the help of ur code ie using wrapper class, now am supposed to use <apex: enhancedlist> directly to display a table of contacts and from there i have select the contacts to insert into my junction object so plz tell how will i know in apex code which contacts are selected from the table so that i can perform a insert operation. Plz take look at this link

Link:-
http://community.salesforce.com/sforce/board/message?board.id=Visualforce&thread.id=7717


Message Edited by RAAM on 12-16-2008 08:26 PM

Message Edited by RAAM on 12-16-2008 10:15 PM