You need to sign in to do that
Don't have an account?
Anthony Zirilli 10
Trigger blanks field instead of populating it when doing mass update
I have this trigger below that works to update a field with the active opportunity, but when I do a mass upload and I go to the account the field is blank. When I click edit and save the field is populated again with the active opportunity. The mass upload does not throw any errors and I belive the code is bulkified. Any ideas of why this is happening and how to fix it?. Thank you!
trigger act_opp_membership on Account (before insert, before update) { List<Opportunity> oppList = new List<Opportunity>([SELECT Id, Name,AccountId FROM Opportunity WHERE AccountID in : trigger.new AND ((RecordType.Name = 'Membership Renewal' AND Active_Membership__c = 'Active') OR (RecordType.Name = 'Membership Prospect' AND Web_Expire_Date__c != null) OR (RecordType.Name = 'Hill Site License' AND Membership_Web_Access__c=TRUE AND Membership_Start_Date__c<=today AND Membership_Expire_Date__c>=today) OR (RecordType.Name = 'Complimentary Access' AND Membership_Web_Access__c=TRUE AND Membership_Start_Date__c<=today AND Membership_Expire_Date__c>=today)) ORDER BY Web_Expire_Date__c DESC NULLS LAST LIMIT 1]); Map<Id,Opportunity> accIdwithOpp = new Map<Id,Opportunity>(); for(Opportunity opp : oppList){ if(!accIdwithOpp.containsKey(opp.AccountId)){ accIdwithOpp.put(opp.AccountId,opp); } } for(Account acc : trigger.new){ if(accIdwithOpp.containsKey(acc.id)){ acc.Active_Membership_Opportunity__c = accIdwithOpp.get(acc.id).id; } else{ acc.Active_Membership_Opportunity__c = null; } } }
Krishna is absolutely right, You will not get any ID in before trigger that is why values are not updated while insert.
I have modified your trigger code, please find it below :
Hope it will help you.
Let me know if you have any issue in it.
Thanks,
Abhishek
That is because of a syntactical error. Replace 30th line of Abhishek's code to this:
Thanks!
Yes shyama is right, there was a syntax error on line no. 30.
Please find below the updated code : Let me know if you have any other issue in it.
Thanks,
Abhishek