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
BigPBigP 

Can someone explain this code?

public class VisitTriggerHandler {
           
    public static void sTime (List<visit__c> newVisits){
        
        set<id> physicianId = new set<id>();
        
        for (visit__c vis : newVisits) {
            physicianId.add(vis.OwnerId);
        }
        
        List<Visit__c> oldVisits = [
            SELECT id,Name, ownerid, Visitation__c, Timespan__c, Visitation_End__c
            FROM visit__c
            Where ownerid in : physicianId
        ];
        
       
        
        
        for (visit__c vis : oldVisits) {
            for(visit__c visit : newVisits){
                
                if(visit.Visitation__c == vis.Visitation__c){
                    visit.Visitation__c.addError('The date you have selected is not available');
                    
                }
            }
                
        }
Best Answer chosen by BigP
Karan_JainKaran_Jain
Hey Enea 
In this trigger first, you receive the list of (custom object - visit__c ) newVisits 
after that you create a Set  name -> 'physicianID'  datatype -> Id (Set is created to store Unique data ) 

in first for each loop you Iterate on - newVisits list and get the OwnerId and stored it in the Set that you created before 

after for each you created a List - oldVisits or stored the records using  query on the object (visit__c ) and retrieved the records related to OwnerId which we stored in Set 

now there is a nested for each loop first for each loop iterate on the old visit list and the second loop iterates on the newVisits list 
and get the Visitation list both old and new value 
and in if the part we check that ( visit.visitation__c == vis.visitation__c ) then it will throw an error that means if the old value or new value is same then it will throw an error like - ' the date you have selected is not available ' 

thanks