1

I've been searching the net as I could but could not resolve my problem.

In my VFPage I have this column with "Cancel" commandLink that should remove assigned row for good from DB. What happens is that the row only disappears but comes back with page reload.

What am I missing?

Controller:

public class myOrdersController {

    Id loggedUserid = UserInfo.getUserId();
    public List<Order__c> currentOrder {get; set;}
    public Integer rowNumber {get; set;}



    public myOrdersController() {
        String sql = 'SELECT Id, Name, OwnerId, LastModifiedById, Status__c, Payment__c, Delivery__c, Asset__r.Product2Id FROM Order__c WHERE OwnerId =: loggedUserid';
        currentOrder = Database.query(sql);

    }


    public PageReference newOrder() {
        PageReference page = new PageReference('/apex/NewOrderPage');
        return page;
    }

    // Problematic part
    public void cancel() {
        rowNumber = Integer.valueOf(ApexPages.currentPage().getParameters().get('index'));
        currentOrder.remove(rowNumber);
    }
}

And VFPage form:

<apex:form >
        <apex:variable var="rowNumber" value="{!0}"  />
        <apex:pageBlock title="Order List" id="orderForm">
        <apex:variable var="rowNumber" value="{!0}" />
        <apex:pageBlockSection >
        <h1>Viewing as: {! $User.FirstName & ' ' & $User.LastName } </h1>
        </apex:pageBlockSection>
        <apex:pageBlockTable value="{! currentOrder }" var="o">

            <apex:column headerValue="No." style="width:20px; text-align:center;">
                <apex:outputText value="{0, number, ###}" style="text-align:center;">
                    <apex:param value="{!rowNumber+1}" />
                </apex:outputText>
            </apex:column>
            <apex:column headerValue="Action" style="width: 180px;">
                <apex:commandLink value="Edit"  />&nbsp;|&nbsp;

                <apex:commandLink value="Cancel" action="{!cancel}" reRender="orderForm,temp" rendered="{!rowNumber>=0}">
                    <apex:param value="{!rowNumber}" name="index" />
                </apex:commandLink>
                <apex:variable var="rowNumber" value="{!rowNumber+1}"/>


            </apex:column>
            <apex:column value="{!o.Name}" />
            <apex:column value="{!o.OwnerId}" />
            <apex:column value="{!o.LastModifiedById}" />
            <apex:column value="{!o.Payment__c}" />
            <apex:column value="{!o.Delivery__c}" />
            <apex:column value="{!o.Status__c}" />
        </apex:pageBlockTable><br /><br />

            <apex:commandButton value="New order" action="{!newOrder}"/>


    </apex:pageBlock>
</apex:form>

Thank you kindly for any hint. Just to point out, this row-delete solution comes from third party website as I could not figure it out myself.

2
  • Your cancel method doesn't include any code to delete rows from the database, only removes from the list that exists in memory. Is the above code complete and accurate?
    – Mark Pond
    Mar 12, 2019 at 18:41
  • Yes, the code is, unfortunately, complete. I tried to include some delete statements, but that either did not work at all or threw error.
    – user65177
    Mar 12, 2019 at 18:42

1 Answer 1

3

This solution doesn't allow for scenarios such as 'undo'... but it'll delete the record removed from the table's collection, immediately.

public void cancel() {
    rowNumber = Integer.valueOf(ApexPages.currentPage().getParameters().get('index'));

    Order__c orderToDelete = currentOrder.remove(rowNumber);

    // immediately delete the row removed from the collection
    delete orderToDelete;
}
1
  • Oh my... so simple and yet I could not figure it out. Growing smarter each day! Thank you very much, Mark.
    – user65177
    Mar 12, 2019 at 18:57

You must log in to answer this question.