+ Start a Discussion
JWykelJWykel 

Record Not Updating Outside Of Page

I have this bit of code:
public AccountSummary__c Summary{ 
        get{
            if(AccountId == null){
                return new AccountSummary__c();
            }
            
            AccountTrigger.ProcessAccountSummary(AccountId); //Processes and upserts the record
            //SELECT the record that was processed and upserted above
            return Database.query('SELECT ' + SelectStar + ' FROM AccountSummary__c WHERE AccountId__c = :AccountId');
        }
    }
The page that uses this Summary record looks something like this:
<apex:pageBlock >
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem ><apex:outputLabel value="Tests {!MonthNames[15]}"/><apex:outputText value="{!Summary.T15MA__c}"/></apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem ><apex:outputLabel value="Patients {!MonthNames[15]}"/><apex:outputText value="{!Summary.P15MA__c}"/></apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem ><apex:outputLabel value="Tests {!MonthNames[14]}"/><apex:outputText value="{!Summary.T14MA__c}"/></apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem ><apex:outputLabel value="Patients {!MonthNames[14]}"/><apex:outputText value="{!Summary.P14MA__c}"/></apex:pageBlockSectionItem>
...

When I open the page, all fields are correctly calculated and viewed. I view the debug logs and it shows 1 DML statement/row and shows the upsert with no errors. The fact that it does the upsert and then does a SELECT from the database for the record would lead me to believe the record was updated in the database.

However, this is not the case! When I view the record directly or via a report, the fields are not updated; they remain at their previous values.

If I run the AccountTrigger.ProcessAccountSummary(AccountId); via Execute Anonymous, the record gets updated correctly and shows in reports/view correctly.

Has anyone else experienced this? Does it have something to do with 'when' this code is called, in that it is called in a 'get' of a property for a VF page? How can that 'get' SELECT from the database the correct values when the database does not appear to get updated with correct values?
Best Answer chosen by JWykel
pconpcon
I am surprised that you do not get the DML exception in the non-getSummary() method.  I think that is a bug.  If you want it to do an update, you have to do it in a non get/set context of the visualforce page.  Such as the action call or as part of a button press.

All Answers

JWykelJWykel

dditional tests:

I changed the 

public AccountSummary__c Summary{
        get{ ...
to
public AccountSummary__c getSummary(){ ...


and it errors out with DML not allowed.  Is this a bug in SF in that the 'Property{ get{...' concept is acting differently than the 'getProperty(){...' concept?

My current workaround is to move the code into a method and call the method in the page's action tag:
 

public AccountSummary__c Summary{ get; set; }

public RefreshSummary(){
    if(AccountId == null){
        Summary = new AccountSummary__c();
    }
        
    AccountTrigger.ProcessAccountSummary(AccountId);
    Summary = Database.query('SELECT ' + SelectStar + ' FROM AccountSummary__c WHERE AccountId__c = :AccountId');
}

<apex:page action="{!RefreshSummary}">
...
pconpcon
I am surprised that you do not get the DML exception in the non-getSummary() method.  I think that is a bug.  If you want it to do an update, you have to do it in a non get/set context of the visualforce page.  Such as the action call or as part of a button press.
This was selected as the best answer
Tejpal KumawatTejpal Kumawat
Hello JacobLabrix,

This is You can follow this process :

1. Query in your constructor for AccountSummary__c if return 0 recods then send new AccountSummary__c();
2. Create Seprate method (If RefreshSummary is seprate then its OK)in which you wants to update record.
3. Remove action="{!RefreshSummary}" from <apex:page > 
4. Add these in your Page
    <script>
            jsSummary();
    </script>

and add also

<apex:pageMessages id="pgmsgId"/>
<apex:actionFunction name="jsSummary" action="{!RefreshSummary}" rerender="pgmsgId"/>


PS: If this answers your question then hit Like and mark it as solution!



 
JWykelJWykel
pcon, I tend to agree with you on this one, which is what I thought in the first place once I did the additional testing.

Tejpal Kumawat, thanks for another way to get it working :)  The <apex:page action="{!RefreshSummary}" works as expected; is there a best practice or other reason I do not know to use your suggestion over the page action?