1

I am able to get record in controller and getting the recordtypeid that I wanted to assign the record

private ApexPages.StandardController ctrl;
public FTR__c ftrObj {get;set;}

public FTR_Controller(ApexPages.StandardController controller) {
    this.ctrl = controller;
    this.ftrObj = (FTR__c) controller.getRecord();

    this.ftrObj.RecordTypeId = Schema.SObjectType.FTR__c.getRecordTypeInfosByName().get('FTR OE Record Type').getRecordTypeId();
}

But how to set it to currentpage record??
Is there any workaround to set it to controller like: controller.setRecord(ftrObj) ?

What I want is when the VisualForce page load, the object is predefined to specific record type

2
  • 2
    From what I understood, while creating a new record, you want to assign a recordtype to that record at the page load? Do you want to do something like assign the record type to the record, save the record, and then let the user update the record through the VF page? Please elaborate your query. Nov 24, 2017 at 5:16
  • 1
    while creating new record, user will be directed to my custom visualforce page and while loading the page, the constructor will be updating the recordtype based on what I defined in the controller and set the record to certain recordtype. This will be resulting different picklist value or different field to populate each recordtype as example
    – Rival
    Nov 24, 2017 at 6:48

1 Answer 1

2

There is one problem with this. You can not perform a DML in constructor, as per Salesforce standards, so you wont be able to update the record in the constructor.

If you need to retrieve the picklist values based on the record type of the user, you might wanna do this:

Visualforce page

<apex:page controller="Sample">
    <apex:form >
        <apex:inputField value="{!objEmp.Approval_Status__c}"/>
        <br/><br/><br/>
        <apex:inputField value="{!objEmp1.Approval_Status__c}"/>
    </apex:form>
</apex:page>

Controller

public with sharing class Sample { 

    public Employee__c objEmp {get;set;}
    public Employee__c objEmp1 {get;set;}

    public Sample() {
        objEmp1 = new Employee__c(RecordTypeId = '0120P000000EDfb');
        objEmp = new Employee__c(RecordTypeId = '0120P000000EDfg');
    }

}

This will result in the picklist values regarding record type.

If you still feel the need to update the record as according to your requirement, you may find this workaround useful: https://developer.salesforce.com/forums/?id=906F0000000AjPRIA0

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.