0

How can I display the selected record type name in my vf page even though it is not totally inserted yet?

Note: I am using salesforce's standard record selection.

Someone told me to use before insert in apex trigger, but I am not so used to it. Can anyone show me how to execute this?

Meanwhile, here are my controller and visualforce page:

Controller:

public class TheController {
    private ApexPages.StandardController controller;
    public Object__c obj {get;set;}
    public List<Object__c> objList {get;set;}

    public TheController(ApexPages.StandardController con) {
        controller = con; 
        this.obj = (Object__c)controller.getRecord();   
        Object__c obj = new Object__c();
        List<Object__c> objList = new List<Object__c>();
    }

    public PageReference save() {

        objList = new List<Object__c>();
        Object__c__c obj2 = new Object__c();

        obj2.Field1__c = obj.Field1__c;
        obj2.Field2__c = obj.Field2__c;
        obj2.Field3__c = obj.Field3__c;
        obj2.Field4__c = obj.Field4__c;
        objList.add(obj2);

        if(!objList.isEmpty()) {

            insert objList; 

        }    

        PageReference pg = new PageReference('/' + obj2.Id);
        pg.setRedirect(true);
        return pg;
    }

}

Visualforce Page:

<apex:page standardController="Object__c" extensions="TheController">
    <apex:form >
        <apex:pageBlock >

            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>

            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!obj.Field1__c}"/>
                <apex:inputField value="{!obj.Field2__c}"/>
                <apex:inputField value="{!obj.Field3__c}"/>
                <apex:inputField value="{!obj.Field4__c}"/>
            </apex:pageBlockSection>

        </apex:pageBlock>
    </apex:form>  
</apex:page>
3
  • What do you mean, 'even though it is not totally inserted yet'? Do you want the record type before you click save? or after?
    – Nick C
    Nov 22, 2016 at 6:50
  • @NickCook I want the record type before clicking save. Nov 22, 2016 at 6:54
  • but how do you know what it's supposed to be? Are you trying to specifically set it? or is it sufficient to get the default record type for the user that's filling out the form?
    – Nick C
    Nov 22, 2016 at 7:36

1 Answer 1

1

If you use standard Controller you can just use:

<apex:page standardController="Object__c" >
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:inputField value="{!Object__c.RecordTypeId}" />
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

Or if you need some logic on selection try this:

<apex:page standardController="Object__c" extensions="TheController">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:selectList value="{!Object__c.RecordTypeId}" size="1" > 
                <apex:selectOptions value="{!rtypes}"/>
            </apex:selectList>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

public class TheController {

public TheController(ApexPages.StandardController controller) {}

public Selectoption[] getRTypes(){
    SelectOption[] options = new Selectoption[]{};
    //change query to filter out some data - as needed
    for(RecordType rt : [SELECT Id, Name FROM RecordType]){
        options.add(new SelectOption(rt.Id, rt.Name));
    }  
    return options ;
}
}
1
  • But what if I am using Salesforce's standard record type selection instead of making a customized version of it, then proceed with my vf page? Nov 22, 2016 at 11:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .