You need to sign in to do that
Don't have an account?
mayur Kalbande
trigger on only one record is created per year for each user
Here is One custom object name is target setting on that object distributor name(name. of distributor) field is present which is lookup to target setting object. (distributor(parent) & target setting(child)). so user have only permit to create only one record per year for user(distributor) otherwise show error(if user try to create another record with same distributor show error 'you can't create more than one record for + distributor name. in present year')
you can try below piece of code :
trigger distributorTrigger on Distributor__c(before insert){
Id currentUserId = UserInfo.getUserId();
List<Distributor__c>currentYeardistList =[SELECT Id,createdDate from Distributor__c where createdDate = THIS_YEAR AND createdBy.Id= :currentUserId];
if(currentYeardistList != NULL && currentYeardistList.size()>0){
for(Distributor__c dist : trigger.new){
dist.addError('current year distributor record is already present!');
}
}
}
Please mark above answer as best answer if it resolves your query. @mayur.
Thanks,
Dipika