You need to sign in to do that
Don't have an account?

how to prevent duplication of records based on two fields
help please. how can i prevent duplication of records based on two fields?
Objects Fields
Schedule__c
Seat__ Schedule__c(lookup)
Reservation__c Seat__c(lookup), Schedule__c(lookup), passenger_name__c
i have this code:
but it only prevents the same seat # for the same name, for example, if there is already a record on Reservation__c(Passenger_Name__c='test1', Seat__c='1', Schedule__c='sched001') how can i prevent Reservation__c(Passenger_Name__c='test2', Seat__c='1', Schedule__c='sched001') from saving?
help!
Objects Fields
Schedule__c
Seat__ Schedule__c(lookup)
Reservation__c Seat__c(lookup), Schedule__c(lookup), passenger_name__c
i have this code:
trigger PreventSeatDuplication on Reservation__c (before insert, before update) {
Map<String, Reservation__c> resMap = new Map<String, Reservation__c>();
for (Reservation__c res : System.Trigger.new) {
if ((res.Seat__c != null) && (System.Trigger.isInsert || (res.Seat__c != System.Trigger.oldMap.get(res.Id).Seat__c))) {
if (resMap.containsKey(res.Seat__c)) {
res.Seat__c.addError('Seat not available.');
} else {
resMap.put(res.Seat__c, res);
}
}
}
for (Reservation__c res : [SELECT Seat__c FROM Reservation__c WHERE Seat__c IN :resMap.KeySet()]) {
Reservation__c newRes = resMap.get(res.Seat__c);
newRes.Seat__c.addError('Seat not available.');
}
}
Map<String, Reservation__c> resMap = new Map<String, Reservation__c>();
for (Reservation__c res : System.Trigger.new) {
if ((res.Seat__c != null) && (System.Trigger.isInsert || (res.Seat__c != System.Trigger.oldMap.get(res.Id).Seat__c))) {
if (resMap.containsKey(res.Seat__c)) {
res.Seat__c.addError('Seat not available.');
} else {
resMap.put(res.Seat__c, res);
}
}
}
for (Reservation__c res : [SELECT Seat__c FROM Reservation__c WHERE Seat__c IN :resMap.KeySet()]) {
Reservation__c newRes = resMap.get(res.Seat__c);
newRes.Seat__c.addError('Seat not available.');
}
}
but it only prevents the same seat # for the same name, for example, if there is already a record on Reservation__c(Passenger_Name__c='test1', Seat__c='1', Schedule__c='sched001') how can i prevent Reservation__c(Passenger_Name__c='test2', Seat__c='1', Schedule__c='sched001') from saving?
help!
<pre>
//resMap maps a schedule to the full list of seats taken for that schedule
Map<String, Set<String>> resMap = new Map<String, Set<String>>();
Set<String> schedules = new Set<String>();
//build set of all schedules in play
for (Reservation__c res : System.Trigger.new) {
schedules.add(res.Schedule__c);
}
//for this, i'm assuming you can't be inserting or updating a reservation unless a schedule has already been created
//i'm getting the full list of reservations for each schedule in the context of this trigger
for (Reservation__c res : [SELECT Schedule__c, Seat__c FROM Reservation__c WHERE Schedule__c IN :schedules.KeySet()]){
Set<String> seats = resMap.get(res.Schedule__c);
if(seats != null){
seats.add(res.Seat__c);
}else{
resMap.put(res.Schedule__c, new Set<String>{res.Seat__c});
}
}
//now we know what seats are booked for each schedule, now we can compare to our trigger
for(Reservation__c res : Trigger.new){
String seatNum = res.Seat__c;
String schedule = res.Schedule__c;
//get all booked seats for current schedule in play
Set<String> seatsThatAreTaken = resMap.get(schedule);
//if seat is already booked
if(seatsThatAreTaken.contains(seatNum){
res.addError('Seat\'s Taken');
}
}
</pre>
You might also want to add a check in that last loop that looks for if the same seat is being booked twice in the trigger, because I didn't put something for that in. But should be easy to handle.