0

ok im about to give up... I have a wrapper object that among other objects, also hold an account. Im referencing this accounts fields in a visualforce page and when i hit the save button, a debug shows that the wrapper object has not received any of the inputs i entered on the page. I really need some guidance here as I have tried everything to no avail...The account wrapper object is editCompanyWrapper

debug log on saveEditCompany method even though all fields were entered with information: DEBUG|### [saveEditCompany] editCompanyWrapper: GuaranteeAccountWrapper:[abn=, accType=--none--, acn=, active=true, city=, companyGuarantee=null, companycontactWrappers=(), fullLegalName=, guaranteeAccount=Account:{Id=0010000000NEW01AAH}, isNew=true, name=, postalcode=, state=, street=]

relevant vf page code: (not all its much bigger)

<apex:outputPanel id="editCompPanel">
        <apex:pageBlock rendered="{!showCompanyEditSection}">   
            <apex:sectionHeader title="Company Edit" subtitle="{!if(editCompanyWrapper.isNew =true,'New Account', editCompanyWrapper.guaranteeAccount.Name)}"/>
            <apex:pageBlockSection>
                <apex:inputField styleclass="slds-input slds-size_1-of-1" label="Name" value="{!editCompanyWrapper.guaranteeAccount.name}"/>
                <apex:inputField styleclass="slds-input slds-size_1-of-1" label="Full Legal Name" value="{!editCompanyWrapper.guaranteeAccount.full_Legal_Name__c}"/>
                <apex:inputtext  styleclass="slds-input slds-size_1-of-2" label="ABN" value="{!editCompanyWrapper.guaranteeAccount.abn__c}"/>
                <apex:inputField  styleclass="slds-input slds-size_1-of-2" label="ACN" value="{!editCompanyWrapper.guaranteeAccount.acn__c}"/>
                <apex:inputField  styleclass="slds-input slds-size_1-of-2" label="Type" value="{!editCompanyWrapper.guaranteeAccount.type}"/>
                <apex:inputField styleclass="slds-input slds-size_1-of-1" label="Street" value="{!editCompanyWrapper.guaranteeAccount.billingStreet}"/>
                <apex:inputField styleclass="slds-input slds-size_1-of-1" label="City" value="{!editCompanyWrapper.guaranteeAccount.billingCity}"/>
                <apex:inputField styleclass="slds-input slds-size_1-of-4" label="State" value="{!editCompanyWrapper.guaranteeAccount.billingState}"/>
                <apex:inputField styleclass="slds-input slds-size_1-of-4" label="Postal Code" value="{!editCompanyWrapper.guaranteeAccount.billingPostalCode}"/>
            </apex:pageBlockSection>
            <br/>
            <!-- EDIT COMPANY CONTACTS TABLE-->
            <apex:outputPanel id="contCompPanel">
            <table class="slds-table slds-table_bordered slds-table_cell-buffer slds-table_col-bordered" width="100%">
                <thead class="slds-text-title_caps">
                    <tr class="slds-text-title_caps">
                        <th class="slds-truncate col1 customColCont">First Name</th>
                        <th class="slds-truncate col1 customColCont">Last Name</th>
                        <th class="slds-truncate col1 customColCont">Email</th>
                        <th class="slds-truncate col1 customColCont">Mobile</th>
                        <th class="slds-truncate col1 customColCont">Designated Signer</th>
                        <th class="slds-truncate col1 customColCont">Active</th>
                        <th class="slds-truncate col1 customColCont">Delete</th>
                    </tr>
                </thead>
                <tbody>
                        <apex:repeat var="cont" value="{!editCompanyWrapper.companycontactWrappers}">
                            <tr>
                                <td class="slds-truncate">
                                    <apex:inputField styleclass="slds-input" value="{!cont.contact.FirstName}"/>
                                </td>
                                <td class="slds-truncate">
                                        <apex:inputField styleclass="slds-input" value="{!cont.contact.LastName}"/>
                                </td>
                                <td class="slds-truncate">
                                    <apex:inputField  styleclass="slds-input" value="{!cont.contact.Email}"/>

                                </td>
                                <td class="slds-truncate">
                                    <apex:inputField  styleclass="slds-input" value="{!cont.contact.MobilePhone}"/>
                                </td>
                                <td class="slds-truncate"><apex:inputCheckbox style="zoom: 150%" value="{!cont.acr.Designated_Signer__c}" /></td>
                                <td class="slds-truncate"><apex:inputCheckbox style="zoom: 150%"  value="{!cont.contact.Active__c}" /></td>                                
                                <td class="slds-truncate">
                                    <apex:actionRegion>
                                        <apex:commandButton action="{!deleteContact}" title="Remove Contact" value="Remove"  styleclass="slds-button slds-button_destructive delBtn" reRender="contCompPanel,msgsBtn" rendered="{!cont.isNew == true}" > 
                                            <apex:param value="{!cont.contact.Id}" assignTo="{!contId}" name="contactId" />
                                        </apex:commandButton>
                                    </apex:actionRegion>
                                 </td>
                            </tr>
                        </apex:repeat>
                </tbody>
            </table>  
            </apex:outputPanel>
            <br/>

            <apex:actionRegion>
                <apex:commandButton action="{!saveEditCompany}" value="Save Changes" reRender="editCompPanel,contCompPanel,guarCompPanel,msgsBtn,newCompBtn" styleclass="slds-button slds-button_brand saveContactsBtn"/> &nbsp;&nbsp;
                <apex:commandButton action="{!AddNewContactRow}" value="Add Contact" reRender="contCompPanel,msgsBtn" styleclass="slds-button slds-button_success saveContactsBtn"/>
                <apex:commandButton action="{!CancelEdit}" value="Cancel Edit" reRender="editCompPanel,contCompPanel,msgsBtn,guarCompPanel" styleclass="slds-button slds-button_destructive saveContactsBtn"/>
            </apex:actionRegion>


        </apex:pageBlock>
    </apex:outputPanel>

relevant apex controller code:

public Boolean                          showCompanyEditSection  {get;set;}
public integer newAccCnt = 1;
public integer newContCnt = 1;
public String                           accId                   {get;set;} // accid parameter from controlle to indicate account to edit
public Account                          editCompany             {get;set;}
public Account                          cloneCompany            {get;set;}
public list<GuaranteeAccountWrapper>    accCGWraps              {get{return cloneCompanyGuaranteeWrappersMap.values();} set;}

private map<id,GuaranteeAccountWrapper> companyGuaranteeWrappersMap = new map<id,GuaranteeAccountWrapper>();
private map<id,GuaranteeAccountWrapper> cloneCompanyGuaranteeWrappersMap = new map<id,GuaranteeAccountWrapper>();
private map<id,Company_Guarantee__c>    companyGuaranteeMap         = new map <id,Company_Guarantee__c>();


/*COMPANY GUARANTEE WRAPPER CLASS*/
public class GuaranteeAccountWrapper {
    public boolean                 isNew                        {get;set;}
    public Boolean                 active                       {get;set;}
    public Company_Guarantee__c    companyGuarantee             {get;set;}
    public Account                 guaranteeAccount             {get;set;}
    public GuaranteeContactWrapper[] companycontactWrappers     {get;set;}
}

public class GuaranteeContactWrapper {
    public boolean                 isNew                {get;set;}
    public AccountContactRelation  acr                  {get;set;}
    public Contact                 contact              {get;set;}
}

private void initCompanyGuaranteeDirectorWrappers (String pCompGuarQueryStr, String pCompGuarContQueryStr){
    companyGuaranteeWrappersMap.clear();
    companyGuaranteeMap.clear();
    set<id> accIds = new set<id>();

    // first query for the CompanyGuarantee records
    for(Company_Guarantee__c cgw: database.query(pCompGuarQueryStr + ' WHERE Account__c =\'' +  account.Id + '\'')){
        //add to an accid,compGuar map
        companyGuaranteeMap.put(cgw.Company_Guarantee__c,cgw);
        //add to an accIds set
        accIds.add(cgw.Company_Guarantee__c);
    }
    set<id> cgwSet = new Set <id> (companyGuaranteeMap.keyset());

    //query for contacts with parent info (accounts) using accIds set as filter
    for(AccountContactRelation acr: database.query(pCompGuarContQueryStr + 'WHERE AccountId IN :cgwSet ORDER BY AccountId')){
        //create new accounts and contact from parent acc info
        Account compAccount = new Account(id = acr.AccountId, Name= acr.Account.Name, Full_Legal_Name__c= acr.Account.Full_Legal_Name__c, BillingStreet = acr.Account.BillingStreet, BillingCity = acr.aCcount.BillingCity,
                                            BillingState = acr.Account.BillingState, BillingPostalCode = acr.Account.BillingPostalCode, type= acr.Account.type, ABN__c = acr.Account.ABN__c, ACN__c = acr.Account.ACN__c);
        Contact compCont = new Contact (id=acr.ContactId, FirstName = acr.Contact.FirstName, LastName = acr.Contact.LastName, Email = acr.Contact.Email, MobilePhone= acr.Contact.MobilePhone,
                                        Active__c = acr.Contact.Active__c);
        GuaranteeContactWrapper ccw = new GuaranteeContactWrapper();
        ccw.acr = acr;
        ccw.contact = compCont;
        ccw.isNew = false;


        //create the GuaranteeAccountWrapper
        if(!companyGuaranteeWrappersMap.containsKey(compAccount.Id)){
            GuaranteeAccountWrapper cgr     = new GuaranteeAccountWrapper();
            cgr.isNew = false;
            cgr.companyGuarantee = companyGuaranteeMap.get(compAccount.Id);
            cgr.Active = companyGuaranteeMap.get(compAccount.Id).Active__c;
            cgr.guaranteeAccount = compAccount;
            cgr.companycontactWrappers = new GuaranteeContactWrapper[] {ccw};
            companyGuaranteeWrappersMap.put(compAccount.Id,cgr);
        }else{
            companyGuaranteeWrappersMap.get(compAccount.Id).companycontactWrappers.add(ccw);
        }
        String companywrappersjson = JSON.serialize(companyGuaranteeWrappersMap);
        system.debug('### [initCompanyGuaranteeDirectorWrappers] companywrappersjson: ' + companywrappersjson);
        cloneCompanyGuaranteeWrappersMap = (map<id,GuaranteeAccountWrapper>) JSON.deserialize(companywrappersjson, map<id,GuaranteeAccountWrapper>.class);
     }       
}

private String                              companyGuaranteeWrapperJSON; // original copy of the CompanyGuarantee that was set to be edited
public  GuaranteeAccountWrapper             editCompanyWrapper      {get;set;}
public  Account                             editCompanyAccount      {get{ return editCompanyWrapper.guaranteeAccount;} set;}


/*this method assumes an existing account is being edited*/
public Pagereference editGuaranteeCompany (){

    //set the company to editCompanyWrapper
    system.debug('### [editGuaranteeCompany] companyGuaranteeWrappersMap.get(accId): ' + companyGuaranteeWrappersMap.get(accId));    
    companyGuaranteeWrapperJSON = JSON.serialize(companyGuaranteeWrappersMap.get(accId));

    if(editCompanyWrapper== null) editCompanyWrapper = new GuaranteeAccountWrapper ();

    editCompanyWrapper = (GuaranteeAccountWrapper) JSON.deserialize(companyGuaranteeWrapperJSON, GuaranteeAccountWrapper.class); //companyGuaranteeWrappersMap.get(accId).clone(); // ;
    system.debug('### [editGuaranteeCompany] companyGuaranteeWrappersMap.get(accId): ' + companyGuaranteeWrappersMap.get(accId));
    system.debug('### [editGuaranteeCompany] editCompanyWrapper: ' + editCompanyWrapper);    

    showCompanyEditSection  = true;
    return null;
}

/* this method will add a contact to a new or existing accounts related contact records*/
public PageReference AddNewContactRow (){

    //to add a new contact to an existing guarantee company
    //first init a contact
    Contact newcont = new contact();
    //then link the contact to the accountid (if its a new account then generate a mock id for now)
    newCont.AccountId = editCompanyWrapper.guaranteeAccount.Id; 
    newCont.Id = '0030000000'+ 'NEW0' + newContCnt;
    newCont.Active__c = true;

    AccountContactRelation acr = new AccountContactRelation();

    //create the Contact Wrapper record 
    GuaranteeContactWrapper ccw = new GuaranteeContactWrapper();
    ccw.contact = newCont;
    ccw.isNew = true;

    ccw.acr= acr;

    editCompanyWrapper.companycontactWrappers.add(ccw);
    system.debug('### [AddNewContactRow] editCompanyWrapper.companycontactWrappers: ' + editCompanyWrapper.companycontactWrappers);
    system.debug('### [AddNewContactRow] companyGuaranteeWrappersMap.get(accId): ' + companyGuaranteeWrappersMap.get(accId));

    newContCnt++;
    return null;
}

public PageReference AddNewCompanyRow (){

    // Create a blank account
    Account acc = new Account();

    // assign a mock id
    acc.Id =  '0010000000'+ 'NEW0' + newAccCnt;
    newAccCnt++;

    //create the GuaranteeAccountWrapper
    GuaranteeAccountWrapper cgw = new GuaranteeAccountWrapper();
    cgw.guaranteeAccount = acc;
    cgw.active = true;
    cgw.isNew = true;
    cgw.companycontactWrappers = new GuaranteeContactWrapper[]{};
    editCompanyWrapper = cgw; 
    showCompanyEditSection  = true;
    system.debug('### [AddNewContactRow] editCompanyWrapper: ' + editCompanyWrapper);

    return null;
}

public Id contId {get;set;}

public PageReference DeleteContact (){
    if(contId == null) return null;
    Integer j = 0;
    while (j < editCompanyWrapper.companycontactWrappers.size()){
      if(editCompanyWrapper.companycontactWrappers.get(j).contact.id == contId){
        editCompanyWrapper.companycontactWrappers.remove(j);
      }else{
        j++;
      }
    }

    return null;
}

public PageReference CancelEdit (){
    showCompanyEditSection = false;
    newAccCnt = 0;
    newContCnt = 0;
    system.debug('### [CancelEdit] editCompanyWrapper: ' + editCompanyWrapper.companycontactWrappers);

    editCompanyWrapper =null ; 
    system.debug('### [CancelEdit] editCompanyWrapper.companycontactWrappers: ' + editCompanyWrapper);
    system.debug('### [CancelEdit] companyGuaranteeWrappersMap: ' + companyGuaranteeWrappersMap);    
    return null;
}

private boolean validateAccountData (Account pAcc){
    if(pAcc.Name == null || pAcc.Full_Legal_Name__c == null || pAcc.BillingStreet == null || pAcc.BillingCity == null || pAcc.BillingState == null || pAcc.BillingPostalCode == null){
        return false;
    }
    else return true;
}



public PageReference saveEditCompany (){

    //if the account isNEW then need to insert account, get ID, 
    //update the contact Accountids

    list<AccountContactRelation> saveACRs       = new List<AccountContactRelation>();
    List<Contact>                saveContacts   = new List<Contact>();
    List<Company_Guarantee__c>   saveCompGuars  = new List<Company_Guarantee__c>();

    // first validate account data
    system.debug('### [saveEditCompany] editCompanyWrapper: ' + editCompanyWrapper);
    if(validateAccountData(editCompanyWrapper.guaranteeAccount) == false){
        ApexPages.Message msgError = new ApexPages.Message(ApexPages.Severity.ERROR,'All Guarantee Account information must be completed before saving.');
        ApexPages.addMessage(msgError);
        return null;
    }

    system.debug('### [saveEditCompany] editCompanyWrapper: ' + editCompanyWrapper);

    // then update the account (reset the Id if itsd a new account as the id is not valid, was used only for reference between the )
    if(editCompanyWrapper.isNew){editCompanyWrapper.guaranteeAccount.id = null;
    Account saveAcc = editCompanyWrapper.guaranteeAccount;
    upsert saveAcc;

    //validate at least one contct has been added to the account 
    if(editCompanyWrapper.companycontactWrappers == null || editCompanyWrapper.companycontactWrappers.isEmpty()){
        ApexPages.Message msgError = new ApexPages.Message(ApexPages.Severity.ERROR,'Guarantee Accounts must contain at least one contact.');
        ApexPages.addMessage(msgError);
        return null;
    }

        //create the Company Guanratee for the borrower's Account
        if(editCompanyWrapper.isNew){
            Company_Guarantee__c cg = new Company_guarantee__c (Account__c = account.id, Company_Guarantee__c= saveAcc.id);
            saveCompGuars.add(cg);
        }

        //now iterate over all contacts    
        for(GuaranteeContactWrapper gcw: editCompanyWrapper.companycontactWrappers ){
            system.debug('[saveEditCompany] guaranteeContactWrapper: ' + gcw);
            Contact saveCont = gcw.contact;
            if(gcw.isNew == true){
                saveCont.AccountId = saveAcc.id;
            }
            saveContacts.add(saveCont);
        }

        //upsert the Contact to get the new ids from new contacts, then use that to update 
        if(!saveContacts.isEmpty()) upsert saveContacts;

        /*system.debug('### saveACRs: ' + saveACRs);
        if(!saveACRs.isEmpty()) upsert saveACRs;*/
        editCompanyWrapper = null;
        initCompanyGuaranteeDirectorWrappers(companyGuaranteesQuery, companyGuaranteeContactsQuery);
        showCompanyEditSection = false;
    }
    return null;
}
  • 1
    I believe it is because your actionregion only surrounds the buttons. Since the field are outside the actionregion they will not be sent to the server. A quick fix would be to wrap the section and buttons in within the same apex:form tag – Eric Jul 26 '18 at 4:59
  • Hello sorry for the late reply, but yes you were right the action function was not being used properly, after moving around fields and form it worked as expected. thank you for your reply. – Mauricio.Ramos1979 Aug 27 '18 at 6:38

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.