+ Start a Discussion
Anthony Zirilli 10Anthony 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;
        }
    }
}

 
Anthony Zirilli 10Anthony Zirilli 10
Also note that the Active_Membership_Opportunity field is a lookup field if that has anything to do with it.
Krishna SambarajuKrishna Sambaraju
The Id field will be null before insert. The Id only gets generated after insert. You need to change your trigger accordingly.
Anthony Zirilli 10Anthony Zirilli 10
Hi Krishna, That makes sense. The only issue is I'm getting this error now when switching it to after insert: Apex trigger act_opp_membership caused an unexpected exception, contact your administrator: act_opp_membership: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.act_opp_membership: line 20, column 1 How should the code be updated? Thank you!
Abhishek BansalAbhishek Bansal
Hi Anthony,

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 :
 
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;
    }
}

Hope it will help you.
Let me know if you have any issue in it.

Thanks,
Abhishek
Anthony Zirilli 10Anthony Zirilli 10
Hey I'm getting this error:Error: Compile Error: Variable does not exist: update.updateAccList at line 30 column 9
Shyama B SShyama B S
Hi Anthony,
That is because of a syntactical error. Replace 30th line of Abhishek's code to this:
update updateAccList;

Thanks!
Abhishek BansalAbhishek Bansal
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