+ Start a Discussion
mayur Kalbandemayur 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')
mayur Kalbandemayur Kalbande
somebody help me
Dipika RajputDipika Rajput
@mayur you can create trigger on Distributor object to fetch all the records from the org and check there created date if it is of current year or not. if currect record is already present then give error.

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