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
nil_von_9wonil_von_9wo 

Mapping boolean is either getting or putting the opposite value

I have created a trigger which will reassigns a custom checkbox field (Ready_to_Purchase_Piqqo_Labels__c) on the standard object Opportunity's value to a similar custom checkbox on the standard Account object.

 

I have the code working now, but I don't understand why I needed to make the line below in red as I did, and I'm concerned that there is something else I don't understand in this code which can cause collateral damage.

 

Can anyone explain this to me:

 

 

trigger syncOpportunityReady2Purchase on Opportunity (before insert, before update) { // Get list of all modified Opportunities. List<Opportunity> triggerList = trigger.new; // Initialize sets and maps. Set<Id> accountIds = new Set<Id>(); Set<Id> opportunityIds = new Set<Id>(); Map<Id, boolean> readyToPurchaseMap = new Map<Id, boolean>(); // Collect modified Opportunity IDs into set. for (Opportunity opportunityModified: triggerList) { if(opportunityModified == null) return; opportunityIds.add(opportunityModified.id); } // Collect modified Opportunities into list. List<Opportunity> opportunityList = new List<Opportunity>([ Select AccountId, Ready_to_Purchase_Piqqo_Labels__c from Opportunity where id In : opportunityIds]); // Collect related Account Ids and map "readiness" to Ids. for (Opportunity opportunityModified: opportunityList) { accountIds.add(opportunityModified.AccountId); readyToPurchaseMap.put (opportunityModified.AccountId, opportunityModified.Ready_to_Purchase_Piqqo_Labels__c); } // Collect related Accounts for (Account a: [ select Ready_to_Purchase_Piqqo_Labels__c from Account where id In : accountIds ] ) { if(readyToPurchaseMap.get(a.Id) == null)return; // Assign Opportunity's readiness to Account // ! This code is "weird". I've had to negate the value to get the correct value to appear. a.Ready_to_Purchase_Piqqo_Labels__c = !(readyToPurchaseMap.get(a.Id)); update a; // Sync with Account's other Opportunities

// This function also needs to be debugged, but it is a separate issue. triggerAccountToolkit.syncReady2Purchase(a, a.Ready_to_Purchase_Piqqo_Labels__c); }}

 

Best Answer chosen by Admin (Salesforce Developers) 
nil_von_9wonil_von_9wo

While I never managed to understand what was happening with my previous code, I did manage to fix it so it makes sense and works:

 

 

trigger syncOpportunityReady2Purchase on Opportunity (after insert, after update) { // Get list of all modified Opportunities. List<Opportunity> triggerList = trigger.new; // Initialize sets and maps. Set<Id> accountIds = new Set<Id>(); Set<Id> opportunityIds = new Set<Id>(); Map<Id, boolean> readyToPurchaseMap = new Map<Id, boolean>(); // Collect modified Opportunity IDs into set. for (Opportunity opportunityModified: triggerList) { if(opportunityModified == null) return; opportunityIds.add(opportunityModified.id); } // Collect modified Opportunities into list. List<Opportunity> opportunityList = new List<Opportunity>([ Select AccountId, Ready_to_Purchase_Piqqo_Labels__c from Opportunity where id In : opportunityIds]); // Collect related Account Ids and map "readiness" to Ids. for (Opportunity opportunityModified: opportunityList) { accountIds.add(opportunityModified.AccountId); readyToPurchaseMap.put (opportunityModified.AccountId, opportunityModified.Ready_to_Purchase_Piqqo_Labels__c); } // Collect related Accounts for (Account a: [ select Ready_to_Purchase_Piqqo_Labels__c from Account where id In : accountIds ] ) { if(readyToPurchaseMap.get(a.Id) == null)return; // Assign Opportunity's readiness to Account if (a.Ready_to_Purchase_Piqqo_Labels__c != (readyToPurchaseMap.get(a.Id))) { a.Ready_to_Purchase_Piqqo_Labels__c = (readyToPurchaseMap.get(a.Id)); update a; } }}