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
ckellieckellie 

Custom Save Button not saving the amended records

I have a visualforce page that renders a list of records based on three variables the user sets. The user will then change the status for the intended records and clck the button "Update All." The page will refresh and return the status to the original status. The record page still shows the originnal status. Here is my save page reference:

 

 public PageReference save() {
        try {
            List<CampaignMember> changedCMList=new List<CampaignMember>();
            for ( CampaignMember cm: CampaignMember) {
                CampaignMember cm2=origCMMap.get(cm.Id);
                if ( cm2 != null &&
                    (cm2.Status != selectedsval ||
                     cm2.Log_A_Call_Notes__c != cm.Log_A_Call_Notes__c) ) {
                     changedCMList.add(cm);
                }
            }
            update changedCMList;
        } catch(DmlException ex){
            ApexPages.addMessages(ex);
        }
        return null;
    }

  The complete class:

 

public with sharing class CampaignListOwner13{

    public CampaignListOwner13() {

    }

        List<SelectOption> selectedOwnerList = new List<SelectOption>();
        Map<id, String> mappingval = new Map<id, String>();
        Map<String, String> mappingcval = new Map<String, String>();
        Map<String, String> mappingsval = new Map<String, String>();
        public Map<String, String> localCStatusMap;
        public String selectedval { get;  set;}
        public String selectedcval { get;  set;}
        public String selectedsval { get;  set;}
        public string statusval { get; set;}
        public string statusUpdate   {get; set;}
        public string campaignStatus {get; set;}
 
    public CampaignListOwner13(ApexPages.StandardController controller) {   }
    public String getCampaign() {
        return this.Campaign;
    }
    public String getCampaignStatus() {
        return this.CampaignStatus;
    }    
    public string s;
    public String campaign;
    public transient string               Campaignid{ get; set;}
    private transient CampaignMember[]    members; //the members for that campaign
    public transient String               owner; //owner ID
    public string getOwner() {
        return this.owner;
    }    
    public void setOwner(String s) {
 this.Owner = s; }
  public List<CampaignMember> CampaignMember { get;  set;}
     public List<SelectOption> getselectedOwner(){
         List<SelectOption> optionList = new List<SelectOption>();
            optionList.add(new SelectOption( '1', 'SELECT' ));
            for(User u:[select id,name from user where id in (select ownername__c from campaignmember) order by name]) {
            optionList.add(new SelectOption(u.id, u.name));
            mappingval.put(u.id, u.name);
        }
            return optionList;
        }      
    public List <SelectOption> getcampaignlists(){
system.debug('SFDC TEST ************ OWNER' + Owner);
system.debug('SFDC TEST1 *************' + selectedVal);

            AggregateResult[] groupedResults = [select Campaignid , Campaign_Name__c from campaignmember where 
                        ownername__c=:selectedval group by Campaignid, Campaign_Name__c ];
        List <SelectOption> optionList = new list<SelectOption>();
        optionList.add(new SelectOption( '1', 'SELECT' ));
        system.debug('SFDC TEST2 *************' + selectedVal);
        if(selectedcval!= null){
    for(Aggregateresult ar :groupedResults) {
            optionList.add(new SelectOption(String.valueof( ar.get('Campaign_Name__c')), String.valueof(ar.get('Campaign_Name__c'))));
            String aID = (string)ar.get('Campaign_Name__c');
            mappingcval.put(aID, aID);
            }
        }
        return optionList;
    }
    public List <SelectOption> getstatuslist(){
system.debug('SFDC TEST ************ OWNER' + Owner);
system.debug('SFDC TEST3 *************' + selectedVal);
            AggregateResult[] groupedResults = [select Status from campaignmember where 
                        ownername__c=:selectedval and Campaign_Name__c=:selectedcval group by Status ];
        List <SelectOption> optionList = new list<SelectOption>();
        optionList.add(new SelectOption( '1', 'SELECT' ));
        system.debug('SFDC TEST4 *************' + selectedVal);
        
        system.debug('SFDC TESTold *************' + selectedVal);
        
        if(selectedval!= null){
    for(Aggregateresult ar :groupedResults) {
            optionList.add(new SelectOption(String.valueof( ar.get('Status')), String.valueof(ar.get('Status'))));
            String sID = (string)ar.get('Status');
            mappingsval.put(sID, sID);
            }
        }
        return optionList;
    }
    public List<SelectOption> getcStatus(){
         List<SelectOption> options = new List<SelectOption>();
             options.add(new SelectOption(campaignmember[0].status, campaignmember[0].status));
             for(CampaignMemberStatus ccm:[select id, Label from CampaignMemberStatus where  Campaignid in 
                 (select campaignid from CampaignMember where Campaign_Name__c =:selectedcval) and Label != :statusupdate]){
                 options.add(new SelectOption(ccm.id, ccm.label));
             }
         return options;
        }


    public PageReference campaignRefresh() {       
              system.debug('$$$$$:'+selectedcval);
              system.debug('SFDC TEST5 *************' + selectedVal);
              system.debug(mappingval.get(selectedval));
              system.debug('SFDC TEST6 *************' + selectedVal);
               try {
                 if( selectedval != '1'){
                   
                    CampaignMember = [select Id, owner_Name__c, Campaign_Name__c, ContactId, LeadId, 
                    Status, Lead.Lead_Salutation_Name__c, Country__c, CampaignId, Campaign.Name, Company__c,
                    Lead.Id, Lead.Salutation, Lead.Title, Lead.Name, Lead.Company, Lead.Phone,
                    Lead.Street, Lead.State, Lead.PostalCode, Lead.Country, Lead.City, Lead.Fax, 
                    Lead.Email, Lead.OwnerId, Lead.Owner.FirstName, Lead.Owner.LastName, Lead.Owner.Id,
                    Contact.ID, Contact.AccountId, Contact.Salutation, Contact.Title, Contact.Name,
                    Contact.Phone, Contact.MailingStreet, Contact.MailingState, Contact.MailingPostalCode, 
                    Contact.MailingCountry, Contact.MailingCity, Contact.Fax, Contact.Email, Contact.OwnerId,
                    Contact.Owner.FirstName, Contact.Owner.LastName, Contact.Owner.Id, Contact.Account.name,
                    Log_A_Call_Notes__c, Contact.Contact_Salutation_Name__c, Title__c, Email__c, Phone__c from
                        Campaignmember where owner_Name__c =: mappingval.get(selectedval) limit 99];
                        
                   }
                     
                   if (selectedcval != '1' & selectedval != '1' )
                    {
                      CampaignMember = [select Id, owner_Name__c,Campaign_Name__c , ContactId, LeadId, Status, 
                        Lead.Lead_Salutation_Name__c, Country__c, CampaignId, Campaign.Name, Company__c,
                        Lead.Id, Lead.Salutation, Lead.Title, Lead.Name, Lead.Company, Lead.Phone,
                        Lead.Street, Lead.State, Lead.PostalCode, Lead.Country, Lead.City, Lead.Fax, 
                        Lead.Email, Lead.OwnerId, Lead.Owner.FirstName, Lead.Owner.LastName, Lead.Owner.Id,
                        Contact.ID, Contact.AccountId, Contact.Salutation, Contact.Title, Contact.Name, 
                        Contact.Phone, Contact.MailingStreet, Contact.MailingState, Contact.MailingPostalCode, 
                        Contact.MailingCountry, Contact.MailingCity, Contact.Fax, Contact.Email, Contact.OwnerId,
                        Contact.Owner.FirstName, Contact.Owner.LastName, Contact.Owner.Id, Contact.Account.name,
                        Log_A_Call_Notes__c, Contact.Contact_Salutation_Name__c, Title__c, Email__c, Phone__c from
                        Campaignmember where owner_Name__c =: mappingval.get(selectedval) and
                        Campaign_Name__c =: mappingcval.get(selectedcval) limit 99];  
                    }
                    system.debug('SFDC TEST10 *************' + selectedVal);        
                   if(selectedsval != '1' && selectedcval != '1' && selectedval != '1')
                    {
                    CampaignMember = [select Id, owner_Name__c, Campaign_Name__c, ContactId, LeadId, Status, 
                        Lead.Lead_Salutation_Name__c, Country__c, CampaignId, Campaign.Name,Company__c,
                        Lead.Id, Lead.Salutation, Lead.Title, Lead.Name, Lead.Company, Lead.Phone,
                        Lead.Street, Lead.State, Lead.PostalCode, Lead.Country, Lead.City, Lead.Fax, 
                        Lead.Email, Lead.OwnerId, Lead.Owner.FirstName, Lead.Owner.LastName, Lead.Owner.Id,
                        Contact.ID, Contact.AccountId, Contact.Salutation, Contact.Title, Contact.Name, 
                        Contact.Phone, Contact.MailingStreet, Contact.MailingState, Contact.MailingPostalCode, 
                        Contact.MailingCountry, Contact.MailingCity, Contact.Fax, Contact.Email, Contact.OwnerId,
                        Contact.Owner.FirstName, Contact.Owner.LastName, Contact.Owner.Id, Contact.Account.name,
                        Log_A_Call_Notes__c, Contact.Contact_Salutation_Name__c, Title__c, Email__c, Phone__c from
                       Campaignmember where owner_Name__c =: mappingval.get(selectedval) and
                        Campaign_Name__c =: mappingcval.get(selectedcval) and status =: mappingsval.get(selectedsval) limit 99];
                       system.debug('SFDC TEST11 *************' + selectedVal);   
                    }    
               }catch( Exception e ){
                ApexPages.addMessages( e );
            }     
             system.debug('##################' + CampaignMember);
             system.debug('SFDC TEST12 *************' + selectedVal);   
             return null;
    }
    
    List<CampaignMember> results;
    
    PageReference pageRef = new PageReference('/apex/AExcel');
    public PageReference campaignExport(){
             results= [select Id, owner_Name__c,Campaign_Name__c , ContactId, LeadId, Status, 
                        Lead.Lead_Salutation_Name__c, Country__c, CampaignId, Campaign.Name, Company__c,
                        Lead.Id, Lead.Salutation, Lead.Title, Lead.Name, Lead.Company, Lead.Phone,
                        Lead.Street, Lead.State, Lead.PostalCode, Lead.Country, Lead.City, Lead.Fax, 
                        Lead.Email, Lead.OwnerId, Lead.Owner.FirstName, Lead.Owner.LastName, Lead.Owner.Id,
                        Contact.ID, Contact.AccountId, Contact.Salutation, Contact.Title, Contact.Name, 
                        Contact.Phone, Contact.MailingStreet, Contact.MailingState, Contact.MailingPostalCode, 
                        Contact.MailingCountry, Contact.MailingCity, Contact.Fax, Contact.Email, Contact.OwnerId,
                        Contact.Owner.FirstName, Contact.Owner.LastName, Contact.Owner.Id, Contact.Account.name,
                        Log_A_Call_Notes__c, Contact.Contact_Salutation_Name__c, Title__c, Email__c, Phone__c from
                        Campaignmember where owner_Name__c =: mappingval.get(selectedval)limit 99];
      return pageref;
    }    
    Map<Id, CampaignMember> origCMMap = new Map<Id, CampaignMember>([select Id, owner_Name__c, Campaign_Name__c, ContactId, LeadId, Status, 
                        Lead.Lead_Salutation_Name__c, Country__c, CampaignId, Campaign.Name,Company__c,
                        Lead.Id, Lead.Salutation, Lead.Title, Lead.Name, Lead.Company, Lead.Phone,
                        Lead.Street, Lead.State, Lead.PostalCode, Lead.Country, Lead.City, Lead.Fax, 
                        Lead.Email, Lead.OwnerId, Lead.Owner.FirstName, Lead.Owner.LastName, Lead.Owner.Id,
                        Contact.ID, Contact.AccountId, Contact.Salutation, Contact.Title, Contact.Name, 
                        Contact.Phone, Contact.MailingStreet, Contact.MailingState, Contact.MailingPostalCode, 
                        Contact.MailingCountry, Contact.MailingCity, Contact.Fax, Contact.Email, Contact.OwnerId,
                        Contact.Owner.FirstName, Contact.Owner.LastName, Contact.Owner.Id, Contact.Account.name,
                        Log_A_Call_Notes__c, Contact.Contact_Salutation_Name__c, Title__c, Email__c, Phone__c from
                       Campaignmember where owner_Name__c =: mappingval.get(selectedval) and
                        Campaign_Name__c =: mappingcval.get(selectedcval) and status =: mappingsval.get(selectedsval) limit 99]);
              

 public PageReference save() {
        try {
            List<CampaignMember> changedCMList=new List<CampaignMember>();
            for ( CampaignMember cm: CampaignMember) {
                CampaignMember cm2=origCMMap.get(cm.Id);
                if ( cm2 != null &&
                    (cm2.Status != selectedsval ||
                     cm2.Log_A_Call_Notes__c != cm.Log_A_Call_Notes__c) ) {
                     changedCMList.add(cm);
                }
            }
            update changedCMList;
        } catch(DmlException ex){
            ApexPages.addMessages(ex);
        }
        return null;
    }
}

 

Visualforce Page

<apex:page standardcontroller="CampaignMember" extensions="CampaignListOwner13" showheader="true" sidebar="false">
  <apex:sectionHeader title="Campaign Lead Management" />
  <apex:form id="calls">  
    <apex:pageBlock >

        <apex:outputpanel layout="block">
            <apex:outputText >Owner: </apex:outputText>  
             
        <apex:selectList value="{!selectedval}" size="1">
          <apex:selectOptions value="{!selectedOwner}"/>
          <apex:actionSupport event="onchange" status="counterStatus" action="{!campaignRefresh}" rerender="campPck,calls,calls, 
                  status, callList, owners, goToCampaign"/>
</apex:selectList>
</apex:outputpanel>
        <apex:outputpanel layout="block" id="campPck">

            <apex:outputText >Campaign:</apex:outputText>        
        <apex:selectList value="{!selectedcval}" size="1"> 
          <apex:selectOptions value="{!campaignLists}"/>
            <apex:actionSupport event="onchange" status="counterStatus" action="{!campaignRefresh}" 
                rerender="campPck,calls,calls, status, callList, owners, goToCampaign"/>
</apex:selectList>
<br/>
            <apex:outputText >Status:</apex:outputText>        
        <apex:selectList value="{!selectedsval}" size="1"> 
          <apex:selectOptions value="{!statuslist}"/>
            <apex:actionSupport event="onchange" status="counterStatus" action="{!campaignRefresh}" 
rerender="calls, status, callList, owners, goToCampaign"/>
</apex:selectList>
      </apex:outputpanel>
      <apex:pageBlockButtons >
            <apex:outputPanel layout="block" style="float: right; padding-right: 5px; padding-bottom: 5px">
             <apex:commandButton value="Export to Excel" action="{!campaignExport}"/>
             <apex:commandButton value="Update All" action="{!save}"/>  
            </apex:outputPanel>
      </apex:pageBlockButtons>
        <apex:outputPanel layout="block">


            <apex:outputPanel layout="block" style="clear: both;">
            </apex:outputPanel>
        </apex:outputPanel>
           <apex:actionStatus id="counterStatus">
              <apex:facet name="start">  
                <apex:outputPanel id="processing">
                  <apex:pageMessage title="Processing..." severity="info" strength="2" rendered="true" />
                  
                  <apex:image id="loader" url="{!URLFOR($Resource.CampaignCallDown, '/images/loaderImage.gif')}"/>
                </apex:outputPanel>  

              </apex:facet>

              <apex:facet name="stop">
              
                    <apex:pageBlockTable value="{!CampaignMember}" var="members" id="calls">
                     <apex:column value="{!members.Owner_Name__c}" headerValue="Owner" 
                         rendered="true" />
                     <apex:column value="{!members.Campaignid}" headerValue="Campaign" 
                         rendered="true" />
                     <apex:column value="{!members.Country__c}" headerValue="Country" 
                         rendered="true" />
                     <apex:column value="{!members.leadid}" headerValue="Name" 
                         rendered="true" />
                     <apex:column value="{!members.Title__c}" headerValue="Title" 
                         rendered="true" />
                     <apex:column value="{!members.Country__c}" headerValue="Country" 
                         rendered="true" />
                    <apex:column headerValue="Status">
                           <apex:selectList size="1" value="{!statusupdate}" multiselect="false">
                           <apex:selectOptions value="{!cStatus}"/>
                           </apex:selectList>                    
                    </apex:column>
                     <apex:column headerValue="Notes">
                            <apex:inputTextarea value="{!members.Log_A_Call_Notes__c}" cols="25" rows="1" onFocus="this.rows=3;" onBlur="this.rows=1" />
                            <br />
                        </apex:column> 
                     <apex:column value="{!members.Email__c}" headerValue="E-mail" 
                         rendered="true" />
                     <apex:column value="{!members.Phone__c}" headerValue="Phone" 
                         rendered="true" />
                      

                    </apex:pageBlockTable>
                   </apex:facet>
                  </apex:actionStatus>
 
  </apex:pageBlock>     
</apex:form>
</apex:page>

 

How do I save correct this page reference to save only the records that I am editing?

 

thank you,

ckellie

Shashikant SharmaShashikant Sharma

Even though chances are very little that it will work but still jsut change in save Method

 

return null; to  

return ApexPages.currentPage();

 

Try one more thing , if you have not given any default value to status value when page loads for the first time , please set it to some value don't let it be null

 

Please provide this info 

1)As I see you have a action support as well on status change , do you rerender this status picklist field as well ?

 

ckellieckellie

Shashikant,

 

The change to the save method did not work. I am not rerendering the status column intentionally, rather am rerendering the entire pageBlock.

 

I am tring to set a default status value to the current campaignmember status value, but have not been able predictibly control the default status values.

 

Do you have any other suggestions?

 

Thank you,

ckellie

Chamil MadusankaChamil Madusanka

Hi,

 

change the method name of save method. "save" is satandard method name. Sometime It will be conflict with method name. Use a method name other than save. (Use saveBulk, saveApp, or something)

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

ckellieckellie

Hi,

 

I changed save to saveCustom, and it didn't help. The button only appears to refersh the page to its previous values.

 

What else can I do?

carlzcarlz

I'm having the same problem.