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
max.alexander1.3945796734965325E12max.alexander1.3945796734965325E12 

Prompt user to confirm when calling VF page from custom button

HI I have following VF page 
<apex:page StandardController="Opportunity" extensions="DeleteOpportunityLIneItems" action="{!Autorun}"  >

</apex:page>

it called controller
public class DeleteOpportunityLIneItems {

    Opportunity oli;

    public DeleteOpportunityLIneItems(ApexPages.StandardController stdController){
    
        this.oli = (Opportunity)stdController.getRecord();

    } 
       
    public PageReference autoRun() {
        
        OpportunityLineItem[] objsToDelete = [SELECT id FROM OpportunityLIneItem WHERE Opportunityid = :this.oli.id];
           


        delete objsToDelete;
            
        PageReference pageRef = new PageReference('/' + Oli.id);
        pageRef.setRedirect(true);
        return pageRef;
        }  
    }

this controller deletes all Opportunity Line items, ti works just fin, is there any way i can add a prompt to ask user Are yo USure you want to delete?
If they say yes then Delete if they say No then go back to the Opportunity page, i tried numebr us thisng but i cna get th eprobpt appear stright away, i moves me to VF page with command button then it prompts me if i'm sure, is ther any way to go directly to prompt and hten execute controller?
Best Answer chosen by max.alexander1.3945796734965325E12
Vinit_KumarVinit_Kumar
Max,

Got it what you are looking for,below should work for you.You should create a custom button with below attributes :-

Display Type : Detail Page Button

Behavior : Execute Javascript

Content Source : Onclick Javascript 

and in the text box copy the below JS code ,
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
    {!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")} 
    var x;
    if (confirm("Press a button!") == true) {
        x = "OK";
    } else {
        x = "Cancel";
    }
    if(x == 'Cancel'){ 
      window.location.replace('/{!Account.Id}')
    } 

else { 
window.location.replace('/apex/AccountDetail?id={!Account.Id}') //replace your VF page here
}
If this helps,please mark it as best answer to help others :)

All Answers

Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
try this with javascript.use this kind of function in VF page:
<script>
function popup()
{
var p = prompt('Please press yes or no to delete' );
if (p == 'yes')
{
alert("oppurtunity  going to delete");
}
else
{   
window.location="http://www.newlocation.com";
}
</script>

and in the commandbutton
<apex:commandbutton onclick="popup" Value="Delete">
 If it helps mark it as best answer
max.alexander1.3945796734965325E12max.alexander1.3945796734965325E12
tried it, it open new page with another button to click inspead of simply giving me warning
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
have you tried with confirm() method?
Vinit_KumarVinit_Kumar
You can create a JS function like below :-

<script type=”text/javascript”>
function confirmDelete() {
return confirm(‘Are you sure you want to Delete?’);
}

and then use this function on Onclick event of commandbutton something like below :-

<apex:commandbutton action=”{!<your commnadbutton method>}” onClick=”if (!confirmDelete()) return false;” reRender=”myPageBlock” />

If this helps,please mark it as best answer to help others :)
Deepak Kumar ShyoranDeepak Kumar Shyoran
Yes they are some ways to do that and one of them is given below

<apex:page StandardController="Opportunity" extensions="DeleteOpportunityLIneItems"  >

<script>
var r = confirm('Are you sure you want to delete');
if (r == true) {
   callAutorun() ;
}
</script>
<apex:form>
<apex:actionFunction action="{!Autorun}" name="callAutorun" />
</apex:form
</apex:page>

this will show to the confirm box and once you selected yes it will call you Autorun method.

Please mark my answer as a solution to your question if it solves your problem.

Ravi NarayananRavi Narayanan
<apex:commandButton value="TEST" onclick="return confirm('Are You Sure you want to continue (Y/N)?');" action="{!TEST}"
max.alexander1.3945796734965325E12max.alexander1.3945796734965325E12
if i do it with command button it works, but it redirects me to another page with the button then wheni click on it it just stays there
max.alexander1.3945796734965325E12max.alexander1.3945796734965325E12
i tried this
<apex:page StandardController="Opportunity" extensions="DeleteOpportunityLIneItems"  >

<script>
var r = confirm('Are you sure you want to delete');
if (r == true) {
   callAutorun() ;
}
</script>
<apex:form>
<apex:actionFunction action="{!Autorun}" name="callAutorun" />
</apex:form
</apex:page>

gives me the alert whic is perfect but does not executte !AutoRun for some reason
max.alexander1.3945796734965325E12max.alexander1.3945796734965325E12
tried this
alert('Do you want to proceed?');

If you want to pass id to some page use the below one,

location.href="/apex/YourPageName?EmailtempId={!Opportunity.Id}"

get the warning 
but get this error

Formula Expression is required on the action attributes.
max.alexander1.3945796734965325E12max.alexander1.3945796734965325E12
Have tried this
<apex:page StandardController="Opportunity" extensions="DeleteOpportunityLIneItems" >
<script>

<script type=”text/javascript”>
function confirmDelete() {
return confirm(‘Are you sure you want to Delete?’);
}
</script>
<apex:form>

<apex:commandButton value="Delete All Line Items" action="{!Autorun}"  onclick="if (!confirmDelete()) return false;" reRender="myPageBlock"  /> 
</apex:form>


</apex:page>

it shows another page with the button, when i press it and say yes it delete teh line items, but if i press cancell it does nothing
what i need is this button not to show at all as it is extra step that i dont need
Vinit_KumarVinit_Kumar
This works in my org with confirm popup,Once I click on OK it redirects,else it does not:-

VF page :-

<apex:page StandardController="Account" extensions="redirecttonewPage">
<script>

<script type=”text/javascript”>
function confirmDelete() {
return confirm(‘Are you sure you want to redirect?’);
}
</script>
<apex:form>

<apex:commandButton value="redirect" action="{!navigateToEmail}" onclick="if (!confirmDelete()) return false;" reRender="myPageBlock"  /> 
</apex:form>


</apex:page>

Apex Class :-

public class redirecttonewPage
{

    public String accId {get;set;}

    public redirecttonewPage(ApexPages.StandardController controller) {
    
    //accId=ApexPages.currentpage().getParameters().get('id');
    accId='0019000000Lzhw3';
    system.debug('@@@@@@@' + accId);

    }




public PageReference navigateToEmail(){
    
        PageReference page = new Pagereference('/apex/AccountDetail?Id='+accId);
        page.setRedirect(true);
        return page;
        }
        
        }


max.alexander1.3945796734965325E12max.alexander1.3945796734965325E12
it does but i have detail page button on the opportunity object, they you have it it will work perfectly if it woud have been stand alone wVF page
Vinit_KumarVinit_Kumar
Max,

Got it what you are looking for,below should work for you.You should create a custom button with below attributes :-

Display Type : Detail Page Button

Behavior : Execute Javascript

Content Source : Onclick Javascript 

and in the text box copy the below JS code ,
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
    {!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")} 
    var x;
    if (confirm("Press a button!") == true) {
        x = "OK";
    } else {
        x = "Cancel";
    }
    if(x == 'Cancel'){ 
      window.location.replace('/{!Account.Id}')
    } 

else { 
window.location.replace('/apex/AccountDetail?id={!Account.Id}') //replace your VF page here
}
If this helps,please mark it as best answer to help others :)
This was selected as the best answer
max.alexander1.3945796734965325E12max.alexander1.3945796734965325E12
thank you very much this worked perfectly