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
Timothy Wade 26Timothy Wade 26 

How to enforce that users can only create one record on a custom object?

I created a custom object. How do I enforce that users cannot create more than 1 record on this custom object? Validation rule, flow, trigger???
AbhinavAbhinav (Salesforce Developers) 
Hi Timothy,

Check suggestion on this link for refernce:

https://developer.salesforce.com/forums/?id=906F0000000D82jIAC

https://salesforce.stackexchange.com/questions/56187/want-to-restrict-a-user-from-creating-more-than-10-records-w-o-rsf

https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000A86aESAR

Thanks!
Giancarlo BaileyGiancarlo Bailey
Thank you so much for sharing. It's very helpful for me.
Suraj Tripathi 47Suraj Tripathi 47
Hi Timothy,

Class__c is a Custom Object
trigger insertTriggerClass on Class__c (before insert) {
    insertClass.restrictInsertion(trigger.new);
}

public class insertClass {
    public static void restrictInsertion(List<Class__c> classList)
    {
        try{
            Set<Id>classID = new Set<Id>();
            for(Class__c c : classList)
            {
                classID.add(c.Id);
            }
            
            List<Class__c> cla = [Select Id from Class__c];
            List<Class__c> clas = new List<Class__c>();
            for(Class__c con : cla)
            {
                if(cla.Size() > 0)
                {
                    con.addError('You can not insert the record more than 1');
                }
                else
                {
                    con.Name='abc';
                    clas.add(con);
                }
            }
            insert clas; 
        }catch(Exception e)
        {
            system.debug('Exception '+e);
        }
        
    }
}

Thanks!