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
Alex SkemprisAlex Skempris 

Save related record of the same object

Hello,

I've been trying to build a VF page that will call a related opportunity record of an opportunity. The related record is being successfully pulled through but there's something missing in my save method and I cannot store inputs of the related record in the database. Can anyone offer some assistance? Here's my code below 

public class ClaimAndExtensionsROR {
    public ApexPages.StandardController std;
    
   Public Opportunity prclaim{get; set;}
   
   public ClaimAndExtensionsROR(ApexPages.StandardController stdCtrl) {
       
    std = stdCtrl;
    stdCtrl.addFields(new List<string>{'AccountId', 'Previous_Relevant_Claim__c'});  
    
     }
   
public Opportunity getOpportunity()
    {
     return (Opportunity) std.getRecord();
    }

public void PopulatedClaim(){
    
  if (null!=getOpportunity().Previous_Relevant_Claim__c && updateRelevantClaims())
         
         {
              Opportunity prclaim=(Opportunity) std.getRecord();
              prclaim.Previous_Relevant_Claim__r =[SELECT Id, Name, AccountId, FOS_Reference_Number__c, StageName, Opportunity_Sub_Stage__c, Submitted_to_FSCS_CB__c,
                                                  Submitted_to_FSCS_Date__c, SIPP__c, Financial_Advice__c, FSCS_Reference_Number__c, Previous_Relevant_Claim__c 
                                                  FROM Opportunity 
                                                  WHERE id=: prclaim.Previous_Relevant_Claim__c];
         

         }
                 
        }

     public PageReference save()
     {
     try{
     upsert prclaim.Previous_Relevant_Claim__r;
     upsert prclaim;
     }
     catch (Exception e){
         System.debug('Exception is =' +e.getMessage());
         }
     Boolean result=true;
     PageReference pr=Page.ROR_Questionnaire;

     if (result)
     {
        // call standard controller save, but don't capture the return value which will redirect to view page
        std.save();
           ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Changes saved'));
     }
        pr.getParameters().put('id', getOpportunity().id);
      
     return pr;
        }
}
Tiago Armando CoelhoTiago Armando Coelho
Hi Alex, 

Your code is kite confusing and I don't understand this two lines can you please explain what are you trying to do?
     upsert prclaim.Previous_Relevant_Claim__r;
     upsert prclaim;

if you want to update two different records please create a dedicated object for each one and what are you trying to update?

Kind regards,
Alex SkemprisAlex Skempris

Thank you Tiago for your reply

Sorry my explanation wasn't great. So I've got a lookup on the opportunity that looks up ,again, on the opportunity. That way I've got two opportunity records related via a lookup field. The look up field is the Previous_Relevant_Claim__c. Via the method PopulatedClaim() i've been able to display the related opportunity record fields (input and output) on the VF page like so:

<apex:outputField value="{!Opportunity.Previous_Relevant_Claim__r.Name}"/>
<apex:OutputField value="{!Opportunity.Previous_Relevant_Claim__r.AccountId}" />
<apex:inputField value="{!Opportunity.Previous_Relevant_Claim__r.StageName}"/>
<apex:inputField value="{!Opportunity.Previous_Relevant_Claim__r.Opportunity_Sub_Stage__c }"/>
<apex:inputField value="{!Opportunity.Previous_Relevant_Claim__r.Submitted_to_FSCS_CB__c}"/>

The problem is that after changing the information on those input fields they are not saved on the database so I'm looking to see what I can do in my save method to store those. My idea was that if I include those update statements along with a try-catch block (which I'm not even sure it's needed) it would store the information when the save method is called but that didn't seem to work.

Please let me know if that still doesn't make sense.

Thanks
Alex 

Tiago Armando CoelhoTiago Armando Coelho
You are trying to changing the values in prclaim.Previous_Relevant_Claim__r right so try the following code:

public class ClaimAndExtensionsROR {
   
   public ApexPages.StandardController std;
    
   public Opportunity prclaim{get; set;}
   
   public Opportunity relatedClaim {get; set;}

   public ClaimAndExtensionsROR(ApexPages.StandardController stdCtrl) {
       
    std = stdCtrl;
    stdCtrl.addFields(new List<string>{'AccountId', 'Previous_Relevant_Claim__c'});  
    
     }
   
public Opportunity getOpportunity()
    {
     return (Opportunity) std.getRecord();
    }

public void PopulatedClaim(){
    
  if (null!=getOpportunity().Previous_Relevant_Claim__c && updateRelevantClaims())
         
         {
              Opportunity prclaim = (Opportunity)std.getRecord();
              

              system.assertEquals(prclaim.Previous_Relevant_Claim__c,null);
              if(prclaim.Previous_Relevant_Claim__c != null ) {
                  relatedClaim =[SELECT Id, Name, AccountId, FOS_Reference_Number__c, StageName, Opportunity_Sub_Stage__c, Submitted_to_FSCS_CB__c,
                                                      Submitted_to_FSCS_Date__c, SIPP__c, Financial_Advice__c, FSCS_Reference_Number__c, Previous_Relevant_Claim__c 
                                                      FROM Opportunity 
                                                      WHERE id=: prclaim.Previous_Relevant_Claim__c];
              }else{
                  system.assertEquals('Your claim is null please check it',null);
              }

         }
                 
        }

     public PageReference save()
     {
     try{
     upsert prclaim.Previous_Relevant_Claim__r;
     upsert prclaim;
     }
     catch (Exception e){
         System.debug('Exception is =' +e.getMessage());
         }
     Boolean result=true;
     PageReference pr=Page.ROR_Questionnaire;

     if (result)
     {
        // call standard controller save, but don't capture the return value which will redirect to view page
        std.save();
           ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Changes saved'));
     }
        pr.getParameters().put('id', getOpportunity().id);
      
     return pr;
        }
}


    Opportunity previousClaim =  prclaim.Previous_Relevant_Claim__r;

    if(previousClaim.Id != null) {
        upsert previousClaim;
    }else 
    {
        system.assertEquals(prclaim.Previous_Relevant_Claim__r.name, 'Check that this is populated');
        system.assertEquals(prclaim.Previous_Relevant_Claim__r.Id, null);
    }
  
I don't test this but you need to refactor your code because after putting this to working.
Alex SkemprisAlex Skempris

Hi Tiago

I'm getting the message that I'm attempting to de-reference a null object. Not sure why but I can tell it's the last "if" causing it:

 if(previousClaim.Id != null) {
        upsert previousClaim;
    }else 
    {
        system.assertEquals(prclaim.Previous_Relevant_Claim__r.name, 'Check that this is populated');
        system.assertEquals(prclaim.Previous_Relevant_Claim__r.Id, null);
    }

Thanks
Alex

Tiago Armando CoelhoTiago Armando Coelho
This error means that  prclaim.Previous_Relevant_Claim__r; is null

Opportunity previousClaim =  prclaim.Previous_Relevant_Claim__r;

If you change the if if(previousClaim.Id != null) { to if(previousClaim.!= null) { you going to enter in else