0

I have a table that displays a list of Surveys

In addition, I have a button that goes to a creates a new survey, survey Questions, survey Referral links, all of which is on a single VF page.

Each survey can have multiple survey questions & referral links related to it.

I want to be able to click the name of a survey on the table and be presented with an edit page that will let me edit the survey and all related questions and referral links

Are the steps I need to do something like this?

  1. Get the Id of the survey from the table
  2. Forward to the new survey page with the survey Id
  3. Make page in edit mode
  4. Using the survey Id, query the record and all related records
  5. Assign data to proper variables on the controller
  6. Upsert records
  7. Redirect to table of survey's

I'm unsure how to do 2, 3, and 5.

Here's a visual help

Table that displays survey's

Code

SurveyList VF page

<apex:page controller="SurveyListController" sidebar="false" standardStylesheets="false">
    <head>
        <link href="{!URLFOR($Resource.assets, 'assets/bs/css/bootstrap-theme.css')}" rel="stylesheet"/>
        <link href="{!URLFOR($Resource.assets, 'assets/css/custom.css')}" rel="stylesheet"/>
    </head>

    <apex:form >
        <apex:commandButton action="{!newSurveyPage}" value="New Survey"/>
    </apex:form>

    <apex:pageBlock title="Surveys">
        <apex:pageBlockTable value="{!surveys}" var="survey" id="theTable" rowClasses="odd,even" styleClass="table table-bordered table-condensed">
                <apex:column >
                    <apex:facet name="header">Survey Name</apex:facet>
                    <apex:outputText value="{!survey.name}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">Status</apex:facet>
                    <apex:outputText value="{!survey.Survey_Status__c}"/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">Owner</apex:facet>
                    <apex:outputText value="{!survey.owner.name}"/>
                </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>

</apex:page>

SurveyListController

public class SurveyListController {
    List<SEO__CSEO_Survey__c> surveys;

    public List<SEO__CSEO_Survey__c> getSurveys() {
        if(surveys == null)
            surveys = [SELECT Id, name, owner.name, SEO__Survey_Status__c FROM SEO__CSEO_Survey__c];
        return surveys;
    }

    public PageReference newSurveyPage() {
        PageReference page = new PageReference('/apex/NewSurvey');
        page.setRedirect(true);
        return page; 
    }

    public PageReference editSurveyPage() {
        PageReference page = new PageReference('/apex/NewSurvey');
        page.setRedirect(true);
        return page;        
    }

}

NewSurvey VF page

<apex:page sidebar="false" standardStylesheets="false" controller="NewSurveyController">
    <head>
        <link href="{!URLFOR($Resource.assets, 'assets/bs/css/bootstrap-theme.css')}" rel="stylesheet"/>
        <link href="{!URLFOR($Resource.assets, 'assets/css/custom.css')}" rel="stylesheet"/>
    </head>
    <apex:outputPanel styleClass="content-wrapper">
        <apex:outputPanel styleClass="content-fluid">
    <apex:pagemessages ></apex:pagemessages>
    <apex:form>
    <apex:pageBlock title="Create New Survey" mode="edit" >

    <apex:outputPanel styleClass="space5"></apex:outputPanel>  
        <h4>Survey Setup</h4>
        <apex:outputPanel styleClass="panel panel-default">
        <apex:outputPanel styleClass="panel-body">
            <apex:outputPanel styleClass="form-group">
            <apex:outputLabel value="Survey Name" for="survey__name"/>
            <apex:inputField value="{! survey.Name }"/>        
            </apex:outputPanel>
            <apex:outputPanel styleClass="form-group">
            <apex:outputLabel value="Status" for="survey__status"/>
            <apex:inputField value="{! survey.SEO__Survey_Status__c}"/>
            </apex:outputPanel>
       </apex:outputPanel>
        </apex:outputPanel>

    <apex:pageBlockSection title="Survey Pages">
        <apex:pageBlockSection title="Welcome Page">
            <apex:pageBlockSectionItem>
                <apex:outputLabel value="Welcome Text" for="account__name"/>
                <apex:inputTextArea value="{! survey.SEO__Welcome_Text__c}"/>      
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Feedback Page">
            <apex:pageBlockSectionItem>
                <apex:outputLabel value="Question #1" for="account__name"/>
                <apex:inputText value="{! question.Name}"/> 
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem>
                <apex:outputLabel value="Testimonial Request" for="account__name"/>
                <apex:inputTextArea value="{! survey.SEO__Testimonial_Request__c}"/> 
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Thank You Page">
            <apex:pageBlockSectionItem>
                <apex:outputLabel value="Thank You Message" for="account__name"/>
                <apex:inputTextArea value="{! survey.SEO__No_Testimonial_Thank_You__c}"/> 
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem>
                <apex:outputLabel value="Thank You Message" for="account__name"/>
                <apex:inputTextArea value="{! survey.SEO__Testimonial_Thank_You__c}"/> 
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem>
                <apex:outputLabel value="Select the referral sites" for="account__name"/>
                <apex:inputTextArea value="{! referral.SEO__Referral_URL__c}"/> 
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>   
    </apex:pageBlockSection>

    <apex:commandButton action="{! save }" value="Save" />

    </apex:pageBlock>
    </apex:form>
        </apex:outputPanel>
  </apex:outputPanel>
</apex:page>

NewSurveyController

public class NewSurveyController {

    public SEO__CSEO_Survey__c            survey{get;set;}
    public SEO__CSEO_Survey_Question__c   question{get;set;}
    public SEO__CSEO_Referral_Site__c     referral{get;set;}

    public List<SEO__CSEO_Survey__c> surveys;
    public List<SEO__CSEO_Survey_Question__c> questions;
    public List<SEO__CSEO_Referral_Site__c> referrals;

    public NewSurveyController (){
        survey    = new SEO__CSEO_Survey__c();
        question  = new SEO__CSEO_Survey_Question__c();
        referral  = new SEO__CSEO_Referral_Site__c();
        surveys   = new List<SEO__CSEO_Survey__c>();
        questions = new List<SEO__CSEO_Survey_Question__c>();
        referrals = new List<SEO__CSEO_Referral_Site__c>();
    }

    public PageReference save() 
    {

            // Check to see if fields are blank
            if(String.isNotblank(survey.name)&& 
               String.isNotblank(survey.SEO__Survey_Status__c)&& 
               String.isNotblank(survey.SEO__Welcome_Text__c)&&
               String.isNotblank(question.name)&&
               String.isNotblank(survey.SEO__Testimonial_Request__c)&&
               String.isNotblank(survey.SEO__No_Testimonial_Thank_You__c)&&
               String.isNotblank(survey.SEO__Testimonial_Thank_You__c)
              )
            {   
                // If not blank, add survey to the database
                surveys.add(survey);

                Database.SaveResult[] srList = Database.insert(surveys, false);

                // Iterate through each returned result
                for (Database.SaveResult sr : srList) 
                {
                    // If successfully inserted
                    if (sr.isSuccess()) 
                    {
                        // Associate question(s) & referral(s) with created survey Id
                        question.SEO__CSEO_Survey__c = sr.getId();
                        referral.SEO__CSEO_Survey__c = sr.getId();

                        // Add question(s) & referral(s) to list
                        questions.add(question);
                        referrals.add(referral);

                        // Insert into database
                        Database.SaveResult[] srListcustB = Database.insert(questions, false);
                        Database.SaveResult[] srListcustC = Database.insert(referrals, false);
                    }
                    // Error checker if something goes wrong  
                    else {         
                        for(Database.Error err : sr.getErrors()) {
                        System.debug('The following error has occurred.');                    
                        System.debug(err.getStatusCode() + ': ' + err.getMessage());
                        System.debug('Account fields that affected this error: ' + err.getFields());
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Survey' + survey.Name + ' was not created because: '+err.getMessage()));
                        }
                    }
                }                
            }
            // If blank, say which field(s) is blank on page
            else
            {
                 if(String.isblank(survey.name)){
                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Warning,'Please enter the custom object Name. '));
                 }
                 else if(String.isblank(survey.SEO__Survey_Status__c)){
                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Warning,'Please enter the custom object B Name. '));
                 }
                else if(String.isblank(survey.SEO__Welcome_Text__c)){
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Warning,'Please enter the custom object B Name. '));
                }
                else if(String.isblank(question.name)){
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Warning,'Please enter the custom object B Name. '));
                }
                else if(String.isblank(survey.SEO__Testimonial_Request__c)){
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Warning,'Please enter text for Testimonial Request. '));
                }
                else if(String.isblank(survey.SEO__No_Testimonial_Thank_You__c)){
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Warning,'Please enter text for Thank You Message if user has not provided a testimonial. '));
                }

                 else{
                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Warning,'Please enter text for Thank You Message if user has provided a testimonial. '));
                 }
            }
    return null;
    }
}
1
  • 1
    Can you share with us the code you've written so far? – Sebastian Kessel Oct 7 '16 at 22:16
0

Assuming there is no logic that needs to happen in the current page's controller before redirecting, you can simply render the link directly on the page, e.g.

<apex:outputLink value="/apex/NewSurvey?id={!survey.ID}&param1={!survey.param__c}">
    {!survey.Name}
</apex:outputLink>

On the NewSurvey page, you can get the query string parameters with ApexPages.currentPage().getParameters(), which is a regular Map<String, String> with each query string parameter name as the key and its value as the value (e.g. getParameters().get('id') will get you the ID of record).

Alternatively, if logic has to be performed before redirecting to the next page or there are a lot/different possible parameters, you can use an apex:commandLink instead. In Visualforce:

<apex:commandLink value="{!survey.Name}" action="{!editSurveyPage}">
   <apex:param assignTo="{!selectedRecordID}" value="{!survey.ID} />
</apex:commandLink>

Apex:

public ID selectedRecordID {get;set;}

public PageReference editSurveyPage()
{
    PageReference page = new PageReference('/apex/NewSurvey');
    Map<String, String> params = page.getParameters();
    params.put('id', selectedRecordID);
    page.setRedirect(true);
    return page; 
}

I'm not sure what other information you want to include, but just add it to the page parameters before redirecting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.