function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TheCustomCloudTheCustomCloud 

Removing an sObject from a List when the sObject has a required field

It is a pretty simple scenario so I will summarize it.   I cannot find anything to explain this behaviour so I am hoping some of you Apex geniuses can tell me that I am just making a simple mistake.

 

The users enters input into a field and then clicks a button.  The button calls the controller to create another sObject and it is displayed on the VF page using a repeat.  Another button is suppose to delete the last item from the list.  The field the user is inputting is a master-detail and therefore is a required field but why do I get the "Error: You must enter a value" when I am trying to just remove the last sObject from the list?


Here is the code.

 

<apex:repeat value="{!requests}" var="r">
                    <apex:pageBlockSection columns="1">
                    <apex:inputField value="{!r.Contact__c}"/>
                    </apex:pageBlockSection>
</apex:repeat>

 

public List<Attendee__c> requests {get; set;}

 

public void addAttendee() {
        attendeesList.add(new Attendee__c(Meeting__c = meet.Id));
}

 

public void removeAttendee() {
        if(attendeesList.size() > 0){
            attendeesList.remove(requests.size()-1);
        }
}

 

 

Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
WizradWizrad

Could we see more of the code?  Specifically the button you click and what it rerenders.

 

Nevertheless the solution will probably involve adding immediate="true" to your commandButton or use of an actionRegion tag.

All Answers

WizradWizrad

Could we see more of the code?  Specifically the button you click and what it rerenders.

 

Nevertheless the solution will probably involve adding immediate="true" to your commandButton or use of an actionRegion tag.

This was selected as the best answer
Shashikant SharmaShashikant Sharma

Do the second action of deleting item from actionFunction and pass any parameter if you want to set any property , set immediate="true" so no setter is called for any property and in action of actionFunction remove item from listand  rerender your repeat. Will definitly work. Please ask if any issues with using actionFunction.

TheCustomCloudTheCustomCloud

Worked perfect! 

 

Thank you!