Guest User

Untitled

a guest
Mar 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. trigger UpdateProjectNo on Project__c (before update) {
  2. //Look for project status update. Check if its associated to junction object
  3. //Update no of projects field in Account object
  4. for (Project__c p : Trigger.new) {
  5. if (p.status__c != Trigger.oldMap.get(p.id).status__c) {
  6. if (p.Status__c == 'Active') {
  7. //List related records from junction object
  8. List<Account_or_Project__c> junctionId = [select id,Account_Name__c
  9. from Account_or_Project__c
  10. where Project_Name__c = :p.id];
  11. if (junctionId.size() > 0) {
  12. //Loop through the list of records
  13. for (integer i = 0; i < junctionId.size(); i++) {
  14. //Get related Accounts
  15. List<Account> acc = [select id,No_Of_Projects__c from Account
  16. where id = :junctionId.get(i).Account_Name__c];
  17. for (Account myAcc : acc) {
  18. myAcc.No_Of_Projects__c++;
  19. update myAcc;
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }
Add Comment
Please, Sign In to add comment