Activity Forums Salesforce® Discussions How to restrict a user to not create more than 15 records in Salesforce? Reply To: How to restrict a user to not create more than 15 records in Salesforce?

  • Mohit

    Member
    January 2, 2018 at 1:50 pm

    Hi Shaik,
    I have done this functionality in my org, with the help of apex trigger.

    This is the following code to achieve this functionaity.

    trigger toCheckNumberOfUser On Opportunity(before Insert, before update){
    Map<Id,Integer> mapofOwnerIdVsCount = new Map<Id,Integer>();
    for(AggregateResult arOpp : [select count(id),OwnerId from Opportunity Group BY OwnerId HAVING Count(id)>0] ){
    mapofOwnerIdVsCount.put(Id.valueOf(String.valueOf(arOpp.get('OwnerId'))),Integer.valueOf(arOpp.get('expr0')));
    }
    for(Opportunity opp: Trigger.new){
    if(mapofOwnerIdVsCount.get(opp.OwnerId)>15){
    opp.addError('Number of Opportunity on this User is greater than 15');
    }
    }
    }

    Thanks