+ Start a Discussion
Ben Merton 15Ben Merton 15 

Add a 'choice' button and then delete a related list if yes or continue if no

I have a class wherein an error is currently thrown if the records contained in a related list already exist when a clone button is pressed:
 
//////Class before

List<Material_Request__c> womaterialrequest = [select Indent_Quantity__c, Good_Service__c from Material_Request__c where Work_Order__c = :workorderid];
        if(womaterialrequest.size() != 0)
        {
        ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'Indents already on the Work Order.  Please delete before Adding Indent Again'));
        return null;
        }

///////Class after
I am looking to convert this to a popup warning that states "Overwrite Existing Material Requests?", with a Yes/No choice.

If the user selects yes, then all the existing records in the list should be deleted, before the rest of the class is performed.  If they select No, then it should return to the existing record without any changes.

Any ideas?
 
Abhishek BansalAbhishek Bansal
Hi Ben,

Yes, this can easily be acheived by following the below mentioned steps :

1. Add an onclick event on your "Clone" button.
2. Call a javascript function in this onclick event.
3. In this javascript function show a confirmation window to user.
4. Now based on the selection of User on that confirmation window you can perform the further actions by calling an action function from your JS function.

I am providing you a sample code below which can help you to get started on this :
<apex:page>
	<script>
		function showConfirmationWindow(){
			if(window.confirm('Overwrite Existing Material Requests?')){
				callActionFunctio();//Call your controller method with help of this action function
			}
			else{
				callOtherActionFunction();//Call your controller method with help of this action function
			}
		}
	</script>
	
	<apex:form>
	-----------
	-----------
	-----------
	-----------
		<apex:commandButton value="Clone" onclick"showConfirmationWindow" />
	-----------
	-----------
	-----------
	-----------
	</apex:form>
</apex:page>
Please let me know if you need more information or help on this.

Thanks,
Abhishek Bansal.
Ben Merton 15Ben Merton 15
Abhishek, thanks but I am a little confused.  Your code here seems to be marked with Visualforce tags, but you are saying that I should add an On Click event on the CloneButton in Javascript.

Can you confirm the process?  Do I not need to point the button to a VF page if I am to use this?
 
Ben Merton 15Ben Merton 15
Also, this won't then check if there are existing records before putting up the screen.

The flow should be:

1.  Press Button
2.  Check if there are existing records
3.  If there aren't existing records, run the controller to execute the clone
4.  If there are existing records, give a choice "Yes" or "No"
5.  If Yes, run the controller to execute the clone]
6.  If no, close the box and don't do anything

Ben
Abhishek BansalAbhishek Bansal
Hi Ben,

Sorry for responding you so late as i was busy with new year celebrations.
The code written between <script> and </script> is define as the javascript code for the VF page.
Yes i completely understand your flow and you can easily achieve this by the steps mentioned by me in first post.

Fist of all when you press the button than an action will be called which will check for existing records and if there are not any than it will call the clone method.
But if there are any existing records than it will set a boolean variable as true and return to the VF page which will than call JS method that will show the popup.

Sample code :
<apex:page>
<script>
function showPopup(isPresent){
if(isPresent){
var userResponse = window.confirm('Write what you want');
//Based on userResponse you can proceed further
}
}
</script>
<apex:commandButton value="Clone" action="(!checkExRecords}" oncomplete="showPopup('{!isExistingRecordPresent}')"/>
</apex:page>

If you want more help on this than please share your VF page and controller class code here or contact me on my gmail or skype :
Gmail : abhibansal2790@gmail.com
Skype : abhishek.bansal2790

Wish you a very Happy New Year :)

Thanks,
Abhishek Bansal.