Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. Trigger updateContatoPonto on Ponto__c (before insert, before update) {
  2.  
  3. // build a set of receivable Inscricao_Numero__c
  4. Set <Decimal> receberInscricaoNumero = new Set <Decimal> ();
  5.  
  6. for(Ponto__c pontos : trigger.New)
  7. {
  8. receberInscricaoNumero.add(pontos.Inscricao_Numero__c);
  9. }
  10.  
  11. // Map the contacts Inscricao_Numero__c and the actual contacts records based on the receivable Inscricao_Numero__c
  12. Map <Decimal, Contact> matchingcontactsMap = new Map <Decimal, Contact> ();
  13. for (Contact contacts : [Select Id, Inscricao_Numero__c From Contact Where Inscricao_Numero__c IN :receberInscricaoNumero])
  14. {
  15. matchingcontactsMap.put(contacts.Inscricao_Numero__c, contacts);
  16. }
  17.  
  18. List <Ponto__c> receivablesToUpdate = new List <Ponto__c> ();
  19. for(Ponto__c pontos : trigger.New)
  20. {
  21. if (matchingcontactsMap.get(pontos.Inscricao_Numero__c) != null)
  22. {
  23. pontos.Contato__c = matchingcontactsMap.get(pontos.Inscricao_Numero__c).Id;
  24. }
  25. else {
  26. pontos.Contato__c = null;
  27. }
  28. }
  29. update receivablesToUpdate;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement