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
DJP1SDJP1S 

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;

}

 

SFFSFF

Try this:

 

trigger buAccount on Account (before update)
{
    // Get a list of record types.
    list<RecordType> typeList = 
        [SELECT Id, DeveloperName FROM RecordType 
          WHERE DeveloperName IN ('NEWRECORDTYPE', 'OLDRECORDTYPE')
            AND SObjectType = 'Account'];
    // Map them for easy, portable reference.
    map<Id, RecordType> typeMap = new map<Id, RecordType>();
    if (typeList.size() == 2)
    {
        for (RecordType rt : typeList)
        {
            typeMap.put(rt.DeveloperName, rt);
        }
    }
    Account oldAccount;
    for (Account acc : trigger.new)
    {
        // If the Type is about to change to Client - Active,
    	if (acc.Type == 'Client - Active')
    	{
    	    oldAccount = trigger.oldMap.get(acc.Id);
    	    if (oldAccount != null)
    	    {
            	// And the Record Type on the Account used to be OLDRECORDTYPE,
            	if (oldAccount.RecordTypeId == typeMap.get('OLDRECORDTYPE'))
                {
                    // Then automatically change the Record Type to NEWRECORDTYPE.
                    acc.RecordTypeId = typeMap.get('NEWRECORDTYPE');
                }
            }
        }     	   
    }
}

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!

 

 

BritishBoyinDCBritishBoyinDC

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)

 

trigger AccountRecordType on Account (before update) {
    
List<RecordType> getRecordType = new List<RecordType>([SELECT Id, Name FROM RecordType WHERE DeveloperName = 'Prospect_Page_2_SEO']);
    
    for(Account acc : trigger.new){
        if (acc.Type == 'Prospect'){
             if (trigger.oldmap.get(acc.Id).RecordTypeId == '012300000018VPW') {
                    acc.RecordTypeId = getRecordType[0].Id;
                }
            }
        }          
   
}

 

SFFSFF

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!

N.V.V.L.Vinay KumarN.V.V.L.Vinay Kumar
trigger AccountRecordType on Account (before update) {
    
    RecordType getRecordType = [SELECT Id, Name FROM RecordType WHERE Name='The RecordType NAme you want to update' AND sObjectType = 'Account' LIMIT 1];  
    
    for(Account acc : trigger.new){
    	if (acc.Type == 'Client - Active'){
            for(Account aco : trigger.old){
            	if (aco.Account_RecordType__c == 'Prospect - Page 1 SEO'){
if(getRecordType != null){
aco.RecordTypeId = getRecordType.Id;
} } } } }
}


try this and if it works mark it as solution