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 

Redirecting the page to a list view after deletion of data

Hi, 

I am invoking a delete action from a visualforce button by invoking JavaScript which would delete the records from the AccountShare object. I also want the page to be redirected to the Account List view page after the delete action is completed. The delete action is working perfectly but the page redirection is not happening. 

 Below is the  code:-

Javascript code:-

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}

var r = confirm("Are you sure you want delete?");
if(r == true)
{
sforce.apex.execute("RemoveFromTerritory","deleteterritory",{accountid:"{!Account.Id}"});

}


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();
           
           
               

       
               
    }


}


VF Page code:-

<apex:page standardcontroller="Account" extensions="RemoveFromTerritory">

<script>
function confirmDelete() {
var doDelete = confirm('Are you sure?');
return doDelete;
}
</script>

    <apex:form >
   
    <apex:commandButton action="{!deleteterritory}" value="Remove From Territory"/>  
   
    </apex:form>

</apex:page>
Andy BoettcherAndy Boettcher
It doesn't look like you ever actually call the method with the PageReference in it - BUT - why would you split out the methods like this?  (webservice and a regular method?)  Why not just put everything in a single method behind that "Remove From Territory" button and be done with it?  Looks like you're overengineering it?