You need to sign in to do that
Don't have an account?
DJP1S
Trigger to Change Record Type on Account not working...
I have no clue why this won't work. I've tried it a couple ways. Basically, I'm just trying to change the record type of the account record to a specific record type whnever certain conditions are met. Is this even possible?
The first:
trigger AccountRecordType on Account (after update) { List<RecordType> getRecordType = new List<RecordType>([SELECT Id, Name FROM RecordType WHERE Id = '01240000000DciMAAS']); List<Account> updateAccounts = new List<Account>(); for(Account acc : trigger.new){ if (acc.Type == 'Client - Active'){ for(Account aco : trigger.old){ if (aco.Account_RecordType__c == 'Prospect - Page 1 SEO'){ aco.RecordType = getRecordType[0]; updateAccounts.add(aco); } } } } update updateAccounts; }
And this way:
trigger AccountRecordType on Account (after update) { List<Account> updateAccounts = new List<Account>(); for(Account acc : trigger.new){ if (acc.Type == 'Client - Active'){ for(Account aco : trigger.old){ if (aco.Account_RecordType__c == 'Prospect - Page 1 SEO'){ aco.RecordTypeId = '01240000000DciMAAS'; updateAccounts.add(aco); } } } } update updateAccounts; }
Try this:
There's some issues with your code:
1. You should do this in a before-update trigger.
2. Never directly reference a record ID in your code. It's just wrong.
3. You don't need to execute an update at all - just change the record type in the before trigger and the update will take care of itself.
4. When working with record types, always use the DeveloperName - it's much safer than Name and WAY safer than ID.
5. Don't forget the SObjectType when looking up a record type - you can have different record types with the same names on different objects.
Good luck!
It is certainly possible...though I would try doing this via Workflow before you code it - it should work I think...
If not, I am not sure I follow the logic...
As written, you loop through the new version of the updated accounts, and if the value for an updated account = 'Client - Active', you then loop through all the accounts in trigger.old and update every record in that collection to have the new Record Type - which I am guessing is not the desired behavior...
If you are trying to check that the new value for Type = 'Client - Active' and the old value fo Record_Type__c for this particualr account was = 'Prospect - Page 1 SEO', then you should trigger.oldmap to reference the same record in both places...something like the quick example below, and change the trigger to be before update, not after, so you don't need to execute another DML call. I would also suggest removing the ID references unless you are sure they are the same in the live system (can you access RecordType info via Describe calls)
If you want to look up record types without burning a query, you can try this:
http://www.salesforcefast.com/2012/03/look-up-record-type-ids-with-no-queries.html
If you do this, you'll have to use the Name instead of the DeveloperName - it's a quirk in the getRecordTypeInfosByName() method.
Good luck!