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
Markey1Markey1 

Set Record Type ID via Page ID

Hello,

 

How do I set the Record Type ID (value) based off which Visualforce Page a user is currently viewing?

 

This is a Sites project. When a user is on the "enrollment" page and submits their form, I want the custom controller extension (or ?) to reference the Page ID to set the Record Type to "Enrollment" (i.e. Record Type Name = Enrollment, Record Type ID = 012Q00000004So4).

 

My Code (could be completely off, mashed a few things together from other forums):

 

public rtt() {

if(System.currentPageReference().getParameters().get('id')= "enrollment") {
  RecordType.ID = "012Q00000004So4";
  } 
}

My Error:

 

Error: Compile Error: line 84:60 no viable alternative at character '"' at line 84 column 60

rmehrmeh

Hi,

 

Check the below code, i think you should use logical operator in the if loop.

 

public rtt() {

if(System.currentPageReference().getParameters().get('id')== "enrollment") {
RecordType.ID = "012Q00000004So4";
}
}

This should solve your error!!!
Markey1Markey1

Hi rmeh,

 

Thanks for your response. I tried:

public rtt() {
    if(System.currentPageReference().getParameters().get('id')== "aeskisenroll") {
        RecordType.ID = "012Q00000004So4";  
    } 
}

 

Unfortunately, I get an error:

Error: Compile Error: line 83:61 no viable alternative at character '"' at line 83 column 61  

 Column 83 is where the "if" statement starts.... any ideas? Need me to post any other code?

rmehrmeh

Hi Markey1,

 

Please do the following changes and try it:

 

 

public rtt() {
if(System.currentPageReference().getParameters().get('id') == 'aeskisenroll') {
RecordType.ID = "012Q00000004So4";
}
}

 This will work!!!

 

System.currentPageReference().getParameters().get('id')
sfdcfoxsfdcfox

The RecordType.ID line is also incorrect. Visualforce always uses a "single quote" (apostrophe) instead of the usual "double quotes" found in most other programming languages.

Markey1Markey1

Thanks sfdcfox,

 

Getting there...now I'm getting an error "Error: Compile Error: Invalid constructor name: rtt at line 82 column 8". Also, not sure if it matters, but this is a "Sites" project.

 

public rtt() {
    if(System.currentPageReference().getParameters().get('id') == 'aeskisenroll') {
        RecordType.ID = '012Q00000004So4';
    }
}

Full Controller Code:

 

public class AESK_Enroll {
    
     /*Variables*/
    public SafeKey_Enrollment__c skEnroll;

    public rtt() {
        if(System.currentPageReference().getParameters().get('id') == 'aeskisenroll') {
            RecordType.Id = '012Q00000004So4';
        }
    }

    /*Controller*/
    ApexPages.StandardController controller;
    public AESK_Enroll(ApexPages.StandardController con){
        controller = con;
        skEnroll = (Enrollment__c)controller.getRecord();
    }           

    /*Validate, save, and return page*/
    public PageReference save() {    
        try {          
            insert skEnroll;
        } 
        catch (DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        return page.aeskenrollconfirm; 
    }        
}

 

 

 

rmehrmeh

Hi,

 

Please do the following changes in your code.

 

 

public class AESK_Enroll {

/* declare a string property*/
public String strId{get;set;}
/*Variables*/
public SafeKey_Enrollment__c skEnroll;

public rtt() {
// In the if condition instead of using System.current.... use the property strId... i.e. if(strId == 'aeskisenroll')
if(System.currentPageReference().getParameters().get('id') == 'aeskisenroll') {
RecordType.Id = '012Q00000004So4';
}
}

/*Controller*/
ApexPages.StandardController controller;
public AESK_Enroll(ApexPages.StandardController con){
// assign the string property in the constructor to System.currentPageReference().getParameters().get('id')
strId = System.currentPageReference().getParameters().get('id');

controller = con;
skEnroll = (Enrollment__c)controller.getRecord();
}

/*Validate, save, and return page*/
public PageReference save() {
try {
insert skEnroll;
}
catch (DMLException e) {
ApexPages.addMessages(e);
return null;
}
return page.aeskenrollconfirm;
}
}

 

Check if it works for you!!

 

Markey1Markey1

Hi rmeh,

 

I really appreciate your help and feel like I'm getting closer. I still get the error "Error: Compile Error: Invalid constructor name: rtt..".  Should I be using "Public Void rtt()"?

rmehrmeh

Yes, please try using the below and i know this should not be the issue, but try and put the method after your constructor call.

public void rtt()

{

 

}