1

How to assign value of one field to another field of a same object by clicking on a button, for selected rows from list view of an object? Here is my visualforce page and apex class...

<<----- Visualforce Page ----->>

<apex:page standardController="Investment__c" extensions="BulkUpdateApex" recordSetVar="Investment">
   <apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!InvestmentList}" var="iL">

            <apex:column value="{!iL.Company__c}"/>
            <apex:column value="{!iL.Ticker__c}"/>
            <apex:column value="{!iL.Updated_Price__c}"/>
            <apex:column value="{!iL.Price__c}"/>
            <apex:column value="{!iL.Pricing_Date__c}"/>
        </apex:pageBlockTable>

        <apex:pageBlockButtons location="bottom" id="mubut">
                <apex:commandButton value="Save" action="{!save}" id="butsav"/>
                <apex:commandButton value="Cancel" action="{!cancel}" id="butcan"/>
            </apex:pageBlockButtons>    
    </apex:pageBlock>
   </apex:form>
</apex:page>

And My extension class is:

<<----- Apex Class Extension ----->>

public class BulkUpdateApex {
    public List<Investment__c> InvestmentList{get;set;}

    private ApexPages.StandardSetController standardController;
    private List<Id> InvestmentIds = new List<Id>();


    public BulkUpdateApex(ApexPages.StandardSetController standardController){

        this.standardController = standardController;

        InvestmentList = new List<Investment__c>();


        for (Investment__c invstmnt : (List<Investment__c>)standardController.getSelected()){
            InvestmentIds.add(invstmnt.Id);
        }

        InvestmentList = [SELECT Id, Investment__c.Company__c, Ticker__c, Price__c, Updated_Price__c, Pricing_Date__c FROM Investment__c WHERE ID IN: InvestmentIds ORDER BY Investment__c.Company__c ASC];

        for (Integer countForInvstmntListIncrement = 0; countForInvstmntListIncrement< InvestmentList.size(); countForInvstmntListIncrement++){
            string CheckId = InvestmentList.get(countForInvstmntListIncrement).Id;

            Integer countForInvstmntIncrement = 0;

            for (Investment__c invstmnt : (List<Investment__c>)standardController.getSelected()){
                if (CheckId.equals(invstmnt.Id)){
             //**************************************************
                    // For getting Price....
                    invstmnt.Price__c = invstmnt.Updated_Price__c;
             //**************************************************   
                    break;
                }else{
                    countForInvstmntIncrement++;
                }  
            }
        }        
    }
}

I want to assign value of "Updated_Price__c" to the field "Price__c".

But I am getting error that is:

"SObject row was retrieved via SOQL without querying the requested field: Investment__c.Updated_Price__c ".

Thanks in advance for help.

I have updated my code as :

for (Investment__c invstmnt : InvestmentList){ if (CheckId.equals(invstmnt.Id)){ double price = invstmnt.Updated_Price__c; // For getting Price.... invstmnt.Price__c = price;

                break;
            }else{
                countForInvstmntIncrement++;
            }  
            update invstmnt;  // This is update Statement to update Investment record. DML statement..
        }

But I got error "DML currently not allowed".

0

in your inner for loop you are iterating over the records from getSelectected() list instead of using your list object InvestmentList

if you change your code from

for (Investment__c invstmnt : (List<Investment__c>)standardController.getSelected()){

to

for (Investment__c invstmnt : InvestmentList){

it should get rid of the error..

but you can try further optimise the code and keep it simple..

public class BulkUpdateApex {
    public List<Investment__c> InvestmentList{get;set;}

    private ApexPages.StandardSetController standardController;    

    public BulkUpdateApex(ApexPages.StandardSetController standardController){

        this.standardController = standardController;

        InvestmentList = [SELECT Id, Investment__c.Company__c, Ticker__c, Price__c, Updated_Price__c, Pricing_Date__c FROM Investment__c WHERE ID IN: standardController.getSelected() ORDER BY Investment__c.Company__c ASC];

        for (Investment__c invstmnt : InvestmentList){
            invstmnt.Price__c = invstmnt.Updated_Price__c;
        }

    }
}
4
  • Hi Vamsi, I have tried your answer code but it is not working. It is showing me the value of "Updated_Price__c" field in the field "Price__c". But when I click on save button to save the values in "Price__c" field, it is not saving these values in "Price__c" field. – Khawaja M. Awais Sep 30 '15 at 6:14
  • the method called from the save action button should update the record with the new values. otherwise it wont get saved. – Vamsi Krishna Gosu Sep 30 '15 at 8:56
  • But I am using Default- Save action of salesforce – Khawaja M. Awais Sep 30 '15 at 10:08
  • I have updated my code as : for (Investment__c invstmnt : InvestmentList){ if (CheckId.equals(invstmnt.Id)){ double price = invstmnt.Updated_Price__c; // For getting Price.... invstmnt.Price__c = price; break; }else{ countForInvstmntIncrement++; } update invstmnt; // This is update Statement to update Investment record. DML statement.. } But I got error "DML currently not allowed". – Khawaja M. Awais Sep 30 '15 at 10:31
0

Thanks For Help Vamsi Krishna.

I got answer for this problem.

In Visualforce page, we have to change

<<---VisualForce page--->>

<apex:page standardController="Investment__c" extensions="BulkUpdateApex" action="{!UpdateBulk}" recordSetVar="Investment"/>

And Controller has been changed as:

<<---- Apex Controller---->>

public class BulkUpdateApex {
    public List<Investment__c> InvestmentList{get;set;}    
    private ApexPages.StandardSetController standardController;
    private List<Id> InvestmentIds = new List<Id>();

    public BulkUpdateApex(ApexPages.StandardSetController standardController){
        this.standardController = standardController;
        InvestmentList = new List<Investment__c>();     

    }



    public void UpdateBulk(){

        for (Investment__c invstmnt : (List<Investment__c>)standardController.getSelected()){
            InvestmentIds.add(invstmnt.Id);
        }

        InvestmentList = [SELECT Id, Investment__c.Company__c, Ticker__c, Price__c, Quandle_Price__c, Pricing_Date__c FROM Investment__c WHERE ID IN: InvestmentIds ORDER BY Investment__c.Company__c ASC];

//        for (Integer countForInvstmntListIncrement = 0; countForInvstmntListIncrement< InvestmentList.size(); ){
        Integer countForInvstmntListIncrement = 0;
        while (countForInvstmntListIncrement < InvestmentList.size()){
            string CheckId = InvestmentList.get(countForInvstmntListIncrement).Id;

            Integer countForInvstmntIncrement = 0;

            for (Investment__c invstmnt : InvestmentList){
                if (CheckId.equals(invstmnt.Id)){

                    // For getting Price....6
                    invstmnt.Price__c = invstmnt.Quandle_Price__c;
                    countForInvstmntListIncrement++;
                    update invstmnt;
                    break;
                }else{
                    countForInvstmntIncrement++;
                } 
            }
        }        
    }
}

In this solution, I had changed the visualforce page by adding an attribute [action="{!UpdateBulk}"]

and In controller, made a function "UpdateBulk" without any interference of constructor and here I used DML statement "update invstmnt" to update records in investment__c object.. And this is working for me...:)

Hope It will help others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.