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
michael32michael32 

How to set Case record type?

The Case object for has a standard field for 'Type' (in the code below, this is represented by 'Case.Type'). The Case object also has a 'Case Type' standard field, which identifies the case record.

 

Right now I my conditionals are based on the 'Type' field, but I want them to be based on the 'Case Type' record field (I want to populate the body based on the record type). My goal is to do this without hardcoding a record identifier, which to my understanding, is bad practice as it is not always consistent across Dev/Production environments.

 

My code is as follows:

 

public class myControllerExtention2 {
private Case cas;
private string EmailBody, Subject;
//Define default values
String defaultEmailBody = '';
String defaultSubject = '';

//Instantiate Visualforce standard controller 
public myControllerExtention2(ApexPages.StandardController stdController) {
//Instantiate case record
this.cas= (case)stdController.getRecord();
//SOQL query
cas = [SELECT Type FROM Case where Id =: cas.Id];
//Case record type conditionals
If (cas.Type=='AAA') {
emailBody = 'AAA body content';
subject = 'AAA subject';
}
If (cas.Type=='BBB') {
emailBody = 'BBB body content';
subject = 'BBB subject';
}
If (cas.Type=='CCC') {
emailBody = 'CCC Body';
subject = 'CCC subject';
}
}

//Accessor methods
public string GetEmailBody() {
If (emailBody == NULL) { 
return defaultEmailBody;
}
Else {
return emailBody;
}
}
public string GetSubject() {
If (subject == NULL) { 
return defaultSubject;
}
Else {
return subject;
}
}
}
Bhawani SharmaBhawani Sharma
I don't think so if you keep the code in thsi way that would be a problem, because anyhow you are separating each condiftion with a different email body and if you need include something new in email text, you will have to change in code.

Still, you can use custom setting to remove all harding coding.
Like
You can create a custom setting with fields:
Type Name, Email Subject, Email Body

new, in your class, hit a SOQLQuery to find the appropriate record
[Select Email Subject, Body from Custom_Setting__c where Name =: Case.Type];

Now set the email subjetc and body
michael32michael32

Let me explain better - Forget about all code example above, I am not concerned about changing the email body/header. Say all I want to do is change the record type of a case record, How would I do this?

 

Better example:

 

public class myControllerExtention2 {
private Case cas;

//Instantiate Visualforce standard controller 
  public myControllerExtention2(ApexPages.StandardController stdController) {

//Instantiate case record
    this.cas= (case)stdController.getRecord();
//SOQL query cas = [SELECT Type FROM Case where Id =: cas.Id]; //SET CASE RECORD TYPE Set cas.RecordType=='AAAA' //<< INCORRECT
} }

 

Bhawani SharmaBhawani Sharma
RecordType rT = [Select Id from RecordType where Name = 'AAA'];
case.RecordTypeId = rT.Id;