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
Jack daniel 16Jack daniel 16 

Show Error Message

How to show an error message 'Not inserted more than 20 Account in the current month and current user'.
Best Answer chosen by Jack daniel 16
Ajay K DubediAjay K Dubedi
Hi Jack,

You can use the below code.

I have written a trigger as per your requirement.  

<<<<<----Trigger ---->>>>>
trigger ErrorMessage on Account (before insert) {
if(Trigger.isInsert && Trigger.isBefore){

<<<<<----Apex Class------>>>>>>
public class User_Account_Count {
    public static void count_Account_Mounth(List<Account> accListInsert){
            String userId = UserInfo.getUserId();
            List<Account> accList = new List<Account>();
            accList = [SELECT Id, Name FROM Account WHERE OwnerId =: userId AND CreatedDate = THIS_MONTH];
            if(accList.size() > 20){
                for(Account acc : accListInsert){
                    acc.adderror('Cannot insert morethan 20 account in a month');
        }catch(Exception exp){
            System.debug('Exception Cause---'+exp.getMessage() + 'Line Number---'+exp.getLineNumber());

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi