0

i really need your help.

Im actually a beginner in dev so maybe you can tell me whats wrong with my code.

I have to make two list of Accounts, each account have a custom field named "Nivel__C" (level _ _ C) that could be Nivel 1 and Nivel 2.

Then with a checkbox and a command button i need to change their Nivel__c field. for example:

list 1: acc 1 - nivel 1

acc 2 - nivel 1

list 2: acc 3 - nivel 2

I need to check acc 1 and acc 3 and once they are cheked click a button and change the nivel__c custom field (i know that is with if niv1 - niv 2 else niv 1)

I already used a code that let me do everything im asking, but it only let me if i add mi checked account to a third table and i cant (and REALLY NEED TO) delete that extra step.

(btw i cant use javascript or anything else, just code like im doing right now...) I post my apex and visualforce code for your help. apex:

public class wrap1 {
public List<AccountWrapper> listAccountWrapper {get; set;}
public list<AccountWrapper> listAccountWrapperr {get; set;}
public List<Account> selectedAccounts{get;set;}
public Wrap1 ()
{
        listAccountWrapper = new List<AccountWrapper>();
        searchRecord();

}
{
    listAccountwrapperr= new List<AccountWrapper>();
}
public void searchRecord()
{
    listAccountWrapper.clear();
        for(Account a: [select Id, Name, Phone ,Nivel__c, LastModifiedById from Account where Nivel__c = 'Nivel 1'])
        {
            listAccountWrapper.add(new AccountWrapper(a));
        }
    for(Account a: [select Id, Name, Phone ,Nivel__c, LastModifiedById from Account where Nivel__c = 'Nivel 2'])
    {
        listAccountWrapperr.add(new AccountWrapper(a));

    }
}




public void ModificarNivelDeCuenta()
{
    selectedAccounts = new List<Account>();
    selectedAccounts.clear();
    for(AccountWrapper wrapAccountObj : listAccountWrapper)
    {
        if(wrapAccountObj.selected == true)
        {
            selectedAccounts.add(wrapAccountObj.acc);
        }
    }
}


public void ProcessSelected(){
    for(Account acc: selectedAccounts)
    {
      if (acc.Nivel__c=='Nivel 1') 
    {
        acc.Nivel__c ='Nivel 2';
    }
        else {
        acc.Nivel__c ='Nivel 1';
    }

        update selectedAccounts ;
        searchRecord();
    }
}




    public class AccountWrapper
{
    public Account acc {get; set;}
    public Boolean selected {get; set;}
    public AccountWrapper(Account a)
    {
        acc = a;
        selected = false;
    }
}

}

visualforce:

<apex:page controller="wrap1">
<script type="text/javascript">
    function selectAllCheckboxes(obj,receivedInputID){
        var inputCheckBox = document.getElementsByTagName("input");
        for(var i=0; i<inputCheckBox.length; i++){
            if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                inputCheckBox[i].checked = obj.checked;
            }
        }
    } 
         </script>
         <apex:form >
         <apex:pageBlock id="PB1">
         <apex:pageBlockButtons >
         <apex:commandButton value="Agregar" action="{!ModificarNivelDeCuenta}" rerender="table2,PB1,PB2"/>
         </apex:pageBlockButtons>
             <apex:pageblockSection title="Cuentas nivel 1" collapsible="false" columns="4">

                 <apex:pageBlockTable value="{!listAccountWrapper}" var="accWrap" id="table">
                 <apex:column >
                    <apex:facet name="header">
                        <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                    </apex:facet>
                    <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                </apex:column>

                <apex:column value="{!accWrap.acc.Name}" />
                <apex:column value="{!accWrap.acc.Phone}" />
                <apex:column value="{!accWrap.acc.LastModifiedById}"  />
                <apex:column value="{!accWrap.acc.Nivel__c}"  />
            </apex:pageBlockTable>
            </apex:pageblockSection>
            </apex:pageBlock>









             <apex:pageBlock id="PB2">
         <apex:pageBlockButtons >
         <apex:commandButton value="Agregar" action="{!ModificarNivelDeCuenta}" reRender="PB1,PB2"/>
         </apex:pageBlockButtons>
             <apex:pageblockSection title="Cuentas nivel 2" collapsible="false" columns="4">

                 <apex:pageBlockTable value="{!listAccountWrapperr}" var="accWrap" id="table">
                 <apex:column >
                    <apex:facet name="header">
                        <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                    </apex:facet>
                    <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                </apex:column>

                <apex:column value="{!accWrap.acc.Name}" />
                <apex:column value="{!accWrap.acc.Phone}" />
                <apex:column value="{!accWrap.acc.LastModifiedById}"  />
                <apex:column value="{!accWrap.acc.Nivel__c}"  />
            </apex:pageBlockTable>
            </apex:pageblockSection>
            </apex:pageBlock>













    <apex:pageBlock id="PB3" >
        <apex:pageBlockButtons >
            <apex:commandButton value="Cambiar nivel de cuenta" action="{!processSelected}" rerender="PB1,PB2"/>
        </apex:pageBlockButtons>

            <apex:pageBlockTable value="{!selectedAccounts}" var="c" id="table2" title="Cuentas seleccionadas">
                <apex:column value="{!c.Name}" headerValue="Nombre de la cuenta"/>
                <apex:column value="{!c.Phone}" headerValue="Telefono"/>
                <apex:column value="{!c.LastModifiedById}" headerValue="Ultima modificación por el usuario"/>

            </apex:pageBlockTable>
    </apex:pageBlock>


</apex:form>

Your Answer

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

Browse other questions tagged or ask your own question.