Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. trigger AutoStudentIDTrigger on Contact (before insert){
  2.  
  3. list<Contact> l1 =
  4.  
  5. [SELECT Id, Type_of_bursary_contact__c, Student_ID_New__c, Do_Not_Auto_Generate_Student_ID__c FROM Contact WHERE
  6. Type_of_bursary_contact__c = 'Student' AND RecordTypeId in (SELECT Id FROM RecordType WHERE Name = 'Bursary Contact') AND Student_ID_New__c != NULL
  7. ORDER BY Student_ID_New__c desc limit 1];
  8.  
  9. list<Contact> l2 =
  10.  
  11. [SELECT Id, Beneficiary_Number__c,Role_in_group__c FROM Contact WHERE
  12. RecordTypeId in (SELECT Id FROM RecordType WHERE Name = 'Community Contact') AND Beneficiary_Number__c != NULL
  13. ORDER BY Beneficiary_Number__c desc limit 1];
  14.  
  15. Integer l1size = l1.size();
  16. Integer l2size = l2.size();
  17.  
  18. Decimal NewLatestStudentID = 0.0;
  19. Decimal NewLatestBeneficiaryID = 0.0;
  20.  
  21. If(l1size > 0){
  22. try {
  23. NewLatestStudentID = l1[0].Student_ID_New__c;
  24. }
  25.  
  26. Catch (System.NullPointerException e) {
  27. System.debug('The following exception has occurred: ' + e.getMessage());
  28. }
  29.  
  30. }
  31.  
  32.  
  33. If(l2size > 0){
  34.  
  35. NewLatestBeneficiaryID = l2[0].Beneficiary_Number__c;}
  36.  
  37. If(trigger.isInsert){
  38. for(Contact li:Trigger.new) {
  39.  
  40. If(li.Type_of_bursary_contact__c == 'Student' & li.Do_Not_Auto_Generate_Student_ID__c == FALSE ){
  41.  
  42. li.Student_ID_New__c = NewLatestStudentID+1; }
  43.  
  44.  
  45. If(li.Role_in_group__c != Null & li.Do_Not_Auto_Generate_Beneficiary_Number__c == FALSE ){
  46.  
  47. li.Beneficiary_Number__c = NewLatestBeneficiaryID+1; }
  48.  
  49. }
  50.  
  51.  
  52. }
  53.  
  54.  
  55.  
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement