+ Start a Discussion

writing bulk trigger based on maps

I'm new, so I know I have the syntax wrong.  Any help with creating a bulk trigger would be greatly appreciated.


For each record that is inserted or updated, the trigger should

  • >review the field "Dept_Seg_1__c",
  • >lookup the mapped value "record_type__c" --- there is a map of Dept_Seg_1__c to record_type__c, and
  • >update the field "record_type__c" on all of the new/inserted records







trigger UpdateLabor_Before on Labor__c (before insert, before update) {

//Create nested map of Labor ID TO Map of Record Type IDs TO Record Type Names
Map<ID,Map<String,String>> labortypeMap = new Map<ID,Map<String,String>> ();
Map<Dept_Seg_1__c,Record_Type__c> labortypeNestMap = new Map<String,String>
{'201' => 'mh center', '310' => 'staffing', '410' => 'sales',
'510' => 'marketing', '610' => 'training', '710' => 'it',
'810' => 'accounting & finance', '820' => 'hr',
'110' => 'call center', '411' => 'recruiting', '150' => 'coaching'};

for(Labor__c laborloop : trigger.new) {

// Set records to values based on map
Labor__c labor = new Labor__c (Record_Type__c = labortypemap.get.(Record_Type__c));


upsert labor;




Message Edited by bpol on 02-22-2010 04:28 PM



You might want to check out the following blog post for help:


Writing Bulk Triggers for Salesforce.com


Jeff Douglas
Appirio, Inc.


The post that helped a bit more was http://sfdc.arrowpointe.com/2008/09/13/bulkifying-a-trigger-an-example/

But both are good.


After much work, I was able to write a bulk trigger that works when I edit or update a new record.  The data updates are correct.


Interestingly -- it "works" when I bulk upload records; that is, it doesn't break.  However, the data is incorrect!!  When I edit any of these new records and re-save, it corrects itself.


Any thoughts?