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
kdaviskdavis 

Updating record with VF but Page Reference Update Failing

Hey Guys,

 

I have a requirement where: A contact already exists, they need a custom button that directs to a visual force page where I have a created a form that has a subset of fields on the contac they need to fill out. When they are done they save and need to update the contact.

 

So far I have completed:

1. Directing to the page

2. Querying the subset of contact fields in the controller that will be used in the page since we cannot use a standard controller

3. Built the page

Error 4. Updating the record using a page refrence

 

Here is the controller:

 

public class IntakeController{

public static Contact currentContact {get; set;}
public final Id cId {get; set;}


public IntakeController() {

cId = ApexPages.currentPage().getparameters().get('cid');
currentContact = new Contact();
currentContact = [SELECT
                  //All the fields
FROM Contact
WHERE Id =: cId];

}

public PageReference SaveContact(){
system.debug(currentContact);

if(currentContact != null){
update currentContact;
}

PageReference returnPage = new PageReference('/' + cId);
returnPage.setRedirect(true); 
return returnPage; 
} 
}

 

Problem is everytime I go to save I get a null value for currentContact in that debug. I am updating the values of contact Record in the page using input fields. What am I doing wrong here, and how do I get the updated values from the page? 

 

Thanks

 

ICSteveICSteve

What does your page look like? Are you using immediate="true" at all?

MoggyMoggy

hmm that looks strange to as you try to asign a brand new value to a newly created object onto a public static variable

it is either static or get/set but not both try without the key word or combine it with your sql query to get the value on it

but public static x

x = new y()

x = select... 

does not work

or you try the static combined with the keyword new and after that x =....

 

why not 

public static Contact currentContact = new Contact();
public final Id cId {get; set;}


public IntakeController() {

cId = ApexPages.currentPage().getparameters().get('cid');
currentContact = [SELECT
                  //All the fields
FROM Contact
WHERE Id =: cId];

}

Just a guess

 

 

kdaviskdavis

I removed the static from the contact variable and made it local, and that seemed to solve the issue. Any Idea why that is?