function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
FabianGFabianG 

Bulkify cross object field update trigger on multiple fields

Dear Salesforce guru's,

I'm currently facing some struggle bulkifiying a cross object update trigger that should update multiple fields.

Background:
I want checkboxes on leads object to be checked, when a date field on a custom object called "Suchprofil" is populated. But there are in total 6 date fields and checkboxes on the same objects. The objects share the same ID (which is actually stored as a string)
The simple trigger on a single field works fine and looks as follows:
trigger updateActivatedFieldLead on Suchprofil__c(before insert, before update) {
    List<Lead> leadList = new List<Lead>();
    List<String> companyId = new List<String>();
    
    for(Suchprofil__c sp : Trigger.new) {
        if(sp.Aktivierungsdatum__c!=null) {
        	companyId.add(sp.UnternehmensID__c);    
        }
    }
    
leadList = [SELECT Id, Activated__c FROM Lead WHERE UnternehmensID__c IN : companyId];
    for(Lead l : leadList) {
        l.Activated__c = true; 
    }
    update leadList;
}

Unfortunately I'm not really familiar wih mapping yet. I tried to build a bulkified trigger on the basis of David Liu's example http://www.sfdc99.com/2014/01/25/use-maps-navigate-across-lists/

Here's what I made up so far:
 
trigger SPLead on Suchprofil__c (before insert, before update){
	Set<String> allSP = new Set<String>();
	for (Suchprofil__c NewSP : Trigger.new){
		if (newSP.Aktivierungsdatum__c != null ||  //
			Mail_versendet__c != null ||
			Mail_ge_ffnet__c != null ||
			RecruiterPINDashboardAccess__c != null ||
			RecruiterPINDashboardAccessSuccess__c != null ||
			Deaktivierungsdatum__c != null){
				allSP.add(newSP.UnternehmensID);}
				}
	List<Lead> CLeads = [SELECT Id, Activated__c, Mail_versendet__c, Mail_ge_ffnet__c, Mail_geklickt__c, Eingeloggt__c, Deaktiviert__c
						FROM Lead WHERE UnternehmensID IN : NewSP];
	Map<String, Lead> SPtoLead = new Map<String, Lead>();
		for (Lead L : CLeads){
		SPtoLead.put(L.UnternehmensID, L);
		}
	for (Suchprofil__c NewSP : Trigger.new){
		if (newSP.Aktivierungsdatum__c != null){
		Lead a = SPtoLead.get(newSP.UnternehmensID);
		a.Activated__c = true
		}
		}
		//Rest of the mapping
	}



Can anyone help me to correctly map the date fields on the custom object to activate the corresponding checkboxes on the lead object?

I really appreciate your help!
Thanks

Fabian
FabianGFabianG
Unfortunately, I can't edit my post. There were some slight mistakes, I could identify so far:
trigger SPLeadUpd on Suchprofil__c (before insert, before update){
    Set<String> allSP = new Set<String>();
    for (Suchprofil__c NewSP : Trigger.new){
        if (NewSP.Aktivierungsdatum__c != null ||  //
            NewSP.Mail_versendet__c != null ||
            NewSP.Mail_ge_ffnet__c != null ||
            NewSP.RecruiterPINDashboardAccess__c != null ||
            NewSP.RecruiterPINDashboardAccessSuccess__c != null ||
            NewSP.Deaktivierungsdatum__c != null){
                allSP.add(NewSP.UnternehmensID__c);}
                }
    List<Lead> CLeads = [SELECT Id, Activated__c, Mail_versendet__c, Mail_ge_ffnet__c, Mail_geklickt__c, Eingeloggt__c, Deaktiviert__c
                        FROM Lead WHERE UnternehmensID__c IN : allSP];
    Map<String, Lead> SPtoLead = new Map<String, Lead>();
        for (Lead L : CLeads){
        SPtoLead.put(L.UnternehmensID__c, L);
        }
    for (Suchprofil__c NewSP : Trigger.new){
        if (NewSP.Aktivierungsdatum__c != null){
        Lead a = SPtoLead.get(NewSP.UnternehmensID__c);
        a.Activated__c = true;
        }
        }
        //Rest of the mapping
    }

 
FabianGFabianG
Push