You need to sign in to do that
Don't have an account?
HARSHIL 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.
Thank you for the help!
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!
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
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.');
}
}
}
Your welcome.
If you have any query in future then you can directly drop a mail to me at dpkm20@gmail.com
Thank you very much for the support! Appreciated!