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
Selina SuarezSelina Suarez 

how to change a record type name in my apex class

I have a lead conversion process and I want to change the name of one of my record types. When I change it from my record type list-  it causes the apex class to return errors. How do I change the record type name within my apex class coding. I used the search magnifying glass icon to look up instances of the old name in the apex class and replaced with the new name but this does not seem to be working. 

Can anyone offer any advice here. i am at the beginning stages of learning to code with APEX.

Thanks!


Best Answer chosen by Selina Suarez
Boris BachovskiBoris Bachovski
Oh so you want to change the actual name of the record type itself? Can you please let me know what your "GlobalVariables" object is? Is that a custom setting or another class somewhere in your code?

All Answers

Boris BachovskiBoris Bachovski
You can set the record's RecordTypeId if you know the record type developer name by doing the following:

public void yourMethod()
{
	Map <String, Map <String, String>> recordTypes = getRecordTypes(new List <String> {'Lead', 'Another_Object__c'});

	Lead lead = new Lead ();
	lead.RecordTypeId = recordTypes.get('Lead').get('YOUR_RECORD_TYPE_DEVELOPER_NAME');
        // do something with the lead record
}

private Map <String, Map <String, String>> getRecordTypes (List <String> objectType)
{
	 Map <String, Map <String, String>> tempMap = new Map <String, Map <String, String>> ();

	try
	{
		for (RecordType rt : [SELECT Id, Name, DeveloperName, sObjectType FROM RecordType WHERE sObjectType IN :objectType])
		{
			if (tempMap.get(rt.sObjectType) == null)
			{
				tempMap.put(rt.sObjectType, new Map <String, String> ());
			}

			// Populate the map with ID and DeveloperName as keys
			tempMap.get(rt.sObjectType).put(rt.ID, rt.DeveloperName);
			tempMap.get(rt.sObjectType).put(rt.DeveloperName, rt.ID);
		}
	}
	catch (Exception e)
	{
		Debug(e.getMessage());
	}

	return tempMap;
}

Selina SuarezSelina Suarez
Thanks Shri- I didnt write this original code so I am trying to edit it to do what its supposed to do. 
I believe there are workflow rules and other things associated with the conversion process.

Bori, Do you mean- add this piece of code to the beginning of the Apex class? 
Boris BachovskiBoris Bachovski
Do not hard code ID's, ever! That's a big no-no on the platform.

You can add the method "getRecordTypes" anywhere in your code, but where you're hardcoding the ID in your code, replace that line with "lead.RecordTypeId = recordTypes.get('Lead').get('YOUR_RECORD_TYPE_DEVELOPER_NAME');". You should be always getting the record type ID based on the record type Developer Name which you can find in setup -> customize -> leads -> record types -> your preferred record type.
Selina SuarezSelina Suarez
The name of the record type is  "Application Lead" the ID is 012700000005TdCAAU. I just want to be able to change it to "National Application Lead". If i try and change it form the object settings I get an error because the Lead conversion process accrofing to the apex class will not work. A piece of the code is as follows:

if (!usingExistingNominee) {
           // Look for an existing application lead and use that on the nomination instead of this one we're converting
           try {
               Lead existingApplicationLead = [
                   SELECT Id FROM Lead WHERE Email = :lead.Email AND RecordTypeId = :GlobalVariables.applicantTypeId
                   and IsConverted = false];
               nomination.Nominee_Lead__c = existingApplicationLead.Id;
           }

Boris BachovskiBoris Bachovski
Oh so you want to change the actual name of the record type itself? Can you please let me know what your "GlobalVariables" object is? Is that a custom setting or another class somewhere in your code?
This was selected as the best answer
Selina SuarezSelina Suarez
it is a class somewhere else
Selina SuarezSelina Suarez
I changed the name in the Global Variable class and tis worked perfectly but now when I am trying to deploy a change set I am running into an error with the name again. Boris do you have any thoughts in this? See below: 

Apex Test Failures

Class Name: MassConvertLead
Method Name: MassConvertLead testMassConvertLead System.QueryException:
Error Message: List has no rows for assignment to SObject
Stack Trace: Class.MassConvertLead.testMassConvertLead: line 55, column 1

I recently changed the record type name and line 55 is pointing to something referencing the record type name as follows:
Id recordTypeId = [Select Id From RecordType Where SObjectType = 'Lead' And Name = 'National Admissions Application'].Id;

National Admissions Application is the new record type name.