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
HARSHIL U PARIKHHARSHIL U PARIKH 

Trigger Not Firing on Lead: Object which make sure that one user can not create more than 100 leads per day.

Hello Developers

I am writing trigger in which it stops user to create a lead record if he/she enters more than 100 records per day.

trigger: currently i am doing it on two records since it's easy to test.:)  but however, trigger is not getting fired and it allows me to create more than 2 opportunities per day.
 
Trigger NoMoreThan100LeadsPerDayPerUser On Lead(Before Insert){
    // We need to write a trigger so there should be no more then 100 leads per day per user.
    // In other words one user can only create no more than 100 leads per day.
    Id userId;
    
    If(Trigger.IsInsert){
        For(Lead Ld: Trigger.New)
        {
            userId = Ld.OwnerID;
            List<Lead> TodaysLead = [Select ID FROM Lead 
                                        WHERE CreatedDate = :Date.Today() AND OwnerID = :UserId LIMIT 3];
            
            If(TodaysLead.size() > 2)
            {
                Ld.addError('You have rich the limit for creating a leads for today.');
            }
        }
    }
}

Thank you for the help!
Best Answer chosen by HARSHIL U PARIKH
Deepak Maheshwari 7Deepak Maheshwari 7
trigger MaxLead on Lead (before insert) {
    Integer maxLead = 4;
    

    
        Set<Id> userIds = new Set<Id>();
        Map<Id, Integer> leadCountMap = new Map<Id, Integer>();

        for (Lead c: trigger.new) {
            userIds.add(c.OwnerId);
            leadCountMap.put(c.OwnerId, 0);
        }

        Map<Id, User> userMap = new Map<Id, User>([
            select Name
            from User
            where Id in :userIds
        ]);

        for (AggregateResult result: [
            select count(Id),
                OwnerId
            from Lead
            where CreatedDate = Today and
                OwnerId in :userIds
            group by OwnerId
        ]) {
            leadCountMap.put((Id) result.get('OwnerId'), (Integer) result.get('expr0'));
        }

        for (Lead c: trigger.new) {
            leadCountMap.put(c.OwnerId, leadCountMap.get(c.OwnerId) + 1);

            if (leadCountMap.get(c.OwnerId) > maxLead) {
                c.addError('You have rich the limit for creating a leads for today.');
            }
        }
    

All Answers

Deepak Maheshwari 7Deepak Maheshwari 7
trigger MaxLead on Lead (before insert) {
    Integer maxLead = 4;
    

    
        Set<Id> userIds = new Set<Id>();
        Map<Id, Integer> leadCountMap = new Map<Id, Integer>();

        for (Lead c: trigger.new) {
            userIds.add(c.OwnerId);
            leadCountMap.put(c.OwnerId, 0);
        }

        Map<Id, User> userMap = new Map<Id, User>([
            select Name
            from User
            where Id in :userIds
        ]);

        for (AggregateResult result: [
            select count(Id),
                OwnerId
            from Lead
            where CreatedDate = Today and
                OwnerId in :userIds
            group by OwnerId
        ]) {
            leadCountMap.put((Id) result.get('OwnerId'), (Integer) result.get('expr0'));
        }

        for (Lead c: trigger.new) {
            leadCountMap.put(c.OwnerId, leadCountMap.get(c.OwnerId) + 1);

            if (leadCountMap.get(c.OwnerId) > maxLead) {
                c.addError('You have rich the limit for creating a leads for today.');
            }
        }
    
This was selected as the best answer
HARSHIL U PARIKHHARSHIL U PARIKH
Thank you it worked!
Deepak Maheshwari 7Deepak Maheshwari 7

Your welcome.

If you have any query in future then you can directly drop a mail to me at dpkm20@gmail.com

HARSHIL U PARIKHHARSHIL U PARIKH
Deepak,
Thank you very much for the support! Appreciated!