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
Amit Singh.ax1042Amit Singh.ax1042 

How to redirect to master page?

hi friends,

i have created two objects 1)Job 2)Job Task

and there is a master detail relationship between Job and JobTask...

i have created visualforc page to create new Job and Jobtask as well...

i am getting problem while deleting Job Task from its Detail page... Because after deleting it goes to standard list of jobtask...

i want that after deleting any jobtask it should go back to respected Job... i have written apex for this but not able to fetch id of master record...

could any one provide me code to override delete of job task

 

here is my code but not able to retrieve id ob JOB

 

 

public class DeleteJobTask
{
    private final JobTask__c objJT;
    public DeleteJobTask(ApexPages.StandardController controller)
    {
    this.objJT=(Holiday__c)controller.getrecord();
    }
     public PageReference checkDelete()
     {
        try
        {
        Database.delete(objJT);       
        }
        catch(System.DMLException e)
        {
        return null;
        }
        PageReference p =  new Pagereference('/apex/JobDetailPage?id=);
        p.setRedirect(true);
        return p;
     }

}

 

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

hmm, field is not been added in the query in this line : Class.DeleteSpecItem.checkDelete: line 18,.

so either add the field in that query or I'm not sure here but give below code a try:

 

public class DeleteJobTask
{
    public Id JobId;
    private final JobTask__c objJT;
    public DeleteJobTask(ApexPages.StandardController controller)
    {
    this.objJT=(Holiday__c)controller.getrecord();
    }
     public PageReference checkDelete()
     {
       JobId = [Select Id, Job__c from Holiday__c where Id =:objJT.Id limit 1].Job__c;
        try
        {
        Database.delete(objJT);       
        }
        catch(System.DMLException e)
        {
        return null;
        }
        PageReference p =  new Pagereference('/apex/JobDetailPage?id='+JobId);
        p.setRedirect(true);
        return p;
     }
 

All Answers

Rahul SharmaRahul Sharma

This can be done, Before deleting a JobTask, store the Id of its Job and after deleting, redirect to that id.

Like:

 

public class DeleteJobTask
{
    public Id JobId = new Id();
    private final JobTask__c objJT;
    public DeleteJobTask(ApexPages.StandardController controller)
    {
    this.objJT=(Holiday__c)controller.getrecord();
    }
     public PageReference checkDelete()
     {
        if(objJT != null && objJT.Job__c!= '') 
 JobId = objJT.Job__c; // Replace Job__c with actual lookup field name. try { Database.delete(objJT); } catch(System.DMLException e) { return null; } PageReference p = new Pagereference('/apex/JobDetailPage?id='+JobId); p.setRedirect(true); return p; } }

 Hope it helps.

Amit Singh.ax1042Amit Singh.ax1042

i am getting error like

Compile Error: Type cannot be constructed

Rahul SharmaRahul Sharma

try this:

 

public class DeleteJobTask
{
    public Id JobId;
    private final JobTask__c objJT;
    public DeleteJobTask(ApexPages.StandardController controller)
    {
    this.objJT=(Holiday__c)controller.getrecord();
    }
     public PageReference checkDelete()
     {
       JobId = objJT.Job__c; // Replace Job__c with actual lookup field name.
        try
        {
        Database.delete(objJT);       
        }
        catch(System.DMLException e)
        {
        return null;
        }
        PageReference p =  new Pagereference('/apex/JobDetailPage?id='+JobId);
        p.setRedirect(true);
        return p;
     }

}

 

Amit Singh.ax1042Amit Singh.ax1042

now when i am going to delete getting this error

 

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Holiday__c.Job__c
Class.DeleteSpecItem.checkDelete: line 18, column 14 External entry point 

Rahul SharmaRahul Sharma

hmm, field is not been added in the query in this line : Class.DeleteSpecItem.checkDelete: line 18,.

so either add the field in that query or I'm not sure here but give below code a try:

 

public class DeleteJobTask
{
    public Id JobId;
    private final JobTask__c objJT;
    public DeleteJobTask(ApexPages.StandardController controller)
    {
    this.objJT=(Holiday__c)controller.getrecord();
    }
     public PageReference checkDelete()
     {
       JobId = [Select Id, Job__c from Holiday__c where Id =:objJT.Id limit 1].Job__c;
        try
        {
        Database.delete(objJT);       
        }
        catch(System.DMLException e)
        {
        return null;
        }
        PageReference p =  new Pagereference('/apex/JobDetailPage?id='+JobId);
        p.setRedirect(true);
        return p;
     }
 
This was selected as the best answer
Amit Singh.ax1042Amit Singh.ax1042

Thank you Rahul, its working now... You are great dude :)