0

Lead, CampaignMember(Passing leadid) are created from flow#1

In Flow#2(record triggered, after save on campaignmember) apex class is run which uses dmloptions to update Lead.

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
...
update LeadList;

debug statement in the apexclass:

System.debug('@@@@ '+[SELECT id,campaignid,campaign.status,leadid from campaignmember]);

Debug Log:

@@@@ (CampaignMember:{Id=00v0K00000dsBqWQAU, CampaignId=7010K000001OPp0QAG, LeadId=00Q0K000019XDYvUAO}

Assignment Rule on lead: Campaign: Status EQUALS Planned

Even though Campaign Status equals planned, we don't see "Campaign.Status" in the debug log, which means it's empty, which is not satisfying assignment rule.

I'm wondering why Campaign Status is empty when accessing from CampaignMember in above SOQL query?

I've replicated this scenario in my dev org outcome is same.

Apex Class:

public class InvokeFromFlow {

    @InvocableMethod(label='Run Assignment Rules, Auto-Response Rule')
    public static void triggerDmlOptions(List<Id> leadIds) {
        System.debug('@@@invokable');
        Database.DMLOptions dmlopt = new Database.DMLOptions();
        dmlopt.assignmentRuleHeader.useDefaultRule= true;
        //dmlopt.assignmentRuleHeader = true;
        dmlopt.emailHeader.triggerAutoResponseEmail = true;
        System.debug('@@@@ '+[SELECT id,campaignid,campaign.status,leadid from campaignmember]);
        System.debug('@@@@ '+[SELECT id,status from campaign where id='7010K000001OPp0QAG']);
        List<Lead> leadslist = new List<Lead>();
        for(Lead led:[SELECT Id from Lead where Id IN:leadIds]){
            led.setOptions(dmlopt);
            leadslist.add(led);
        }
        update leadslist;
    }

}

enter image description here enter image description here

1 Answer 1

1

1) Campaign Status is empty when accessing from CampaignMember in above SOQL query

This is a standard behaviour of the System.debug() method. You traversed one relationship above campaign.status. The traversed relationship value won't be shown in the debug log. You can try running the following code via Anonymous Apex:

System.debug([SELECT id, Account.Name FROM Contact]);

The result will be the same:

15:20:47:025 USER_DEBUG [13]|DEBUG|(Contact:{Id=0030600002AUuWMAA1, AccountId=0010600002ANycJAAT}...

If you want to debug the value of the Campaign Status field, you'd have to do it this way:

@InvocableMethod(label='Debug Campaign Status' description='Debugs the Campaign Status field. Receives the IDs of the Campaign Member records.' category='Lead')
public static void debugLead(List<Id> memIds){
    for(id memId : memIds){
        CampaignMember cm = [SELECT id,campaignid,campaign.status,leadid from campaignmember WHERE ID=:memId];
        System.debug('You can debug the campaign status this way: ' + cm.campaign.status);
        System.debug('Or via a SOQL query, but without traversing: ' + [SELECT id,status from campaign WHERE ID=:cm.campaignid]);            
    }
}

Yields the following ouput:

15:29:05:093 USER_DEBUG [6]|DEBUG|You can debug the campaign status this way: Completed
15:29:05:109 USER_DEBUG [7]|DEBUG|Or via a SOQL query, but without traversing: (Campaign:{Id=70106000001JxV7AAK, Status=Completed})

Remember that the code works on a limited amount of records as the governor limits would be hit for more than 100 records.

2) Assignment Rule won't run

From what I've seen in the examples that you provided, there might be different reasons why you are not receiving the expected result.

A) Mistake in your code. You might need to execute the setOptions() method to execute your assignment rule. See the example below:

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;

Case l = new Case();
l.id = '5000600005uOPb6AAG';
l.Subject = 'test apex';
l.setOptions(dmo);
update l;

B) You might have different rule entries configured for the assignment rule. Each entry has an order index. You might want to check if there is a rule entry with lower order index that is being triggered that perhaps assigns your record to a different user than you expect.

4
  • Thank you for the solution reg. debug log. I have used A, B. They're working fine, assignment rule isn't running when I include campaign status.. You can try this out in your dev. org. trigger above invocable method in campaignmember flow (including dmloptions in the class)and observe if the assignment rule runs when campaign status is included in the trigger criteria
    – user61140
    Jun 20, 2023 at 14:33
  • @user61140 You need to provide the whole code snippet otherwise I can not help you. Possibly also to say whether the flow #1 runs as before and the flow #2 runs as after. Etc.
    – DevelBase2
    Jun 20, 2023 at 15:10
  • 1
    Simpler debugging trick to see relationships: System.debug(JSON.serializePretty([SELECT Owner.Profile.Name FROM Account LIMIT 1]));
    – eyescream
    Jun 24, 2023 at 20:28
  • @eyescream That's a cool trick! Thanks.
    – DevelBase2
    Feb 8 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .