0

I am working on the requirement where I have to setup the limit on number of records created on 10 custom objects.

So I have created one object called Limit__c which contains available_limit__c and required_limit__c fields.

Suppose there is an object Demo1 for which available limit is 5 records and if user try to create more than 5 records then it throws error message.

Same need to be done for all 10 objects.

I know this can be achieved by trigger on that object.

But do I need to write separate trigger for each object or there's any thing through which I need to write one trigger and can controll all the objects.

I have Limit__c object from where I can get the available limit for each object.

0

1 Answer 1

1

Each object requires its own trigger. That's how the system works. But you can, with some careful effort, make a polymorphic utility class that can handle all of the various types. The triggers would look something like:

trigger checkLimitsForObjectX on ObjectX (before insert) {
  LimitChecker.checkFor(Trigger.new);
}

The class itself would use dynamic queries, etc:

public class LimitChecker {
  public static void checkFor(SObject[] records) {
    String query = 'select id from '+records.getSObjectType()+' where ...';
    SObject[] results = Database.query(query);
    // figure out if we need to add errors
  }
}

Since you've not provided more specific details (e.g. time limit, etc), this is about the best I can offer you right now. You'll want to read more about Dynamic Apex for specifics.

2
  • My aim is simple instead of writing separate triggers I want it to achieve dynamically. for example: I want user won't be able to create more than 5 records on Account,Contact,Opportunity, case objects. Commented Jan 4, 2019 at 10:25
  • @AshutoshTripathi again, triggers need to be deployed per object. You can make a common utility class, but there would still be one trigger per object for this.
    – sfdcfox
    Commented Jan 4, 2019 at 14:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .