
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;
}
}
}
Hi Anthony,Yes shyama is right, there was a syntax error on line no. 30. Please find below the updated code :
trigger act_opp_membership on Account (after 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>();
List<Account> updateAccList = new List<Account>();
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;
if(trigger.isInsert){
updateAccList.add(acc);
}
}
else{
acc.Active_Membership_Opportunity__c = null;
if(trigger.isInsert){
updateAccList.add(acc);
}
}
}
if(updateAccList.size() > 0){
update updateAccList;
}
}
Let me know if you have any other issue in it.
Thanks,Abhishek