Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. Set<string> appnameSet = new set<string>();
  2. for(test1__c test : trigger.new)
  3. {
  4. appnameSet.add(test.appName__c);
  5. }
  6.  
  7. List<test1__c> testList = new List<test1__c >();
  8.  
  9. for(test1__c tt: [select appName__c from Test1__c where APPName__c IN :appnameSet])
  10. {
  11. for(test1__c tt1: trigger.new)
  12. {
  13. if(tt1.appName__c == tt.appName__c )
  14. {
  15. tt1.addError('Duplicate app name found') ;
  16. }
  17. }
  18.  
  19. }
  20.  
  21. List<Test1__c> testListinsert = new List<Test1__c>()
  22. testListinsert.add(new Test1__c(name="One",appName__c = 'APP1'));
  23. testListinsert.add(new Test1__c(name="Two",appName__c = 'APP1')); //Duplicate
  24. testListinsert.add(new Test1__c(name="Three",appName__c = 'APP1'));//Duplicate
  25. insert testListinsert;
  26.  
  27. Map<String, String> mapDup = Map<String, String>();
  28.  
  29. //first level filter if existing records to be inserted contains duplicates.
  30. for(test1__c test : trigger.new)
  31. {
  32. mapDup.containsKey(test.appName__c)
  33. {
  34. test.addError('Duplicate app name found') ;
  35. }
  36. else
  37. {
  38. mapDup.put(test.appName__c, test.appName__c);
  39. }
  40. }
  41.  
  42. //now check if records to be inserted is exist in database.
  43. AggregateResult[] lstResult = [ SELECT APPName__c, COUNT(Id),
  44. FROM Test1__c
  45. WHERE APPName__c IN:mapDup.keySet()
  46. GROUP BY APPName__c
  47. HAVING COUNT(Id)>0];
  48.  
  49. if(lstResult.size()>0)
  50. {
  51. trigger.new[0].addError('Duplicate app name found');
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement