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

  • Kumar

    Member
    December 29, 2017 at 12:16 pm

    Hey Shaik,

    You would to most probably write a trigger on Opportunity which saves the count of records created by a user in a custom setting or something. From where you could validate that if a user has created 15 records on Opportunity or not.

    Thanks

  • 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

  • Kumar

    Member
    January 2, 2018 at 1:57 pm

    Thanks you Mohit! Didn't strike me to use aggregate queries to group records by OwnerIds.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos