+ Start a Discussion
Amol ChAmol Ch 

VF page not updating realted list record when created new record from same page.

I have a Vf page to display the related list which is editable.
Assesment is Custom object. We have two button "Create Assesment" & "Update assesment" on VF Page.
Scenario 1: When I open the page & click on update assessment then it updates the related record correct. Its Fine.
Scenario 2: When I open the page & click on create assessment button it open in new tab and I've created the record. Again back to same vf page & click on update assessment button then it updating the wrong records.
how to resolve this issue.
Vf Page:
<apex:page standardController="Assessment__c" sidebar="false" showHeader="false" showChat="true" extensions="AssessmentController">
<apex:form >
    <apex:pageBlock id="block1" >
        <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockSection title="Assessments" id="Assessments" columns="1" collapsible="true" >
                 <apex:outputPanel >
                    <apex:commandLink action="{!createAssesmnt}" target="_blank"><apex:commandButton value="New Assesment" style="float:right;"/></apex:commandLink>        
                    <apex:commandButton action="{!updateAss}" value="Update Assesment" style="float:right;" reRender="refTble"/>  
                  </apex:outputPanel>
                <apex:outputPanel Id="blockrefresh">
                <apex:pageBlockTable value="{!Asses}" var="a" Id="refTble" >
                    <apex:column headerValue="Assment" style="width: 5px;" >
                        <apex:outputLink value="/{!a.id}" target="_blank">Open</apex:outputLink>
                    </apex:column>

                    <apex:column headerValue="Description" >
                        <apex:inputField value="{!a.Assessment_Description__c}" style="overflow:hidden"/>
                    </apex:column>

                    <apex:column headerValue="Last Screening" >
                        <apex:inputField value="{!a.Last_Screening__c}" style="overflow:hidden"/>
                    </apex:column>

                </apex:pageBlockTable> 
                    </apex:outputPanel>
            </apex:pageBlockSection>
     </apex:pageBlock>
</apex:form>
Controller:
public with sharing class AssessmentController {

private List<Assessment__c> asses;
Assessment__c assment;
Assessment__c AssRec;
public Outcome__c outcome{get;set;}

public AssessmentController(ApexPages.StandardController controller) {
    this.assment= (Assessment__c)controller.getRecord();
    outcome = new Outcome__c ();
    AssRec= [select id,Name,Patient__r.Name,PHQ9_Measurement_ID__c,PSMS_Measurement_ID__c from Assessment__c where Id=:assment.Id];
}
public PageReference createAssesmnt(){
    Schema.DescribeSObjectResult result = Assessment__c.SObjectType.getDescribe();
    PageReference pageRef = new PageReference('/' + result.getKeyPrefix() + '/e?C00N1a000006Z8Gs_lkid='+assment.Patient__c+'&CF00N1a000006Z8Gs='+AssRec.Patient__r.Name);
    pageRef.setRedirect(true);
    return pageRef;
}
public List<Assessment__c> getAsses()
{  
    asses = [Select id,Assessment_Description__c,Last_Screening__c,Clone__c,Category__c,Date_Due__c, Resolved__c,Delete__c,Type__c,Sort__c,RT_Type__c,Outcome_Count__c,Create_Outcome__c,Use_Screening_Update__c,Screening_Update__c, (select id,Name,Assessment_Description__c,Outcome_Description__c,Type__c,Completed__c from Outcomes__r) from Assessment__c where Resolved__c=false AND Patient__c=:assment.Patient__c ORDER BY Assessment_Description__c ASC];
    for(Assessment__c a:asses)
    system.debug('Assesment List ##'+a.Id+'   '+a.Assessment_Description__c);
    return asses;
}
public pageReference updateAss(){
try{
    for(Assessment__c a:asses)
    system.debug('Assesment List @@'+a.Id+'   '+a.Assessment_Description__c);
    update asses;

    return null;
}
catch(Exception e){
    string message='Data Base error during saving...';
    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR,message));
    return null; 
}
} 
 }