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
Omo PhalayiOmo Phalayi 

Update Field based on recordtype in Salesforce using Apex

Hi All, 
Please, I am trying to update an account field using a trigger based on recordtype. The requirement is if  the Reocrdtype is AccOneA and the Active field on the Account is "Yes" update the description. 
Please see my code below

trigger AccountTriggerB on Account (before insert, before Update) {
    for(Account Acc:Trigger.New){
        if(Acc.RecordType.DeveloperName=='AccOneA' && Acc.Description ==Null){
            Acc.Description ='Please lets us know if there is any concern.Thanks';
        }
    }
    
}
 
Best Answer chosen by Omo Phalayi
Khan AnasKhan Anas (Salesforce Developers) 
Hi Omo,

Greetings to you!

When you are going through a loop in a trigger you don’t have access to Record Type name like RecordType.Name, you can only access Id as RecordTypeId.

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger updateField on Account (before insert, before update) {
    
    Id rTypeId = Schema.SObjectType.Account.RecordTypeInfosByName.get('Record Type Name').RecordTypeId;
    
    for(Account acc : Trigger.new) {
        if(acc.RecordTypeId == rTypeId && acc.Active__c==Yes'' ) { . // add more conditions here
            acc.Description = 'Please let us know if there is any concern. Thanks';
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Omo,

Greetings to you!

When you are going through a loop in a trigger you don’t have access to Record Type name like RecordType.Name, you can only access Id as RecordTypeId.

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger updateField on Account (before insert, before update) {
    
    Id rTypeId = Schema.SObjectType.Account.RecordTypeInfosByName.get('Record Type Name').RecordTypeId;
    
    for(Account acc : Trigger.new) {
        if(acc.RecordTypeId == rTypeId && acc.Active__c==Yes'' ) { . // add more conditions here
            acc.Description = 'Please let us know if there is any concern. Thanks';
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Omo PhalayiOmo Phalayi
Thanks Khan.
The solution works
Adila Hilal 9Adila Hilal 9
Hi,

Right now I am working on a requirement where we have to automate a process to grant temperory login access to users with System admin privilages for specified period of time. I have followed a process explained in a blog https://jenwlee.wordpress.com/2015/08/23/how-to-use-flow-and-process-builder-to-temporarily-grant-system-admin-rights-to-a-user/. I followed the process but didnt get the expected results , any thoughts would be appreciated.