+ Start a Discussion
SFDC_DeveloperSFDC_Developer 

How to display update Name in vf page

I have one VF page which has one field Name having default value of 'filled by trigger'. In the same page, I have one lookup field to object B. When we select this field and click on save button then our trigger will fire and it will update the Name value with the name of Selected record of object A.
I have checked the record created from vf page in standard page, where it is correctly displaying the field name, but when it comes to vf page it is not displaying the update name.
I am guessing this may be because of getter and setter.

VF Page:
 
<apex:page standardController="ObjectA" extensions="objectAController" >     
<apex:form >
   <apex:pageBlock title="ObjectA Edit" mode="edit">
   <apex:pageMessages /> 
        <apex:pageBlockButtons location="top">
            <apex:commandButton value="Save" action="{!save}" />
        </apex:pageBlockbuttons>   
        <apex:pageBlockSection title="ObjectA Name" columns="2" >
            <apex:inputField value="{!ObjectA__c.Name}" required="true"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Product Information">
            <apex:inputField value="{!ObjectA__c.Product2Id__c}" />
        </apex:pageBlockSection>        
    </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
 
public class objectAController {
  ApexPages.StandardController stdCtrl;

    public objectAController(ApexPages.StandardController controller)
     {  
     this.obj = (ObjectA)controller.getRecord();      
     obj.Name = 'Filled by Trigger';
     this.stdCtrl= controller;      
     }
 public PageReference save()
  {
      ObjectA__c objA =(ObjectA__c)stdCtrl.getRecord();
      try {
       upsert(objA);
        }
    catch(System.DMLException e) {
       ApexPages.addMessages(e);
     }
    return null;
  }
}

 
Best Answer chosen by SFDC_Developer
Agustina GarciaAgustina Garcia
Got it now :)

As you are using standard Controller, objectA__c.Name, in order to show its value, the page needs an url, so after saving, instead of return null, you would need to return your page with the id as parameter. In addition, you would need to check in the constructor if the record has Id in order to populate it with the Productname__c value or your default one.
 
public class objectAController
{

    ObjectA__c obj;
    ApexPages.StandardController stdCtrl;

    public objectAController(ApexPages.StandardController controller)
    {  
        this.obj = (ObjectA__c)controller.getRecord();
     
        if(obj.Name == null)
        {
            obj.Name = 'Filled by Trigger';
        }
        this.stdCtrl= controller;      
    }
     
    public PageReference save()
    {
        ObjectA__c objA =(ObjectA__c)stdCtrl.getRecord();
        try
        {
            upsert(objA);
        }
        catch(System.DMLException e)
        {
            ApexPages.addMessages(e);
        }
    
        PageReference pg = new PageReference('/apex/<yourpagename>?id='+objA.Id);
        pg.setRedirect(true);
    
        return pg;
    }
}
Another option is to override the edit button of your ObjectA__c with this page.

And return edit standard fucntionality in the save method
 
public PageReference save()
{
ObjectA__c objA =(ObjectA__c)stdCtrl.getRecord();
      try {
       upsert(objA);
        }
      catch(System.DMLException e) {
       ApexPages.addMessages(e);
     }
    
    return stdCtrl.edit();
}


 

All Answers

Agustina GarciaAgustina Garcia
As you are using ObjectA__c standard controller in order to show their fields, you don't need a new get/set method in the controller.

I added this line to the vf page in order to be sure that a new record was created, and yes, after clicking on save, the Id value appears in the vf page.
 
<apex:pageBlockSection title="ObjectA Name" columns="2" >
            <apex:inputField value="{!ObjectA__c.Name}" required="true"/>
            <apex:outputField value="{!ObjectA__c.Id}" />            
 </apex:pageBlockSection>
Apart from that, your code looks fine. I have tried setting the new Name value directly and works for me, so maybe the issue is in your trigger. Could your share it here?
 
public with sharing class objectAController
{
    ObjectA__c obj;
    ApexPages.StandardController stdCtrl;

    public objectAController(ApexPages.StandardController controller)
    {  
        this.obj = (ObjectA__c)controller.getRecord();      
        obj.Name = 'Filled by Trigger';
        this.stdCtrl= controller;      
    }
     
    public PageReference save()
    {
        Id objAId;
        ObjectA__c objA =(ObjectA__c)stdCtrl.getRecord();
        objA.Name = 'MyObject_6';
        try
        {
            upsert(objA);
            objAId = objA.Id;
        }
        catch(System.DMLException e)
        {
            ApexPages.addMessages(e);
        }
        
        return null;
  }
}

 
SFDC_DeveloperSFDC_Developer
Hi Agustina,

Thanks for your reply.
I have added the output field 
<apex:outputField value="{!ObjectA__c.Id}" />

in my code and after saving record it is showing record it also.
SFDC_DeveloperSFDC_Developer
trigger ObjectATrigger on ObjectA (before insert, before update)
{
	for(ObjectA a: trigger.new){
		if(a.Product2Id__c != NULL)
                {
                    a.name = a.ProductName__c;
                } 
	}
}

Here ProductName__c is formula field.
Agustina GarciaAgustina Garcia
Got it now :)

As you are using standard Controller, objectA__c.Name, in order to show its value, the page needs an url, so after saving, instead of return null, you would need to return your page with the id as parameter. In addition, you would need to check in the constructor if the record has Id in order to populate it with the Productname__c value or your default one.
 
public class objectAController
{

    ObjectA__c obj;
    ApexPages.StandardController stdCtrl;

    public objectAController(ApexPages.StandardController controller)
    {  
        this.obj = (ObjectA__c)controller.getRecord();
     
        if(obj.Name == null)
        {
            obj.Name = 'Filled by Trigger';
        }
        this.stdCtrl= controller;      
    }
     
    public PageReference save()
    {
        ObjectA__c objA =(ObjectA__c)stdCtrl.getRecord();
        try
        {
            upsert(objA);
        }
        catch(System.DMLException e)
        {
            ApexPages.addMessages(e);
        }
    
        PageReference pg = new PageReference('/apex/<yourpagename>?id='+objA.Id);
        pg.setRedirect(true);
    
        return pg;
    }
}
Another option is to override the edit button of your ObjectA__c with this page.

And return edit standard fucntionality in the save method
 
public PageReference save()
{
ObjectA__c objA =(ObjectA__c)stdCtrl.getRecord();
      try {
       upsert(objA);
        }
      catch(System.DMLException e) {
       ApexPages.addMessages(e);
     }
    
    return stdCtrl.edit();
}


 
This was selected as the best answer
SFDC_DeveloperSFDC_Developer
Thanks Agustina Garcia,
Now its working
 
Agustina GarciaAgustina Garcia
Glad it is working. Could you mark the solution as solved? So if someone else has the same issue can navigate directly to it.

Thanks,
Agustina
SFDC_DeveloperSFDC_Developer
done