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
chercher 

Showing an alert box in a visualforce page

HI,

I have a custom button "Remove from Territory", which when clicked deletes data, I want the user to get a promopt asking "Are you sure you want to RemoveFromTerritory", if the user clicks Yes only then the action should be executed. Thanks in advance.. 

VF Page Code:-


<apex:page standardcontroller="Account" extensions="RemoveFromTerritory" action="{!deleteterritory}">   

</apex:page>


Controller Code:-

global class RemoveFromTerritory {

public static List<UserTerritory> UserList{get;set;}

public RemoveFromTerritory (ApexPages.StandardController cont){
   
UserList=new List<UserTerritory>();
       
   }
  
  
  // public String u {get; set;}

//u = Userinfo.getUserId();

String UserId=Userinfo.getUserId();

static pagereference movetonext()
{


pagereference pageref= new pagereference('/servlet/servlet.Integration?lid=01rA0000000RGei&ic=1');

pageref.setRedirect(true);

return pageref;
}

  webservice static void deleteterritory(id accountid){
  
   /*if(apexpages.currentpage().getParameters().get('id')!=null)
       accountid=apexpages.currentpage().getParameters().get('id');*/
     
    /*    system.debug('@@@'+accountid);
        Account acc=[select id from Account where id=:accountid];
       
           
        UserList=[Select UserId,TerritoryId from UserTerritory where UserId=:Userinfo.getUserId()];
       
        //List<Group> GroupList=new List<Group>([Select Id from Group
            //                                where RelatedId =:UserList.TerritoryId]);
           
           
            Set<Id> TerritoryIds=new Set<Id>();
           
           
            for(UserTerritory ust: UserList){
               
                TerritoryIds.add(ust.TerritoryId);
               
               
            }
           
            List<Group> GroupList=[Select Id from Group
                                                   where RelatedId IN:TerritoryIds
                                                   and Type='Territory'];
                                                  
            Set<Id> GroupIds=new set<Id>();                                   
                                                  
            for(Group g: GroupList){
               
                 GroupIds.add(g.id);
                       
            }                                     
               
               
        AccountShare AccSh=[Select Id from AccountShare where UserorGroupId IN: GroupIds and AccountId=:acc.id];
                                                  
            ///AccountShare AccSh= [Select Id from AccountShare where UserorGroupId=:GroupList];
           
            delete AccSh;
           
              movetonext();    */
    }
   
   
   
    public pagereference deleteterritory(){
     
      String accountid;
     
      accountid=apexpages.currentpage().getParameters().get('id');
     
      system.debug('@@@'+accountid);
        Account acc=[select id from Account where id=:accountid];
       
           
        UserList=[Select UserId,TerritoryId from UserTerritory where UserId=:Userinfo.getUserId()];
       
        //List<Group> GroupList=new List<Group>([Select Id from Group
            //                                where RelatedId =:UserList.TerritoryId]);
           
           
            Set<Id> TerritoryIds=new Set<Id>();
           
           
            for(UserTerritory ust: UserList){
               
                TerritoryIds.add(ust.TerritoryId);
               
               
            }
           
            List<Group> GroupList=[Select Id from Group
                                                   where RelatedId IN:TerritoryIds
                                                   and Type='Territory'];
                                                  
            Set<Id> GroupIds=new set<Id>();                                   
                                                  
            for(Group g: GroupList){
               
                 GroupIds.add(g.id);
                       
            }                                     
               
               
        AccountShare AccSh=[Select Id from AccountShare where UserorGroupId IN: GroupIds and AccountId=:acc.id];
                                                  
            ///AccountShare AccSh= [Select Id from AccountShare where UserorGroupId=:GroupList];
           
            delete AccSh;
           
            pagereference pageref= new pagereference('/servlet/servlet.Integration?lid=01rA0000000RGei&ic=1');

pageref.setRedirect(true);

return pageref;
    }
}


Manish VermaManish Verma
Make use of javascript confirm dialog box in your VF page button's onclick event.
chercher
I tried using the onlick alert in the VF Page code below, but still the alert box is not showing up. 

<apex:page standardcontroller="Account" extensions="RemoveFromTerritory" action="{!deleteterritory}">

     
           
                <apex:form >
                 <apex:commandButton value="click to confirm" onclick="return confirm('are you want to remove  the Account from the territory');"/>
                 </apex:form>
         

</apex:page>
Manish VermaManish Verma
Hello cher

The only thing you are getting wrong here is "you are using action of page". The page action method is very first method called after constructor of controller.

Below is the updated code:

<apex:page standardcontroller="Account" extensions="RemoveFromTerritory">
<apex:form >
<apex:commandButton value="click to confirm" action="{!deleteterritory}" onclick="return confirm('are you want to remove  the Account from the territory');"/>
</apex:form>
</apex:page>

Let me know if you still face issues, Happy coding!!
chercher
Hi Manish, 

I tried the updated code but still the alert box is not showing up. 
Manish VermaManish Verma
Hi It will work fine. Please make sure that your browser's setting for javascript is enabled. *Thanks & Regards* *Manish Verma*
Naveen KNNaveen KN
Implement custom dialog box in salesforce lightning 

https://www.codekiat.com/2019/07/confirm-dialog-in-salesforce-lightning.html

--
Naveen K N