1

I have a trigger that will update campaign members to responded for specific campaign after a specific lead status gets updated in the lead, and this value is specified in custom settings. My trigger is working when I'm testing within the UI, but my test is failing with the following error below. Any help would be really appreciated. I wasn't sure if it's a problem with my trigger or my test class. Thanks!

Error:

System.DmlException: Update failed. First exception on row 0 with id 00Q4x00000AL9W4EAL; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, intento.LeadStatus: execution of AfterUpdate

caused by: System.QueryException: List has more than 1 row for assignment to SObject

Trigger.intento.LeadStatus: line 2, column 1: []

Apex trigger:

trigger LeadStatus on Lead (after update){
  intento__intentomatic_settings__c leadval = intento__intentomatic_settings__c.getAll().values();
  Set<ID> LeadIds= new Set<ID>();
    
  if (leadval.intento__Lead_Active__c) {
  for(Lead l : Trigger.new){
    if (l.status == leadval.intento__Demo_Lead_Status_Val__c && Trigger.oldMap.get(l.Id).status != leadval.intento__Demo_Lead_Status_Val__c) {
      LeadIds.add(l.Id);
    }
  }
  
  List<Campaign> c = [
    SELECT Id 
    FROM Campaign 
    WHERE Campaign.Type = 'Intentomatic'
  ];

  List<CampaignMember> cmList = [
    SELECT id, status 
    FROM CampaignMember 
    WHERE LeadId in :LeadIds 
    AND CampaignId in :c
    AND status != 'Responded'
    AND CreatedDate = LAST_N_DAYS:92
    LIMIT 1
  ];

  for(CampaignMember cm : cmList){
    cm.status = 'Responded';
  }
  update cmList;
}}

Test class:

@isTest
private class IntentomaticTests {
  @isTest static void testLeadStatus() {
  
    //settings
    
    List<intento__intentomatic_settings__c> settingList = new List<intento__intentomatic_settings__c>();
    intento__intentomatic_settings__c posSettings = new intento__intentomatic_settings__c();
        posSettings.Name = 'positive';
        posSettings.intento__Lead_Active__c = true;
        posSettings.intento__Demo_Lead_Status_Val__c = 'Demo';
    intento__intentomatic_settings__c negSettings = new intento__intentomatic_settings__c();
        negSettings.Name = 'negative';
        negSettings.intento__Lead_Active__c = false;
    settingList.add(posSettings);
    settingList.add(negSettings);
    insert settingList;
    
    //create lead
    
    Lead ld = new Lead(
      LastName = 'testlastname', 
      Company='testcompany'
    );
    insert ld;
    
    //create positive and negative campaigns
    
    Campaign poscamp = new Campaign(
                    Name = 'Test',
                    Type = 'Demo Test'
                    ); 
    Campaign negcamp = new Campaign(
                    Name = 'Test',
                    Type = 'Web'
                    );            
    insert poscamp;
    insert negcamp;
    
    // create campaign members
    
    CampaignMember member = new CampaignMember(
      LeadId = ld.Id,
      Status = 'sent',
      CampaignId = poscamp.Id
    ); 
    CampaignMember member2 = new CampaignMember(
      LeadId = ld.Id,
      Status = 'sent',
      CampaignId = negcamp.Id
    ); 
    
    insert member; 
    insert member2;
    
    //start test and change lead
    
    Test.startTest();
    ld.status = posSettings.intento__Demo_Lead_Status_Val__c;
    update ld;
    
    CampaignMember campaignMember = [SELECT Id, Status FROM CampaignMember WHERE Id=:member.Id];
    System.assertNotEquals('Sent', campaignMember.status);
    Test.stopTest();
  }
}

1 Answer 1

0

The error message is pointing you to the line where the issue is. LeadStatus trigger, line 2

intento__intentomatic_settings__c leadval = intento__intentomatic_settings__c.getAll().values();

I'll admit that I haven't seen this error pop up when working with custom settings before, but broadly speaking, it makes sense to me.

  • getAll().values() returns a List<intento__intentomatic_settings__c>
  • You're assigning the result of a List into a single variable
  • When the List has more than one item in it (and you're inserting two instances of this custom setting in your test), Salesforce complains

This is a sign that your trigger needs some more development work. You should assume that there will be more than one record in your custom setting, and have some logic to determine which one to use (if any, don't forget about the case where a Lead won't find any applicable custom setting record).

1
  • Thanks very much! Jul 9, 2021 at 13:34

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.