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
Yang WangYang Wang 

Check record type and set default value for closedate field in creating opportunity page

public with sharing class TestVFController {

    public String recordtype = '';
    public TestVFController(ApexPages.StandardController controller) {
    recordtype = ApexPages.currentPage().getParameters().get('RecordType');
        if(String.isEmpty(recordtype)) {
            Map<Id,Schema.RecordTypeInfo> recordTypeInfos = Opportunity.SObjectType.getDescribe().getRecordTypeInfosByID();
            if(recordTypeInfos.get('012280000011A6F').isAvailable()){
                recordtype = '012280000011A6F';
                system.debug('debug:TypeA');
            }
            if(recordTypeInfos.get('012280000011A6K').isAvailable()){
                recordtype = '012280000011A6K';
                system.debug('debug:TypeB');
            }
        }
        
    }

    public PageReference init(){
        String url = '';
        system.debug('debug:recordtype:' + recordtype);
        if(recordtype == '012280000011A6F') {
            url = '/006/e?nooverride=1&retURL=/006/o';
        } else if(recordtype == '012280000011A6K') {
            url = '/006/e?opp9=' + system.today().addDays(5).format() + '&nooverride=1&retURL=/006/o';
        }
        
        return new PageReference(url);
        
    }
}

 
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Yang, 

Please use getRecordTypeInfosByName() instead of getRecordTypeInfosByID. This code will not work if it is deployed to another org as you are hard coding IDs and Record Type ids used to vary from org to org.

Thanks, 
Prosenjit