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
Merve UnalMerve Unal 

Trigger dev help

Hello, 
I need to develop a trigger on this. Can anyone help me with that?

Develop a Trigger on Service-Consultant object which will throw an error if the Consultant Selected on
Service-Consultant Record already has an Service against his name. i.e - For a Consultant there will be only one Service at a time. Reject Duplicate Bookings.
Q1 - In which object the Trigger will be ( Service-Consultant) Q2 - What are the trigger events ( before insert, before update) Output - Check the duplicate bookings and throw the error.
Mahesh GorrepatiMahesh Gorrepati
If any optimisation is required. please, do suggest
 

Considering :- 1.Service Consult as child Object
                        2.Consultant As the Parent Object


Trigger :-  
trigger SercviceConsultantDuplicateCheck ON Service_Consultant__c(before insert,before update)
{
    SercviceConsultantDuplicateCheckHelper.CheckForDuplicates(Trigger.new);

}

Trigger Handler Class :- 
  Public class  SercviceConsultantDuplicateCheckHelper
{
public static void  CheckForDuplicates(list<Service_Consultant__c> lstRecords)
{
set<id> consultantsids = new set<id>();
 for( Service_Consultant__c  SC : lstRecords)
         {
               consultantsids.add(sc.Consultant__c);
           }
map<id,decimal> lstserviceconsultantcounts = new map<id,decimal>();
for( Consultant__c con :[select id,name,(select id,name from Service_Consultants__r) from Consultant__c])
           {
               if(! lstserviceconsultantcounts.iscontainskey(con.id))
                    {
                      lstserviceconsultantcounts.put(con.id,con.Service_Consultants__r.size());
                    }
              else
                   {
                         lstserviceconsultantcounts.put(con.id, lstserviceconsultantcounts.get(con.id)+1);
                   }
                   

            }

for(Service_Consultant__c SC : lstRecords)
          {
                if(lstserviceconsultantcounts.get(SC.Consultant__c)>1)
                   {
                         SC.addError('YOU can't create two Have two services for a singlec consultant');
                 
                     }


          }

}

}
 
vikas singh 158vikas singh 158
hi Merve Unal
if you write a trigger only for shaking duplicate values then you can do without a trigger when you create the field you have a check box to check the duplicate value should check this check box after that you don't need to write a trigger 
SubratSubrat (Salesforce Developers) 
Hello Merve ,

Certainly! Here's an example trigger on the Service_Consultant__c object that will throw an error if a consultant already has a service associated with their name:
trigger CheckDuplicateBookings on Service_Consultant__c (before insert, before update) {
    Set<Id> consultantIds = new Set<Id>();

    // Collect all consultant IDs from the records being inserted or updated
    for (Service_Consultant__c sc : Trigger.new) {
        consultantIds.add(sc.Consultant__c);
    }

    // Query existing services associated with the consultants
    List<Service_Consultant__c> existingServices = [
        SELECT Id
        FROM Service_Consultant__c
        WHERE Consultant__c IN :consultantIds
    ];

    // Map consultant IDs to the number of existing services
    Map<Id, Integer> consultantServiceCounts = new Map<Id, Integer>();
    for (Service_Consultant__c existingService : existingServices) {
        Id consultantId = existingService.Consultant__c;
        if (!consultantServiceCounts.containsKey(consultantId)) {
            consultantServiceCounts.put(consultantId, 0);
        }
        consultantServiceCounts.put(consultantId, consultantServiceCounts.get(consultantId) + 1);
    }

    // Check for duplicate bookings and throw an error if found
    for (Service_Consultant__c sc : Trigger.new) {
        Integer serviceCount = consultantServiceCounts.get(sc.Consultant__c);
        if (serviceCount != null && serviceCount > 0) {
            sc.addError('A service already exists for this consultant. Duplicate bookings are not allowed.');
        }
    }
}
The trigger first collects the consultant IDs from the Consultant_c field of the Service_Consultant_c records being processed. It then queries the existing services associated with these consultants.

A map called consultantServiceCounts is used to keep track of the number of existing services for each consultant. If a consultant already has a service, the count is incremented.

Finally, the trigger iterates over the Service_Consultant__c records being processed and checks if the consultant already has a service (based on the count in consultantServiceCounts). If a duplicate booking is found, an error is added to the record, preventing it from being inserted or updated.

Remember to replace Service_Consultant__c with the actual API name of your object in the trigger code.

Hope this helps !
Thank you.