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
rajafurajafu 

Add Campaign Members custom functionality

Hi all,

 

I have three Objects Campaigns, Campaign Members and Contacts all are custom Objects.

 

Campaign member is junction object for Campaigns and Contacts.

 

Functionality - At first i will create a campaign record, under campaign record related list i have a visualforce button called Add Contacts so when i click that i i will be seeing all the contacts with check boxes. Next step is i will select some contacts and click process selected button so the selected contacts should add to the campaign members to the certain Campaign.

 

Similar to standard  Campain, Campaign Members functionality.

 

Error: In the below mention code im able to get all the contacts and saves to campaign Members (process method in class), but im not able to add the selected records to the certain Campaign. 

 

CODE:

 

Visualforce page:

 

<apex:page controller="PagingController">
<apex:form >
  <apex:sectionHeader title="Campaign Membership Manager" description="Add Contacts to Campaigns"/>
          <apex:pageblock >
      <apex:pageBlockButtons location="top">
        <apex:commandButton action="{!process}" value="Process Selected"/>
        <apex:commandButton action="{!cancel}" value="Cancel"/>
      </apex:pageBlockButtons>
      <apex:pageMessages />
 
      <apex:pageBlockSection title="Category Results -  Page #{!pageNumber}" columns="1">
        <apex:pageBlockTable value="{!categories}" var="c">
          <apex:column width="25px">
            <apex:inputCheckbox value="{!c.checked}"/>
          </apex:column>
          <apex:column value="{!c.Obj.Name}" headerValue="Name"/>
        </apex:pageBlockTable>
      </apex:pageBlockSection>
    </apex:pageBlock>
 
    <apex:panelGrid columns="4">
    <apex:commandLink action="{!first}">First</apex:commandlink>
    <apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
    <apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandlink>
    <apex:commandLink action="{!last}">Last</apex:commandlink>
    </apex:panelGrid>
   </apex:form>
</apex:page>

 Apex Class:

 

public with sharing class PagingController {

  
    List<CobjectsWrapper> cobj {get;set;}
    public Campaign__c camp{get;set;}
   // private List<Campaign_Member__c>  campaignMemberList { get; set; }  
    //private List<Campaign_Member__c>  pageCampaignMemberList;  
    private String  selectedCampaign;
    public boolean bAdd;
    public String  messageTitle             { get; set; }
    public String  messageDetail            { get; set; }
    public String  messageSeverity          { get; set; }
    transient public Boolean messageShow    { get; set; }
    // instantiate the StandardSetController from a query locator
    public ApexPages.StandardSetController con {
        get {
            if(con == null) {
                con = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, Name from Lead_Contact__c  Order By Name limit 100]));
                // sets the number of records in each page set
               system.debug('Controllerleadcontact'+con);
                con.setPageSize(2);
            }
            return con;
        }
        set;
    }
 
    // returns a list of wrapper objects for the sObjects in the current page set
    public List<CobjectsWrapper> getCategories() {
  //  if(cobj== null)
   // {
        cobj = new List<CobjectsWrapper>();
        for (Lead_Contact__c lobj : (List<Lead_Contact__c>)con.getRecords())
            cobj.add(new CobjectsWrapper(lobj));
           system.debug('LeadContactWrapper' + cobj);
    //    }'
        return cobj;
    
    }
    public List<SelectOption> getCampaigns(){
        
        List<SelectOption> options  = new List<SelectOption>();
        List<Campaign__c> cList        = new List<Campaign__c>();
        
        try {
            cList = [ SELECT Id, Name, Description__c FROM Campaign__c WHERE Status__c = 'Planned' AND Active__c = true ORDER BY Name ];
            for( Campaign__c  c: cList ){
                options.add( new SelectOption( c.Id, c.Name ) );
            }
            
            if( cList == null || cList.size() <= 0 ){
                // Show Info Message that no records have been found
                messageTitle    = 'No data';
                messageDetail   = 'No Campaign';
                messageSeverity = 'info';
                messageShow     = true;
                
                return null;    
            }else{
                return options;
            }        
        }catch( Exception ex ){
           // Show error Message with severity FATAL
           // messageTitle  = 'CMM_Error';
           // messageDetail     = ex.getMessage();
           //messageSeverity = 'fatal';
           //messageShow    = true;
           return null;
        } 
    }
    public String getSelectedCampaign(){
   
     return this.selectedCampaign;
    }
      public void setSelectedCampaign( String selectedCampaign ){
        this.selectedCampaign = selectedCampaign;
    }   
    // displays the selected items
    public PageReference process() {
    List<Campaign__c> camp1=[select Name,Id from Campaign__c  WHERE  Active__c = true];
    system.debug('Campaign'+camp1);
    List<Lead_Contact__c > selectedObjects = new List<Lead_Contact__c >();
        for (CobjectsWrapper cw : cobj) {
            if (cw.checked == true){
            selectedObjects.add(cw.Obj);
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'changes  saved sucessfully'));
                }
        }
        List<Campaign_Member__c> SavedDBO = [Select  Name,Lead_Contact__c,Campaign__c from Campaign_Member__c where Lead_Contact__c=:selectedObjects  ];
             //system.debug('Campaign'+SavedDBO.Campaign);
             system.debug('LeadContact '+SavedDBO);
             List<Campaign_Member__c> SEDBO = new List <Campaign_Member__c>();
            for(Lead_Contact__c Obj1 : selectedObjects) {
             bAdd = TRUE;

                
                for (Campaign_Member__c DBObj : SavedDBO) {
                    system.debug('CampaignMember%%%%%%%%%%%%%%%%'+DBObj);
                    if (SavedDBO.size() > 0)
                    {
                        // if the IDs match
                        if (Obj1.ID == DBObj.ID) {
                           // Do not save
                            bAdd = FALSE;
                        }
                    }
                    
                    
                  /*  for(Campaign__c cmp :camp1)
                    {
                    system.debug('CAMP&&&&&&&&&&&&&&&'+cmp);
                    if(DBObj.ID == cmp.ID)
                    {
                       system.debug('campaign'+DBObj.ID);
                       bAdd=FALSE;
                       }
                       }*/
                }

                if (bAdd) { 
                    // use SEDBO to add selected rows to object
                    // add one Campaign member row per LeadContact record if bAdd = TRUE
                    SEDBO.add(new Campaign_Member__c(Lead_Contact__c=Obj1.Id,Name=Obj1.Name));
                     
                  //  SavedDBO.add(DBObj);
                       system.debug('Contacts######################' +SEDBO);
                }

            }
            insert SEDBO;
        return null;
    }
  
    // indicates whether there are more records after the current page set.
    public Boolean hasNext {
        get {
            return con.getHasNext();
        }
        set;
    }
 
    // indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }
 
    // returns the page number of the current page set
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }
 
    // returns the first page of records
    public void first() {
        con.first();
    }
 
    // returns the last page of records
    public void last() {
        con.last();
    }
 
    // returns the previous page of records
    public void previous() {
        con.previous();
    }
 
    // returns the next page of records
    public void next() {
        con.next();
    }
 
    // returns the PageReference of the original page, if known, or the home page.
    public void cancel() {
        con.cancel();
    }
    
    public class CobjectsWrapper {
 
    public Boolean checked{ get; set; }
    public Lead_Contact__c Obj{get; set;}
 
    public CobjectsWrapper(){
        Obj = new Lead_Contact__c();
        checked = false;
    }
 
    public CobjectsWrapper(Lead_Contact__c lc){
        Obj = lc;
        checked = false;
    }
 
}
}

 Thanks in advance........