2

I have a VF page that List account records that meet certain criteria. The user would select which accounts as well as fill out a couple notes fields (that do not exist in the Account object). Once they fill out the form and have selected the accounts, then i want it to write certain fields from the account record along with the two notes fields on the form to a new object.

Page:

<apex:page controller="wrapperClassController">
<script>
//This javascript method is to select/unselect all the check boxes if header level check box is selected.
function checkAll(selectAll) {

    var inputElem = document.getElementsByTagName("input");
    for(var i=0; i<inputElem.length; i++) {
            if(inputElem[i].id.indexOf("selectedRecords")!=-1)
                inputElem[i].checked = selectAll.checked;
    }
}
</script>
<apex:form >
    <apex:pageBlock title="New/Reactivated Accounts" mode="edit">
    <apex:pageBlockSection title="Parent BPID" columns="2">
        <apex:inputField value="{!nar.Parent_BPID__c}"/>
    </apex:pageBlockSection>            
    <apex:pageBlockSection title="General Notes" columns="2">
        <apex:inputTextarea id="newNote" value="{!nar.General_Notes__c}" cols="100"/>
    </apex:pageBlockSection>
    <apex:pageBlockSection title="Web Notes" columns="2">
        <apex:inputTextarea id="webNote" value="{!nar.Web_Notes__c}" cols="100"/>
    </apex:pageBlockSection>

        <apex:pageBlockButtons >
            <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
        </apex:pageBlockButtons>



        <apex:pageBlockSection title="Select Account(s)" columns="2">

        <apex:pageBlockTable value="{!accounts}" var="a" id="table">
            <apex:column >

                <apex:facet name="header">
                    <input type="checkbox" id="headerCheck" onclick="checkAll(this);" />
                </apex:facet>
                <apex:inputCheckBox id="selectedRecords" value="{!a.selected}"/>
                <apex:param value="{!a.acct.Id}" name="selectedRecord"/>
            </apex:column>

            <apex:column value="{!a.acct.Baan_BPID__c}"><apex:facet name="header">BPID</apex:facet></apex:column>
            <apex:column value="{!a.acct.name}"><apex:facet name="header">Account</apex:facet></apex:column>
            <apex:column value="{!a.acct.shippingstreet}"><apex:facet name="header">Address</apex:facet></apex:column>
            <apex:column value="{!a.acct.shippingcity}"><apex:facet name="header">City</apex:facet></apex:column>
            <apex:column value="{!a.acct.shippingstate}"><apex:facet name="header">State</apex:facet></apex:column>
            <apex:column value="{!a.acct.shippingpostalcode}"><apex:facet name="header">Zip</apex:facet></apex:column>
            <apex:column value="{!a.acct.OG_Baan_BP_Type__c}"><apex:facet name="header">Baan BP Type</apex:facet></apex:column>
            <apex:column value="{!a.acct.DEA_License__c}"><apex:facet name="header">DEA #</apex:facet></apex:column>
            <apex:column value="{!a.acct.ASD_SML__c}"><apex:facet name="header">SML #</apex:facet></apex:column>
            <apex:column value="{!a.acct.OG_Channel_Code__c}"><apex:facet name="header">Channel Code</apex:facet></apex:column>
            <apex:column value="{!a.acct.OG_Credit_Limit__c}"><apex:facet name="header">Credit Limit</apex:facet></apex:column>
            <apex:column value="{!a.acct.OG_Payment_Terms__c}"><apex:facet name="header">Payment Terms</apex:facet></apex:column>
            <apex:column value="{!a.acct.OG_Statement_Method__c}"><apex:facet name="header">Statement Method</apex:facet></apex:column>
        </apex:pageBlockTable>

        </apex:pageBlockSection>

    </apex:pageBlock>

</apex:form>

Class:

public class wrapperClassController {

public wrapperClassController() {
    nar = new New_Account_Reactivation__c();
}

public New_Account_Reactivation__c nar {get;set;}

public List<cAccount> accountList {get; set;}

public List<cAccount> getAccounts() {
    if(accountList == null) {
        accountList = new List<cAccount>();
        for(Account c: [SELECT
                                    Id,
                                    baan_bpid__c, 
                                    name, 
                                    shippingstreet, 
                                    shippingcity, 
                                    shippingstate, 
                                    shippingpostalcode, 
                                    og_baan_bp_type__c,
                                    DEA_License__c, 
                                    ASD_SML__c,
                                    OG_Channel_Code__c, 
                                    OG_Credit_Limit__c, 
                                    OG_Payment_Terms__c, 
                                    OG_Statement_Method__c
                                FROM Account 
                                WHERE 
                                    baan_bpid__c <> '' AND 
                                    CreatedDate = LAST_N_DAYS:222 
                                LIMIT 10]) {
            accountList.add(new cAccount(c));
        }
    }
    return accountList;
}


public PageReference processSelected() {

    List<Account> selectedAccounts = new List<Account>();

    for(cAccount cAcct: getAccounts()) {
        if(cAcct.selected == true) {
            selectedAccounts.add(cAcct.acct);
        }
    }

    for(Account acct: selectedAccounts) {

        nar.BPID__c = acct.Baan_BPID__c;
        nar.Account__c = acct.Id;
        nar.Address__c = acct.shippingStreet;
        nar.State__c = acct.shippingState;
        nar.City__c = acct.shippingCity;
        nar.Zip__c = acct.shippingPostalCode;
        nar.Baan_BP_Type__c = acct.og_baan_bp_type__c;
        nar.DEA__c = acct.DEA_License__c;
        nar.SML__c = acct.ASD_SML__c;
        nar.Channel_Code__c = acct.OG_Channel_Code__c;
        nar.Credit_Limit__c = acct.OG_Credit_Limit__c;
        nar.Payment_Terms__c = acct.OG_Payment_Terms__c;
        nar.Statement_Method__c = acct.OG_Statement_Method__c;

    }
    upsert nar;

    accountList=null; 
    return null;
}

public class cAccount {
    public Account acct {get; set;}
    public Boolean selected {get; set;}

    public cAccount(Account c) {
        acct = c;
        selected = false;
    }
}
}

If I select 4 account records and fill in the two notes textarea boxes, it will only write the last account record i select to the New_Account_Reactivation__c object and will not write the data from the notes textarea boxes. I am a bit new to apex so please forgive my ignorance. If someone could help out...point in the right direction....point out what "all" i am doing wrong, I would very much appreciate it.

Thanks in advance.

2 Answers 2

2

The reason why you're seeing behavior of "last one wins" is because your code is overwriting the data of New_Account_Reactivation__c for each account that is processed in the for loop.

To correct the behavior you're seeing, you need an instance of New_Account_Reactivation__c for each Account that you process. You need to move the instance of New_Account_Reactivation__c that is used for DML within the processing method and create one instance for each selected Account that you are processing for later DML.

Because you're also using the nar record for the user to input data with - I've created another instance of the object which is only a helper to collect the notes data from the user. The notes are copied from this one instance into the individual New_Account_Reactivation__c records that are upserted into the database at the end of your process method.

public class wrapperClassController {

    public New_Account_Reactivation__c bogusNAR { get; set; }

    public wrapperClassController() {
        bogusNAR = new New_Account_Reactivation();
    }

    public List<cAccount> accountList {get; set;}

    public List<cAccount> getAccounts() {
            if(accountList == null) {
                accountList = new List<cAccount>();
                for(Account c: [SELECT
                    Id,
                    baan_bpid__c, 
                    name, 
                    shippingstreet, 
                    shippingcity, 
                    shippingstate, 
                    shippingpostalcode, 
                    og_baan_bp_type__c,
                    DEA_License__c, 
                    ASD_SML__c,
                    OG_Channel_Code__c, 
                    OG_Credit_Limit__c, 
                    OG_Payment_Terms__c, 
                    OG_Statement_Method__c
                    FROM Account 
                    WHERE 
                    baan_bpid__c <> '' AND 
                    CreatedDate = LAST_N_DAYS:222 
                    LIMIT 10]) {
                    accountList.add(new cAccount(c));
            }
        }
        return accountList;
    }


    public PageReference processSelected() {

        List<Account> selectedAccounts = new List<Account>();

        // create a list for DML - this list is populated by the account loop below
        List<New_Account_Reactivation__c> reactivationsToUpsert = new List<New_Account_Reactivation__c>();

        for(cAccount cAcct: getAccounts()) {
            if(cAcct.selected == true) {
                selectedAccounts.add(cAcct.acct);
            }
        }

        for(Account acct: selectedAccounts) {

            // create an instance
            New_Account_Reactivation__c nar = new New_Account_Reactivation__c();

            // set the field values
            nar.BPID__c = acct.Baan_BPID__c;
            nar.Account__c = acct.Id;
            nar.Address__c = acct.shippingStreet;
            nar.State__c = acct.shippingState;
            nar.City__c = acct.shippingCity;
            nar.Zip__c = acct.shippingPostalCode;
            nar.Baan_BP_Type__c = acct.og_baan_bp_type__c;
            nar.DEA__c = acct.DEA_License__c;
            nar.SML__c = acct.ASD_SML__c;
            nar.Channel_Code__c = acct.OG_Channel_Code__c;
            nar.Credit_Limit__c = acct.OG_Credit_Limit__c;
            nar.Payment_Terms__c = acct.OG_Payment_Terms__c;
            nar.Statement_Method__c = acct.OG_Statement_Method__c;

            // get the notes from the fake NAR record bound to the page input fields
            nar.Web_Notes__c = bogusNAR.Web_Notes__c;
            nar.General_Notes__c = bogusNAR.General_Notes__c;

            // add it to the list for later DML
            reactivationsToUpsert.add(nar);
        }

        // check for not empty and then DML
        if (!reactivationsToUpsert.isEmpty()) {
            upsert reactivationsToUpsert;
        }

        accountList=null; 
        return null;
    }

    public class cAccount {
        public Account acct {get; set;}
        public Boolean selected {get; set;}

        public cAccount(Account c) {
            acct = c;
            selected = false;
        }
    }
}

VF

<apex:pageBlockSection title="Parent BPID" columns="2">
    <apex:inputField value="{!bogusNAR.Parent_BPID__c}"/>
</apex:pageBlockSection>            
<apex:pageBlockSection title="General Notes" columns="2">
    <apex:inputTextarea id="newNote" value="{!bogusNAR.General_Notes__c}" cols="100"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Web Notes" columns="2">
    <apex:inputTextarea id="webNote" value="{!bogusNAR.Web_Notes__c}" cols="100"/>
</apex:pageBlockSection>
1
  • Perfect....Thank you so much Mark. Have a blessed day, my friend.
    – Billw
    Commented Sep 9, 2014 at 14:57
0

Your Visualforce page uses "wrapperClassController" as its custom controller. Because of that, it won't be saving anything related to variable "nar" (of type New_Account_Reactivation__c)

 public New_Account_Reactivation__c nar {get;set;}

unless you do it explicitly. You could have a "insert nar" line in method processSelected() to do that.

I'm not sure why only one account is getting updated. I'd recommend some debug statements so you can verify in the log file that you are getting all the records you expect to.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .