My scenario is Object A has a field that stores ID of Object B. If Object B is deleted, I want another field marked on Object A. I'm not sure what I'm missing, but my SOQL Query isn't finding anything when I run the actual code.

Here is my trigger:

trigger SF_OppSplit on OpportunitySplit (before delete) {

And here is the class:

public class OppSplitRevenueMatchCheck {
    public static void checkForDelete(List <OpportunitySplit> oppSplits){
        List <Revenue_Detail__c> revUpdateList = new List <Revenue_Detail__c>();
        for (OpportunitySplit oppSplit:oppSplits){
            Revenue_Detail__c rev = [SELECT Id, Opportunity_Split_Id__c
                                       FROM Revenue_Detail__c
                                      WHERE Opportunity_Split_Id__c = :oppSplit.Id];

            rev.Opt_Out__c = TRUE;
        update revUpdateList;

I know I must be missing something simple, I just can't find what.

*Also, if someone can help me re-write this with the query outside of the for loop and break it down a bit (or point to a place that does), I'd be eternally grateful. Been having a hard time wrapping my head around maps - which I assume is what I need to use here instead.



A bulkified version might be:

trigger SF_OppSplit on OpportunitySplit (before delete) {

    Set<Id> osIds = Trigger.oldMap.keySet();
    Revenue_Detail__c[] rds = [
            select Id
            from Revenue_Detail__c
            where Opportunity_Split_Id__c in :osIds
            and Opt_Out__c = false
    for (Revenue_Detail__c rd : rds) rd.Opt_Out__c = true;
    update rds;

But this probably won't fix your problem as it is using the same fields - have you double checked the data you are testing on to make sure Opportunity_Split_Id__c contains the Ids you think it does?

  • Thanks Keith! your question actually led me to the answer. The field had the 15 char version of the ID instead of the 18 char, once I made the field 18 char, it was able to find the record. Also thanks for the bulkification! – noviceCoder Jul 9 '17 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.