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
PalosPalos 

Record type change when a picklist value change

Hi Experts,

 

I am new with triggers. I would like to know how I can create a trigger that will change the record type of an Opportunity when the stage is change.

 

Steps to replicate: 

 

1. User clicks new and select OPID as the record type. Answer the fields and select New as the stage.

 

Expected results:

 

1. The record type would change to PreRFP when the User edits an Opportunity and change the Stage to Working.

 

Thank you in advance for the help!

 

Palos

RoyGiladRoyGilad

Hi 

You could update the RecordTypeId of the record using code.

The record type ID itselt (the value you want to update to)  you can find if you check the RecordType table (Select r.SobjectType, r.Name, r.Id From RecordType r)

 

I hope it helps,

Roy

PalosPalos

Hi Roy,

 

Thanks for the suggestion. I tried that before but I was unsuccessful. Heres the code I tried to use. I got the code from one of the post in the board. Hopefully you can provide with a sample code that I can reference to. 

 

trigger recordtypechange on Opportunity (before insert){
for (Opportunity c: Trigger.new) {
c.RecordType = [select Id from RecordType where Name = 'working' and SobjectType = 'Case'];

}
}

trigger changerecordtype on Opportunity (before update) { {
// Get ID for PreRFP Record Type
Id theRT=Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('working').getRecordTypeId();

Integer count=0
List<Id> projIds=new List<Id>();
for (Id theId : trigger.oldMap.keySet())
{
System.debug('############ Opportunity id = ' + theId);
Opportunity oldCont=trigger.oldMap.get(theId);
Opportunity newCont=trigger.newMap.get(theId);
String oldStatus=oldCont.Status__c;
String newStatus=newCont.Status__c;
System.debug('############ Old status = ' + oldStatus + ', new status = ' + newStatus);
if ( (null!=oldStatus) && ('new'==oldStatus) &&
(null!=newStatus) && ('working'==newStatus) )
{
newCont.RecordTypeId=theRT;
}
}
}

 

Any suggestions?

 

Thank you so much for the help!!

AmitSahuAmitSahu

Hi,

 

Are you sure if the code you have added here works ?

 

 

AdrianCCAdrianCC

Hello Palos,

 

Try this:

trigger recordtypechange on Opportunity (after insert, after update){
	//get the id of the required RecordType
	String workingRecTypeId = [SELECT Id FROM RecordType WHERE Name='PreRFP' AND sObjectType='Opportunity' LIMIT 1].Id;
	
	List<Opportunity> updatedOpptiesList = new List<Opportunity>();
	for (Opportunity oppty: trigger.new) {
		if ((oppty.Stage == 'Working') && (oppty.RecordTypeId != workingRecTypeId)) {
			oppty.RecordTypeId = workingRecTypeId;
			updatedOpptiesList.add(oppty);
		}
	}
	
	try {
		if (updatedOpptiesList.size() > 0) {
			update updatedOpptiesList;
		}
	} catch(Exception ex) {
		System.debug(ex.getMessage());
	}
}

 This will essentially change the RecordType of an Opportunity when the Stage changes to "Working".

Questions?

 

Thanks,

Adrian

PalosPalos

Thanks for the help...Unfortunately I am getting the error below when I use the code you provided.

 

ErrorError: Compile Error: Invalid field Stage for SObject Opportunity at line 7 column 14 

PalosPalos

Hi Jo20,

 

I do not see the sample code you provided...

 

thanks

PalosPalos

Hi AdrianCC,

 

I was able to fix the code you provided by changing the oppty.Stage with oppty.Stagename but I am getting the error below when I test it out.

 

Error:Apex trigger recordtypechange caused an unexpected exception, contact your administrator: recordtypechange: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.recordtypechange: line 8, column 1

 

Any suggestion?

AmitSahuAmitSahu

Make sure the record type you are going to assign to that record should be visible to the profile you are using for testing. The error says that record is readonly. Is there any approval process working for this kind of Opportunity ?

PalosPalos

 

Hi Jo20,

 

Thank you so much for yout help!

 

Yes there are approval processes associated to the Stage field but the criterias are for the other entries of this picklist and not for the entries that were used on this trigger. 

Rahul Joshi 2Rahul Joshi 2
I too was getting the below error, even after disabling the Workflow rule:
Error:Apex trigger recordtypechange caused an unexpected exception, contact your administrator: recordtypechange: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.recordtypechange: line 8, column 1

- To resolve I changed 'after insert, after update' to 'before insert, before update'
trigger recordtypechange on Opportunity (before insert, before update)

- And, commented below code:
13    try {
14        if (updatedOpptiesList.size() > 0) {
15            update updatedOpptiesList;
16        }
17    } catch(Exception ex) {
18        System.debug(ex.getMessage());
19    }