You need to sign in to do that
Don't have an account?
Merve 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.
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.
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');
}
}
}
}
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
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: 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.