1

I'm leaning a VF Page Creation. I'm try to create page from which i can save muliple account. in which i've to implement, add and remove row. i'm able to add row perfectly as required but unable to delete row.

My VF Page code:

<apex:page standardController="Account" sidebar="false" extensions="Accountextension">
    <apex:form >

     <apex:pageBlock title="Account Records Edit" id="Refresh" >
        <apex:pageBlockButtons location="both">
        <apex:commandButton value="Save" action="{!save}" />
        </apex:pageBlockButtons>

        <apex:pageBlockSection title="Adding Multiple Accounts" columns="1">
        <apex:pagemessages />

            <apex:pageBlockTable value="{!Acc}" var="a"  >
             <apex:variable value="{!0}" var="rowNumber" />
                <apex:column headerValue="Action">
                <apex:commandButton action="{!removeNewObject}" value=" X "  >
                <apex:param name="rowIndex" value="{!rowNumber}" />
                </apex:commandButton>
                <apex:variable var="rowNumber" value="{!rowNumber+1}"/>
                </apex:column>

                <apex:column headerValue="Account Name">
                <apex:inputField value="{!a.Name}"/>
                </apex:column>
                <apex:column headerValue="Account Type">
                 <apex:inputField value="{!a.Type}"/>
                </apex:column>
                <apex:column headerValue="Industry">
                <apex:inputField value="{!a.Industry}"/>
                </apex:column>


            </apex:pageBlockTable>
        <apex:commandLink value="Add Row" action="{!AddRow}" reRender="Refresh" />        

        </apex:pageBlockSection>

</apex:pageBlock>

</apex:form>
</apex:page>

Controller is :

public with sharing class Accountextension {
    public List<Account> Acc {get;set;}
    public Integer rowIndex { get; set; }

    public Accountextension(ApexPages.StandardController controller) {
        this.acc = new list<account>();
        this.acc.add(new Account());
    }

    public PageReference AddRow()
    {
        this.acc.add(new Account());
        return null;
    }  

    public PageReference removeNewObject(){
       Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('rowIndex'));
       system.debug('%%%%%%%%'+rowIndex);
       system.debug('%%%%%%%%'+indexVal );

       if(acc[indexVal-1].Id != null)
       acc.remove(indexVal-1 ); 
       return null;
    }
}

Screen shot is

Page Demo

When I click on delete button ("X") it gives me error like

System.NullPointerException: Argument cannot be null. error at; Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('rowIndex'));

I'm unable to understand what wrong here:

1
  • Did you ever get this one working?
    – Adrian Larson
    Sep 23, 2016 at 23:37

1 Answer 1

1

The core issue is that you need to use <apex:commandLink> here, not <apex:commandButton>. From the documentation for the <apex:param> tag:

The <apex:param> component can only be a child of the following components:

  • <apex:actionFunction>
  • <apex:actionSupport>
  • <apex:commandLink>
  • <apex:outputLink>
  • <apex:outputText>
  • <flow:interview>

I also recommend using assignTo here, but that's not mandatory to get your page working, I believe. Here's a fully working demo you should only have to modify slightly to get your own page working.

Markup

<apex:page controller="Demo">
    <apex:form>
        <apex:variable value="{!0}" var="rowNumber" />
        <apex:repeat value="{!data}" var="i">
            <ul>
                <apex:commandLink action="{!removeNewObject}" value=" X ">
                    <apex:param name="rowIndex" assignTo="{!rowIndex}" value="{!rowNumber}" />
                </apex:commandLink>
                <apex:outputText value=" {!i}" />
                
                <apex:variable var="rowNumber" value="{!rowNumber+1}"/>
            </ul>
        </apex:repeat>
    </apex:form>
</apex:page>

Code

public with sharing class Demo
{
    public List<String> data { get; private set; }
    public Demo()
    {
        data = new List<String>();
        for (Integer i = 0; i < 10; i++)
            data.add(String.valueOf(i));
    }
    
    public Integer rowIndex { get; set; }
    public PageReference removeNewObject()
    {
        data.remove(rowIndex);
        return null;
    }
}
7
  • ive refer this link for reference. forcetree.com/2015/03/… this is working perfect. same thing i've used but my code, but not working
    – Amol
    Aug 27, 2016 at 13:12
  • All I'm getting on that page are a bunch of ads that take a long time to load. No code.
    – Adrian Larson
    Aug 27, 2016 at 13:13
  • Please check again i've edited
    – Amol
    Aug 27, 2016 at 13:14
  • No, you are not implementing their code verbatim. They, too, use commandLink. I still think assignTo is cleaner, but the core issue is that you need to use commandLink, not commandButton.
    – Adrian Larson
    Aug 27, 2016 at 13:15
  • I've changed to 'commandLink ' but still unable to delete; only page is refresh.
    – Amol
    Aug 27, 2016 at 13:24

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.