2

I have been working on this for a day too long and I do not know why I'm not getting it. It should be the simplest thing..

A user can review a thing. All I want them to do is make a rating 1-5 and write a review. Then I will add all of the items I need to be able to pull the review with the correct info later.

This saves everything on the screen but doesn't pick-up the other items I want.

Where am I going wrong?

So:

<apex:form >
<apex:pageBlock title="Hey {!$User.FirstName}, why not review this {!LIType}?" mode="edit">

    <apex:pageBlockButtons >
        <apex:commandButton value="Add Review" action="{!saveReview}"/>
    </apex:pageBlockButtons> 
    <apex:pageBlockSection title="Review Information" columns="1">
        <!-- div>
            <div id="slider"></div>
            <div id="value">3</div>
            <div id="value2">3</div>
        </div-->
        <div>
            <apex:inputfield required="True" styleclass="ratingA" id="rating" value="{!Learning_Review__c.Rating__c}"/></div>
        <div>
            <apex:inputfield id="title" value="{!Learning_Review__c.Review_Title__c}"/></div>
        <div>
            <apex:inputfield id="review" value="{!Learning_Review__c.Review__c}"/></div>
        <div>
            <apex:inputfield id="anon" value="{!Learning_Review__c.isAnon__c}" label="Make Comments Anonymous"/></div>
    </apex:pageBlockSection>

</apex:pageBlock>

That's the page, here is the controller:

public with sharing class LearningReviewPageController {

    public Learning_Review__c newReview {get; set;}
    Learning_Review__c newReview;
    private Learning_Review__c LearningReviewSave {get; set;}
    public Id currentUserId { get; set; }
    public Id learningItemID { get; set; }
    public Id AssignmentID { get; set; }
    public String LIType { get; set; }
    ApexPages.standardController master_sc = null;

    public PageReference initReviewPage() {
        currentUserId = userInfo.getUserId();
        learningItemID = ApexPages.currentPage().getParameters().get('Lid');
        AssignmentID = ApexPages.currentPage().getParameters().get('assignmentid');
        LIType = ApexPages.currentPage().getParameters().get('type');

        system.debug(' \n ----------------------> learningItemID ' + learningItemID);
        system.debug(' \n ----------------------> AssignmentID ' + AssignmentID);
        system.debug(' \n ----------------------> LIType ' + LIType);

        return NULL;
    }

    public LearningReviewPageController(apexpages.Standardcontroller con){
        master_sc = con;
        newReview = new Learning_Review__c();
    }

    public PageReference saveReview(){ 

        system.debug(' \n ----------> learningItemID 1' + learningItemID);
        system.debug(' \n ----------> AssignmentID 1' + AssignmentID);
        system.debug(' \n ----------> LIType 1' + LIType);

        system.debug(' \n -------------------------------> Here');
        system.debug(' \n -------------------------------> currentUserId = ' + currentUserId);
        system.debug(' \n -------------------------------> AssignmentID = ' +AssignmentID);
        system.debug(' \n -------------------------------> LIType = ' +LIType);
        system.debug(' \n -------------------------------> newReview = ' +newReview);

        /*LearningReviewSave = new Learning_Review__c (
            rating__c = newReview.Rating__c,
            Review__c = newReview.Review__c,
            Review_Title__c = newReview.Review_Title__c,
            isAnon__c = newReview.isAnon__c,
            Reviewer__c = currentUserId,
            isApproved__c = FALSE,
            Availability__c ='Internal',
            Assignment_del__c = AssignmentID            
        ); **not doing this as it doesn't work** */ 
        newReview.Reviewer__c = currentUserId;
        newReview.isApproved__c = FALSE;
        newReview.Availability__c ='Internal';
        newReview.Assignment_del__c = AssignmentID;

        if(LIType == 'Module'){
            newReview.Module__c = learningItemID;
        }else if(LIType == 'Course'){
            newReview.Course__c = learningItemID;
        }else if(LIType == 'Learningpath'){
            newReview.Learning_Path__c = learningItemID;
        }else if(LIType == 'Curriculum'){
            newReview.Curriculum__c = learningItemID;
        }   

        try{
            master_sc.save();
        }catch (Exception e) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, 'Failed to save'));
        }
        system.debug(' \n -------------------------------> newReview = ' +newReview);
        return null;
    }
}
4

Looks like you're calling save on the standard controller, but you're modifying the newReview record, which the standard controller does not have an instance of.

To fix this, in your constructor, change newReview to reference the standard controller's getRecord() call:

public LearningReviewPageController(apexpages.Standardcontroller con){
    master_sc = con;
    newReview = (Learning_Review__c)con.getRecord();
}

This should work regardless of whether you're using this Visualforce page for a new or existing record.

1
  • 2
    @guy He doesn't necessarily have to use the getRecord call to include those fields as long as the record is instantiated in the controller's constructor, but I did update my answer to reflect your comment. Thanks. Apr 16 '14 at 20:49

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.