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
virkvirk 

Validation on inserting a duplicate record

I have master detail relationship between Book and Lents object. I want a control in order to avoida  book to be issued second time. I have added following code on Lent object creation before inserting I am checking if Book is already in OldMap it should give an error mesage else it should insert the record. But the code is not working:

 

trigger UpdateCount on Lent__c (before insert,after insert,after delete) {
List<Contact> counts_toUpdate = new List<Contact>();
Map<String, Integer> contact_newCount_Map=new Map<String, Integer>(); //for each contact the CountNumber

if(trigger.isinsert){
List<Lent__c> counts =[select Book__c,Contact__c, Contact__r.Count__c,Book__r.Name
from Lent__c where id IN :Trigger.new FOR UPDATE];

for(Lent__c lent:counts){
if((lent.Book__c!= null) && (lent.Book__c != Trigger.oldMap.get(lent.id).Book__c))
{
lent.Book__c.addError('Another new lead has the same email address.');
}else{
contact_newCount_Map.put(lent.Contact__c, Integer.valueOf(lent.Contact__r.Count__c==null ? 0 : lent.Contact__r.Count__c) + 1);
}}
}
else if(trigger.isdelete){
Set<Id> contactIds = new Set<Id>();
for (Lent__c l : Trigger.old)
contactIds.add(l.Contact__c);
//List<Lent__c> counts = [select Contact__c, Contact__r.Count__c
//from Lent__c where id IN :contactIds];
Map<Id,Contact> oldContacts = new Map<Id,Contact>([SELECT Count__c From Contact where Id IN :contactIds]);
for(Lent__c lent : Trigger.old)
contact_newCount_map.put(lent.Contact__c, Integer.valueOf(oldContacts.get(lent.Contact__c).Count__c == null ? 0 : oldContacts.get(lent.Contact__c).Count__c ) -1);
}

for(Contact con : [select id, Count__c from Contact WHERE ID IN : contact_newCount_Map.KeySet()]){
con.Count__c=contact_newCount_Map.get(con.id);
counts_toUpdate.add(con);
}

update counts_toUpdate;
}

 

Someone please help.

harry.freeharry.free
What do you want to do? The code is hard to read.
virkvirk

I want to add a validation if a book is already issued it should not let the book issued second time.

harry.freeharry.free
Do you mean if a contact have already lent a book, then the contact should not be able to lent it again?
virkvirk
Yes if a book is already lent it should not be lent again.
harry.freeharry.free

I think the point is the comparison. You need to compare new lents with existing lents other then compare them with the lent in Trigger.oldMap.

I made two changes to your code:

1. added the code to get the books and their existing lents.

2. updated the comparison.

trigger UpdateCount on Lent__c (before insert,after insert,after delete) 
{
	List<Contact> counts_toUpdate = new List<Contact>();
	Map<String, Integer> contact_newCount_Map=new Map<String, Integer>(); //for each contact the CountNumber
// Start, gets books and their lents. Set<Id> bookIds = new Set<Id>(); for(Lent__c lent : Trigger.new) { bookIds.add(lent.Book__c); } Map<Id, Book__c> books = new Map<Id, Book__c>([select Id, (select Id from Lents__r) from Book__c where Id in :bookIds]); // End
if(Trigger.isInsert) { List<Lent__c> counts =[select Book__c,Contact__c, Contact__r.Count__c,Book__r.Name from Lent__c where id IN :Trigger.new FOR UPDATE]; for(Lent__c lent:counts) { // Updated the comparison. if(lent.Book__c!= null && books.get(lent.Book__c) != null && books.get(lent.Book__c).Lents__r != null && books.get(lent.Book__c).Lents__r.size() > 0) //if((lent.Book__c!= null) && (lent.Book__c != Trigger.oldMap.get(lent.id).Book__c)) { lent.Book__c.addError('Another new lead has the same email address.'); } else { contact_newCount_Map.put(lent.Contact__c, Integer.valueOf(lent.Contact__r.Count__c==null ? 0 : lent.Contact__r.Count__c) + 1); } } } else if(trigger.isdelete) { Set<Id> contactIds = new Set<Id>(); for (Lent__c l : Trigger.old) { contactIds.add(l.Contact__c); } //List<Lent__c> counts = [select Contact__c, Contact__r.Count__c //from Lent__c where id IN :contactIds]; Map<Id,Contact> oldContacts = new Map<Id,Contact>([SELECT Count__c From Contact where Id IN :contactIds]); for(Lent__c lent : Trigger.old) { contact_newCount_map.put(lent.Contact__c, Integer.valueOf(oldContacts.get(lent.Contact__c).Count__c == null ? 0 : oldContacts.get(lent.Contact__c).Count__c ) -1); } } for(Contact con : [select id, Count__c from Contact WHERE ID IN : contact_newCount_Map.KeySet()]) { con.Count__c=contact_newCount_Map.get(con.id); counts_toUpdate.add(con); } update counts_toUpdate; }

 

virkvirk

Now I am getting following error at line 25

 

UpdateCount: execution of AfterInsert caused by: System.FinalException: SObject row does not allow errors: Trigger.UpdateCount: line 25, column 1

 

for(Lent__c lent:counts)
{
// Updated the comparison.
if(lent.Book__c!= null && books.get(lent.Book__c) != null && books.get(lent.Book__c).Lents__r != null && books.get(lent.Book__c).Lents__r.size() > 0)
//if((lent.Book__c!= null) && (lent.Book__c != Trigger.oldMap.get(lent.id).Book__c))
{
lent.Book__r.addError('Another new lead has the same email address.'); //Line 25
}
else
{
contact_newCount_Map.put(lent.Contact__c, Integer.valueOf(lent.Contact__r.Count__c==null ? 0 : lent.Contact__r.Count__c) + 1);
}
}

 

Please help

harry.freeharry.free
Oh, please remove "after insert" in the line 1.