0

I have a custom object with a Mast-Detail relationship to the Opportunity. I am trying to create a Visualforce page that will pre-populate fields based upon values in the Opportunity and Account. I have a custom controller where I can pull in the Opportunity ID, but I have some formula fields in my custom object that I need to populate in the initial VF page and I'm not sure how. For example, I have a field where the formula is Opportunity__r.Type, which pulls the Type field from the Opportunity associated to my custom object record. It works fine when I save a record, but I do I get it to populate upon the initial view? My controller is below:

public class VF_CampaignCaseCreateController{

public List<Campaign_Create_Request__c> CCR {get; set;}

    private final Opportunity opp;
    public VF_CampaignCaseCreateController(ApexPages.StandardController myController){
        CCR = new List<Campaign_Create_Request__c>();
            if (!Test.isRunningTest())
            {
            myController.addFields(new List<String>{'Id', 'OwnerId', 'Owner.Phone'});
            }
        opp=(Opportunity)myController.getrecord();
    }

    public Campaign_Create_Request__c CCR2 = new Campaign_Create_Request__c();
        public void CampaignCaseCreate(){

            CCR2.Opportunity__c = opp.Id;

            Opportunity o = [SELECT (SELECT Id, contactId
                                       FROM OpportunityContactRoles
                                       WHERE role = 'Signatory')
                            FROM Opportunity
                            WHERE id = :opp.id];
            CCR2.Primary_User__c = o.opportunityContactRoles.size() != 0 
                                    ? o.opportunityContactRoles[0].contactId  
                                    : null;

            CCR.add(CCR2);
        }


    public PageReference save() {

        insert CCR;

       PageReference RetPage = new PageReference('/?id=' + CCR[0].id);
        RetPage.setRedirect(true);
        return RetPage; 
    }
}
  • formula field is not writeable in any way that is why you can't assign value to them. – Himanshu Jan 11 '16 at 17:59
  • I know I can't write to them, but is there a way I can show them prior to saving the custom record since I have the Opportunity ID pre-populated in my VF page? – J. Neilan Jan 11 '16 at 18:01
  • is VF_CampaignCaseCreateController a controller extension on Opportunity or a custom controller (not an extension)? – cropredy Jan 11 '16 at 18:19
  • It's actually a controller extension on the Opportunity. – J. Neilan Jan 11 '16 at 18:21
0

As formula fields are not writable you can't initialize that but as alternative option you can display Opportunity fields directly on your page from your controller variable because it is holding same value what you will get after assigning to your custom objec.

| improve this answer | |
  • Yes, I did look at doing that. However, I have some pretty lengthy formula fields on my custom object and I was trying to avoid duplicating them on the Opportunity just for the initial creation of a custom object record. – J. Neilan Jan 11 '16 at 18:22
0

There's an APEX method to populate formula fields on a record without needing to save it first. It's called recalculateFormulas():

CCR2.Primary_User__c = o.opportunityContactRoles.size() != 0 
   ? o.opportunityContactRoles[0].contactId  
   : null;

CCR2.recalculateFormulas();

CCR.add(CCR2);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.