Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trigger UpdateProjectNo on Project__c (before update) {
- //Look for project status update. Check if its associated to junction object
- //Update no of projects field in Account object
- for (Project__c p : Trigger.new) {
- if (p.status__c != Trigger.oldMap.get(p.id).status__c) {
- if (p.Status__c == 'Active') {
- //List related records from junction object
- List<Account_or_Project__c> junctionId = [select id,Account_Name__c
- from Account_or_Project__c
- where Project_Name__c = :p.id];
- if (junctionId.size() > 0) {
- //Loop through the list of records
- for (integer i = 0; i < junctionId.size(); i++) {
- //Get related Accounts
- List<Account> acc = [select id,No_Of_Projects__c from Account
- where id = :junctionId.get(i).Account_Name__c];
- for (Account myAcc : acc) {
- myAcc.No_Of_Projects__c++;
- update myAcc;
- }
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment