function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prakhyat sapraPrakhyat sapra 

In the Visit__c object, if more than 2 visit are made in the name of the same oner, then the error is given that you can't create more than 2 visit by the same onner ????by trigger

In the Visit__c object, if more than 2 visit are made in the name of the same oner, then the error is given that you can't create more than 2 visit  by the same onner ????by trigger
AnkaiahAnkaiah (Salesforce Developers) 
Hi Prakhyat,

Do you want to check based on record owner or record name?

Thanks!!
 
Prakhyat sapraPrakhyat sapra
I have to check based on the owner
Prakhyat sapraPrakhyat sapra
I have to check based on the owner
AnkaiahAnkaiah (Salesforce Developers) 
If its baed on owner then try with below code.
trigger restrictVisitcreation on Visit__c (after insert) {
    
    set<id> ownerids = new set<id>();
    
    for(Visit__c acc:trigger.new){
        
        ownerids.add(acc.OwnerId);       
    }
    
    List<Visit__c> acclist = [select id,ownerid from Visit__c where ownerid=:ownerids ];
    
    for(Visit__c acc: trigger.new){
        
        if(acclist.size()>2){
           acc.adderror('you cant create more than two Visits'); 
        }
    }
    
}

If this helps, Please mark it as best answer.

Thanks!!