+ Start a Discussion
LoneStar69LoneStar69 

Updating selected records using a custom button

Hi Guys,

I am trying to create a custom button on list view that allows updation of selected records.

VF Page -

<apex:page standardController="Test__c" extensions="SetUpdation" sidebar="false" showHeader="false">
    <apex:form >
    
<apex:pageBlock title="Update">

        <apex:pageBlockSection columns="1">
        <apex:inputField value="{!Test__c.Situation__c}" />
</apex:pageBlockSection>
        
<apex:pageBlockButtons location="bottom">
        <apex:commandButton action="{!todo}" value="Save" status="closer"/>
        <apex:commandButton value="Cancel" action="{!cancel}" />
</apex:pageBlockButtons>
        
</apex:pageBlock>    
    
    </apex:form>
</apex:page>

Controller -

public with sharing class SetUpdation {
ApexPages.StandardController stdcontroller ;
    public SetUpdation(ApexPages.StandardController controller) {
    stdcontroller = controller;
    }

public PageReference todo(){
    
stdcontroller.save();
return (new pagereference('/apex/confirm').setredirect(true));
}
}

Now how to code this to update the selected records.
As i don't have that logic in place, it just creates a new record on Test__c when clicking save on my vf page.

Appreciate your help!
Thanks a lot.
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

Hi , I assume that you wan to select the records in the visualforce page and want to save only those records ?

 

Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
Try this code
public with sharing class exportc {

    public exportc() {

    }

    list<Accwrapper> accountlist {get; set;}

    public List<Account> selectedaccounts {get; set;}

    public exportc(ApexPages.StandardController controller) {

    }

    public  pagereference show()
    {
        accountlist=null;
        getaccounts();
        return null;
    }

    public list<Accwrapper> getaccounts()
    {

        if(accountlist==null) {
            accountlist = new list<Accwrapper>();
            for(Account a: [select id,Name from Account limit 10])
            {
                accountlist.add(new Accwrapper(a));
            }

        }
        return accountlist;

    }

    public  pagereference exportaccounts() {

        selectedaccounts=new List<Account>();
        system.debug('accccccccccc'+getaccounts());
        //We will cycle through our list of exportlists and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
        for(Accwrapper acc1: getaccounts()) {
            if(acc1.selected == true) {
                system.debug('trueeee');
                selectedaccounts.add(acc1.acc);
            }
        }

        system.debug('&&&&&&&&&&'+selectedaccounts);
        accountlist=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
        update selectedaccounts;



    }


    public class Accwrapper
    {
        public Account acc {get; set;}
        public Boolean selected {get; set;}

        public  Accwrapper(Account a)
        {
            acc=a;
            selected=false;
        }
    }
}
 
<apex:page controller="exportc">
  <apex:form >
  <apex:pageblock id="pb">
  <apex:pageblockButtons >
  <apex:commandButton value="ExportItems" action="{!exportaccounts}" />
  <c:customcomponent borderColor="blue" rerenderTarget="pb"></c:customcomponent>
  
  </apex:pageblockButtons>
  
  

    <apex:pageblocktable value="{!accounts}" var="a" id="table">
  <apex:column >
  <!-- This is our selected Boolean property in our wrapper class -->
  <apex:inputCheckbox value="{!a.selected}"/> 
  </apex:column>
  <apex:column headerValue="Account Id"> <apex:inputText value="{!a.acc.id}" /> </apex:column>
  <apex:column headerValue="Account Name"> <apex:inputText value="{!a.acc.Name}" /> </apex:column>
  </apex:pageblocktable>
  </apex:pageblock>
  </apex:form>
</apex:page>
LoneStar69LoneStar69
Hi Vivek,

I am trying to place a custom button on the list view to perform a mass update of the selected records.

Something like - https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=906F0000000AZlAIAW

But i am unable to code to make this work for the selected records.