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
Nidhi Sharma 17Nidhi Sharma 17 

Redirection to particular 'Order' record

I have an action 'hello1' on an apex command button 'Cancel'. On clicking Cancel I want to navigate back to the particular Order record whose survey record I am capturing at present. URL of the current survey page is 
https://c.ap2.visual.force.com/apex/TakeSurvey?id=a0R28000000ChcAEAS&cId=801280000004CYc

VF Code
<apex:page standardcontroller="Survey__c" extensions="ViewSurveyController" cache="false" sidebar="false" showheader="false">
<apex:commandButton action="{!hello1}"  value="Cancel"/>
</apex:page>

Apex Class
public PageReference hello1() {
        orderId = Apexpages.currentPage().getParameters().get('cId');
        PageReference reference=new PageReference('https://c.ap2.visual.force.com/801/o?{!Order.Id}=orderId');
        reference.setRedirect(true);
        return reference; 
    }

But 
'https://c.ap2.visual.force.com/801/o?{!Order.Id}=orderId'
is not navigating me back to particular record, rather to the whole list of Order Records. Kindly help.
 
Best Answer chosen by Nidhi Sharma 17
Puru AnnamalaiPuru Annamalai
Hi,

I think you can just use the /<<Id>> to redirect to the particular record detail page.

public PageReference hello1() {
orderId = Apexpages.currentPage().getParameters().get('cId');
PageReference reference=new PageReference('/'+orderId);
reference.setRedirect(true);
return reference;
}


 

All Answers

Puru AnnamalaiPuru Annamalai
Hi,

I think you can just use the /<<Id>> to redirect to the particular record detail page.

public PageReference hello1() {
orderId = Apexpages.currentPage().getParameters().get('cId');
PageReference reference=new PageReference('/'+orderId);
reference.setRedirect(true);
return reference;
}


 
This was selected as the best answer
Nidhi Sharma 17Nidhi Sharma 17

Hi Puru Annamalai,

It worked well.

I am facing another problem. This is a VF page linked to Surveys. And somewhere in the code I want to change field of the specific order record which it is related to.

Like, after clicking on submit button I get redirected back to the page with an updated picklist value there 'Survey taken'. Action on button is 'submitResults'.

 

public void submitResults()
    {
        List <SurveyQuestionResponse__c> sqrList = new List<SurveyQuestionResponse__c>();
        
        for (question q : allQuestions)
        {
            SurveyQuestionResponse__c sqr = new SurveyQuestionResponse__c();
            if (q.renderSelectRadio == 'true')
            {
                
                if (q.required &&  (q.selectedOption == null || q.selectedOption == ''))
                {
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please fill out all required fields'));
                    //return;
                }
                
                if (q.selectedOption == null || q.selectedOption == '')
                {
                    sqr.Response__c = '';
                }
                else
                {
                    sqr.Response__c = q.singleOptions.get(Integer.valueOf(q.selectedOption)).getLabel();
                }
                sqr.Survey_Question__c = q.Id;
                sqrList.add(sqr);
            }
            else if (q.renderFreeText == 'true')
            {
                if (q.required && q.choices == '')
                {
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please fill out all required fields'));
                    //return;
                }
                System.debug('*****Select Radio ' + q.choices);
                
                sqr.Response__c = q.choices;
                sqr.Survey_Question__c = q.Id;
                sqrList.add(sqr);
            }
            else if (q.renderSelectCheckboxes == 'true')
            {
                if (q.required && (q.selectedOptions == null || q.selectedOptions.size() == 0))
                {
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please fill out all required fields'));
                  //  return;
                }
                
                for (String opt : q.selectedOptions)
                {
                    sqr = new SurveyQuestionResponse__c();
                    if (opt == '' || opt == null)
                    {
                        sqr.Response__c = '';
                    }               
                    else
                    {   
                        sqr.Response__c = q.multiOptions.get(Integer.valueOf(opt)).getLabel();
                    }
                    sqr.Survey_Question__c = q.Id;
                    sqrList.add(sqr);
                }
            }
            else if (q.renderSelectRow == 'true')
            {
                if (q.required && (q.selectedOption == null || q.selectedOption == ''))
                {
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please fill out all required fields'));
                 //   return;
                }
                
                if (q.selectedOption == null || q.selectedOption == '')
                {
                    sqr.Response__c = '';
                }
                else
                {
                    sqr.Response__c = q.rowOptions.get(Integer.valueOf(q.selectedOption)).getLabel();
                }
                sqr.Survey_Question__c = q.Id;
                sqrList.add(sqr);
            }
            
             
        }
        

        
        if(AddSurveyTaker())
        {
            for (SurveyQuestionResponse__c sqr : sqrList)
            {
                sqr.SurveyTaker__c = surveyTakerId;
            }
            insert sqrList;
        //    init();
            
        //    thankYouRendered=true;             
                 
        }
        
       //     PageReference pageRef = new PageReference('/801?id=orderId');
       //     pageRef.setRedirect(true);
       //     return pageRef;
          
    }
Puru AnnamalaiPuru Annamalai
Hi Nidhi Sharma 17,

So you want to redirect from this vf page to its order page with a updated picklist field value in the order record right ?

so you can do something like below

//Constructor
public ViewSurveyController() {
orderId = Apexpages.currentPage().getParameters().get('cId');
}

//SubmitResults method  - changed return type from void to pageReference
public pageReference submitResults() {
Order__c order = new Order__c(Id = orderId, picklistField = 'SomeValue');
update order;
pageReference pRef = new PageReference('/'+orderId);
return pRef;
}

if it answers your question, kindly mark it as best answer
Nidhi Sharma 17Nidhi Sharma 17
Hi Puru Annamalai,

I got the code working. But approach was just a bit different. Instead of creating a new record on order I looked for existing record based on a condition and updated it.
 
Order o = [SELECT Status FROM Order WHERE Id =:orderId];
  o.Status='Assessment Done';
   update o;

Thank you for all the help :)