You need to sign in to do that
Don't have an account?
Palos
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
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
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!!
Hi,
Are you sure if the code you have added here works ?
Hello Palos,
Try this:
This will essentially change the RecordType of an Opportunity when the Stage changes to "Working".
Questions?
Thanks,
Adrian
Thanks for the help...Unfortunately I am getting the error below when I use the code you provided.
Error: Compile Error: Invalid field Stage for SObject Opportunity at line 7 column 14
Hi Jo20,
I do not see the sample code you provided...
thanks
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?
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 ?
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.
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 }