Skip to content

Instantly share code, notes, and snippets.

@SalesforceBobLightning
Last active July 21, 2018 16:18
Show Gist options
  • Select an option

  • Save SalesforceBobLightning/c465a1a7d15915846d7d86b397532f22 to your computer and use it in GitHub Desktop.

Select an option

Save SalesforceBobLightning/c465a1a7d15915846d7d86b397532f22 to your computer and use it in GitHub Desktop.
Change Object Record Type via Salesforce Process Builder using Record Type Developer Name
public class ChangeRecordTypeAction {
Private static final String RECORD_TYPE_ID = 'RecordTypeId';
@InvocableMethod(
label = 'Change Object Record Type'
description = 'Change Record Type by Developer Name'
)
public static List<Response> execute(List<Request> requests) {
List<Response> responses = new List<Response>();
for (Request request : requests) {
responses.add(updateRecordType(request));
}
return responses;
}
private static Response updateRecordType(Request request) {
Response response = new Response();
try {
Id recordTypeId = getRecordTypeId(request.objectName, request.developerName);
if (recordTypeId == null){
throw new NoRecordTypeFoundException(request);
}
String queryString = getQueryString(request.objectName, request.recordId);
List<sObject> results = Database.query(queryString);
if (results.size() == 0) {
throw new NoRecordFoundException(request);
}
SObject record = results[0];
record.put(RECORD_TYPE_ID, recordTypeId);
update record;
response.success = true;
}
catch (Exception ex) {
response.errorMesssage = ex.getMessage();
response.success = false;
}
return response;
}
private static String getQueryString(String objectName, String recordId) {
List<String> args = new String[]{objectName, recordId};
return String.format('SELECT Id FROM {0} WHERE Id = \'\'{1}\'\'', args);
}
private static Id getRecordTypeId(String objectName, String developerName){
return Schema.getGlobalDescribe().get(objectName).getDescribe().getRecordTypeInfosByDeveloperName().get(developerName).getRecordTypeId();
}
public class Request {
@InvocableVariable(
label = 'Record ID'
description = 'The ID of the record to have its record type changed'
required = true
)
public Id recordId;
@InvocableVariable(
label = 'Object Name'
description = 'The Name of the Object which will have its record type changed'
required = true
)
public String objectName;
@InvocableVariable(
label = 'Record Type Developer Name'
description = 'The Developer Name of the Record Type to change the object to'
required = true
)
public String developerName;
}
public class Response {
@InvocableVariable(
label = 'Error Message'
description = 'The message of the error'
)
public String errorMesssage;
@InvocableVariable(
label = 'Is Success'
description = 'Successfully changed'
)
public Boolean success;
}
private class NoRecordTypeFoundException extends Exception {
Private Request request;
public NoRecordTypeFoundException(Request request){
this.request = request;
}
public override String getMessage() {
List<String> args = new String[]{request.objectName, request.developerName};
return String.format('No Record Type ID found on object: {0} with Record Type Developer Name: {1}', args);
}
}
private class NoRecordFoundException extends Exception {
Private Request request;
public NoRecordFoundException(Request request){
this.request = request;
}
public override String getMessage() {
List<String> args = new String[]{request.objectName, request.recordId};
return String.format('No Record found for object: {0} with Record ID: {1}', args);
}
}
}
@isTest
public class ChangeRecordTypeActionTests {
private static final String OBJECT_NAME = 'Account';
private static final String SOURCE_RECORD_TYPE_DEVELOPER_NAME = 'Your_Custom_Record_Type_Developer_Name'; // CHANGE ME
private static final String TARGET_RECORD_TYPE_DEVELOPER_NAME = 'Your_Other_Custom_Record_Type_Developer_Name'; // CHANGE ME
@isTest
public static void updateRecordType_Success(){
// arrange
ChangeRecordTypeAction.Request request = new ChangeRecordTypeAction.Request();
request.objectName = OBJECT_NAME;
request.developerName = TARGET_RECORD_TYPE_DEVELOPER_NAME;
request.recordId = getRecordId();
// act
List<ChangeRecordTypeAction.Response> responses = ChangeRecordTypeAction.execute(new List<ChangeRecordTypeAction.Request> {request});
// assert
System.assertEquals(1, responses.size());
System.assertEquals(true, responses[0].success);
}
@isTest
public static void updateRecordType_NoRecordTypeFoundException(){
// arrange
ChangeRecordTypeAction.Request request = new ChangeRecordTypeAction.Request();
request.objectName = OBJECT_NAME;
request.developerName = 'Unknown_Record_Type_Developer_Name';
request.recordId = getRecordId();
// act
List<ChangeRecordTypeAction.Response> responses = ChangeRecordTypeAction.execute(new List<ChangeRecordTypeAction.Request> {request});
// assert
System.assertEquals(1, responses.size());
System.assertEquals(false, responses[0].success);
}
@isTest
public static void updateRecordType_NoRecordFoundException(){
// arrange
ChangeRecordTypeAction.Request request = new ChangeRecordTypeAction.Request();
request.objectName = OBJECT_NAME;
request.developerName = TARGET_RECORD_TYPE_DEVELOPER_NAME;
request.recordId = '100000000000abc';
// act
List<ChangeRecordTypeAction.Response> responses = ChangeRecordTypeAction.execute(new List<ChangeRecordTypeAction.Request> {request});
// assert
System.assertEquals(1, responses.size());
System.assertEquals(false, responses[0].success);
}
private static Id getRecordId(){
Account account = new Account();
account.FirstName = 'Bat';
account.LastName = 'man';
account.PersonEmail = 'bruce@WayneEnterprises.com';
account.RecordTypeId = getRecordTypeId(OBJECT_NAME, SOURCE_RECORD_TYPE_DEVELOPER_NAME);
insert account;
return account.Id;
}
private static Id getRecordTypeId(String objectName, String developerName){
return Schema.getGlobalDescribe().get(objectName).getDescribe().getRecordTypeInfosByDeveloperName().get(developerName).getRecordTypeId();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment