+ Start a Discussion
apsullivanapsullivan 

Trigger Works on Single Object But Not When Bulk Updating

I have a trigger on a custom object called Launch Checklist that is supposed to use a unique identifier to look up another custom object called App and that App's Account based on an ID field.  When I edit and save a single record, everything works swimmingly.  When I try to force a bulk upload (through Data Loader or Anonymous Apex), the Trigger just doesn't seem to fire at all, or worse, it associates the wrong app/account.

Again, though, if I save a single record with the wrong info, without making any changes, the association occurs correctly.  I've spent time to ensure my trigger is bulkified.  Can anyone help identify what I'm doing wrong?  Code is below.

trigger LaunchChecklist on Launch_Checklist__c (before insert, before update) {
    
    String appId = Null;
    Map <String, App__c> mapApp = new Map <String, App__c> ();
    
    for(Launch_Checklist__c lc: Trigger.new) {
        if (lc.Apple_ID__c != Null) {
            appId = lc.Apple_ID__c;
        } else if (lc.Package_Name__c != Null) {
            appId = lc.Package_Name__c;
        }
    }

    List<App__c> apps = new List<App__c>([select App_ID__c, Account__c from App__c where App_ID__c = :appId limit 1]);
    
    if (apps.size() > 0) {
        for (App__c app: apps)  {
        	mapApp.put(app.App_ID__c, app);
    	}
        for(Launch_Checklist__c lc: Trigger.new) {
            lc.Account__c = mapApp.get(appId).Account__c;
            lc.App__c = mapApp.get(appId).Id;
    	}
    }
}


Best Answer chosen by apsullivan
TejTej
trigger LaunchChecklist on Launch_Checklist__c (before insert, before update) {
    
    Set<String> AppId = new set<String>();
    Map <String, App__c> mapApp = new Map <String, App__c> ();
    
    for(Launch_Checklist__c lc: Trigger.new) {
        if (lc.Apple_ID__c != Null) {
            appId.add(lc.Apple_ID__c);
        } else if (lc.Package_Name__c != Null) {
            appId.add(lc.Package_Name__c);
        }
    }

    List<App__c> apps = new List<App__c>([select App_ID__c, Account__c from App__c where App_ID__c IN:appId ]);
    
    if (apps.size() > 0) {
        for (App__c app: apps)  {
        	mapApp.put(app.App_ID__c, app);
    	}
        for(Launch_Checklist__c lc: Trigger.new) {
	    
           if(lc.Apple_ID__c != Null){
	        lc.Account__c = mapApp.get(lc.Apple_ID__c).Account__c;
                lc.App__c = mapApp.get(lc.Apple_ID__c).Id;
	    }else if(lc.Package_Name__c != null){
	    	
		lc.Account__c = mapApp.get(lc.Package_Name__c).Account__c;
                lc.App__c = mapApp.get(lc.Package_Name__c).Id;
	    }
	    
    	}
    }
}
try this and see..

All Answers

TejTej
trigger LaunchChecklist on Launch_Checklist__c (before insert, before update) {
    
    Set<String> AppId = new set<String>();
    Map <String, App__c> mapApp = new Map <String, App__c> ();
    
    for(Launch_Checklist__c lc: Trigger.new) {
        if (lc.Apple_ID__c != Null) {
            appId.add(lc.Apple_ID__c);
        } else if (lc.Package_Name__c != Null) {
            appId.add(lc.Package_Name__c);
        }
    }

    List<App__c> apps = new List<App__c>([select App_ID__c, Account__c from App__c where App_ID__c IN:appId ]);
    
    if (apps.size() > 0) {
        for (App__c app: apps)  {
        	mapApp.put(app.App_ID__c, app);
    	}
        for(Launch_Checklist__c lc: Trigger.new) {
	    
           if(lc.Apple_ID__c != Null){
	        lc.Account__c = mapApp.get(lc.Apple_ID__c).Account__c;
                lc.App__c = mapApp.get(lc.Apple_ID__c).Id;
	    }else if(lc.Package_Name__c != null){
	    	
		lc.Account__c = mapApp.get(lc.Package_Name__c).Account__c;
                lc.App__c = mapApp.get(lc.Package_Name__c).Id;
	    }
	    
    	}
    }
}
try this and see..
This was selected as the best answer
apsullivanapsullivan
That's pretty close.  Thanks so much for taking the time to reply and help.  Really - huge help!!

One small change on the second Launch Checklist loop ensures that I don't get a null reference:

for(Launch_Checklist__c lc: Trigger.new) {
    if(lc.Apple_ID__c != Null && mapApp.get(lc.Apple_ID__c) != Null) {
        lc.Account__c = mapApp.get(lc.Apple_ID__c).Account__c;
        lc.App__c = mapApp.get(lc.Apple_ID__c).Id;
    } else if(lc.Package_Name__c != null && mapApp.get(lc.Package_Name__c) != Null) {
        lc.Account__c = mapApp.get(lc.Package_Name__c).Account__c;
        lc.App__c = mapApp.get(lc.Package_Name__c).Id;
    }