0

I wrote a simple trigger to clear the value of a Lookup field when a certain checkbox is checked. It works fine in the sandbox, but when I try to deploy to Prod a completely different test class is erring, saying "List has more than 1 row for assignment to SObject". I looked at the other test class, and the section it's referring to is a Lead creation piece, so I figured something must be wrong with my own code.

Below is the code and test class, any help would be very much appreciated!

Trigger:

trigger ClearStarSend on Contact (before insert, before update) {

for (Contact c : Trigger.new) {

if (c.Clear_Job_for_Send__c == TRUE && Contact.Job_for_Send__c != null)

c.Job_for_Send__c = null;
c.Clear_Job_for_Send__c = FALSE;
}
}

Test Class:

@isTest 
public class ClearStarSend {
    static testMethod void insertContact() {

       Contact contactToCreate = new Contact();

       contactToCreate.FirstName = 'Reg';
       contactToCreate.LastName = 'M';
       contactToCreate.Email = '[email protected]';
       contactToCreate.Clear_Job_for_Send__c = TRUE;


       insert contactToCreate;
    }
}

The other test class that is erring, it says line 45 is the issue, I've put that piece of code below.

Task aTask = [select Subject from Task where WhoID = :testLead.ID];
        System.AssertEquals(aTask.Subject, 'Email: Subject of Test Email');

The full test class in question:

/*
 * Tests
 */


/*
 * This is the basic test
 */

 public with sharing class testSendEmailFlowPlugin { 


     public static final String SUBJECT = 'Subject of Test Email';
     public static final String SUBJECT1 = 'Subject of Test Email with Only Email Address';
     public static final String BODY = 'BODY of Test Email';
     public static final String EMAIL_ADDRESS = '[email protected]';
     public static final String TEXT_ATTACHMENT_NAME = 'My Text Attachment';
     public static final String TEXT_ATTACHMENT_BODY = 'My Text Attachment BODY';
     public static final String PDF_ATTACHMENT_NAME = 'My PDF Attachment.pdf';
     public static final String PDF_ATTACHMENT_BODY = 'My PDF Attachment BODY';
     public static final String INVALIDID = '000000000000000';    

    static testMethod void basicTest() {

        // Create dummy lead
        Lead testLead = new Lead(Company='Test Lead',FirstName='John',LastName='Doe', Email='[email protected]');
        insert testLead;


        // Test Sending Email against a record
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',testLead.ID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body',BODY);

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);

        System.assertEquals(result.outputparameters.get('Status'),'SUCCESS');

        Task aTask = [select Subject from Task where WhoID = :testLead.ID];
        System.AssertEquals(aTask.Subject, 'Email: Subject of Test Email');

        Lead aLead = [select name, (SELECT Subject, ActivityDate, Description from ActivityHistories) FROM Lead where id=:testLead.ID];
  //      System.assert(aLead.ActivityHistories.size()==1);
  //      System.assertEquals(aLead.ActivityHistories[0].subject, 'Email: '+SUBJECT);

    }


    static testMethod void basicTestwithTextAttachment() {

        // Create dummy lead
        Lead testLead = new Lead(Company='Test Lead',FirstName='John',LastName='Doe', Email='[email protected]');
        insert testLead;


        // Test Sending Email against a record
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',testLead.ID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body',BODY);
        inputParams.put('textAttachmentName',TEXT_ATTACHMENT_NAME);
        inputParams.put('textAttachmentContent',TEXT_ATTACHMENT_BODY);

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);

        System.assertEquals(result.outputparameters.get('Status'),'SUCCESS');
        Lead aLead = [select name, (SELECT Subject, ActivityDate, Description from ActivityHistories) FROM Lead where id=:testLead.ID];
//        System.assert(aLead.ActivityHistories.size()==1);
//        System.assertEquals(aLead.ActivityHistories[0].subject, 'Email: '+SUBJECT);        

        Attachment anAttach = [select id, name from Attachment where parentID = :testLead.ID];
        System.AssertEquals(anAttach.name, TEXT_ATTACHMENT_NAME);

    }


    static testMethod void basicTestwithpdfAttachment() {

        // Create dummy lead
        Lead testLead = new Lead(Company='Test Lead',FirstName='John',LastName='Doe', Email='[email protected]');
        insert testLead;


        // Test Sending Email against a record
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',testLead.ID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body',BODY);
        inputParams.put('pdfAttachmentName',PDF_ATTACHMENT_NAME);
        inputParams.put('pdfAttachmentContent',PDF_ATTACHMENT_BODY);

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);

        System.assertEquals(result.outputparameters.get('Status'),'SUCCESS');
        Lead aLead = [select name, (SELECT Subject, ActivityDate, Description from ActivityHistories) FROM Lead where id=:testLead.ID];
//        System.assert(aLead.ActivityHistories.size()==1);
//        System.assertEquals(aLead.ActivityHistories[0].subject, 'Email: '+SUBJECT);        

        Attachment anAttach = [select id, name from Attachment where parentID = :testLead.ID];
        System.AssertEquals(anAttach.name, PDF_ATTACHMENT_NAME);

    }

 /*
  * This test is to test the convert Lead with the Account ID specified
  */
       static testMethod void basicTestwithCCEmail() {

        // Create dummy lead
        Lead testLead = new Lead(Company='Test Lead',FirstName='John',LastName='Doe', Email='[email protected]');
        insert testLead;


        // Test Sending Email against a record
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',testLead.ID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body',BODY);
        inputParams.put('emailAddress',EMAIL_ADDRESS);

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);

        System.assertEquals(result.outputparameters.get('Status'),'SUCCESS');

        Lead aLead = [select name, (SELECT Subject, ActivityDate, Description from ActivityHistories) FROM Lead where id=:testLead.ID];
 //       System.assert(aLead.ActivityHistories.size()==1);
 //       System.assertEquals(aLead.ActivityHistories[0].subject, 'Email: '+SUBJECT);
 //       System.assertEquals(aLead.ActivityHistories[0].Description.contains(EMAIL_ADDRESS), True);


    }

    static testMethod void basicTestwithTextAttachmentandCCEmail() {

        // Create dummy lead
        Lead testLead = new Lead(Company='Test Lead',FirstName='John',LastName='Doe', Email='[email protected]');
        insert testLead;

        // Test Sending Email against a record
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',testLead.ID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body',BODY);
        inputParams.put('emailAddress',EMAIL_ADDRESS);
        inputParams.put('textAttachmentName',TEXT_ATTACHMENT_NAME);
        inputParams.put('textAttachmentContent',TEXT_ATTACHMENT_BODY);

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);

        System.assertEquals(result.outputparameters.get('Status'),'SUCCESS');        

        Lead aLead = [select name, (SELECT Subject, ActivityDate, Description from ActivityHistories) FROM Lead where id=:testLead.ID];
 //       System.assert(aLead.ActivityHistories.size()==1);
 //       System.assertEquals(aLead.ActivityHistories[0].subject, 'Email: '+SUBJECT);
 //       System.assertEquals(aLead.ActivityHistories[0].Description.contains(EMAIL_ADDRESS), True);
        Attachment anAttach = [select id, name from Attachment where parentID = :testLead.ID];
        System.AssertEquals(anAttach.name, TEXT_ATTACHMENT_NAME);

    }

static testMethod void attachmentTest() {

        // Create dummy lead
        Lead testLead = new Lead(Company='Test Lead',FirstName='John',LastName='Doe', email='[email protected]');
        insert testLead;

        // Create dummy conversion
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',testLead.ID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body','testing body');
        inputParams.put('textAttachmentName','textattach');
        inputParams.put('textAttachmentContent','testing text content');
        inputParams.put('pdfAttachmentName','pdfattach');
        inputParams.put('pdfAttachmentContent','testing pdf content');



        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);

        System.assertEquals(result.outputparameters.get('Status'),'SUCCESS');

        Lead aLead = [select name, (SELECT Subject from ActivityHistories), (select name from Attachments) FROM Lead where id=:testLead.ID];
     //   System.assert(aLead.ActivityHistories.size()==1);
     //   System.assertEquals(aLead.ActivityHistories[0].subject, 'Email: '+SUBJECT);

        System.assert(aLead.Attachments.size()==2);
        String attach1Name = aLead.Attachments[0].name;
        String attach2Name = aLead.Attachments[1].name;

        System.assert(attach1Name == 'textattach' || attach2Name == 'textattach');
        System.assert(attach1Name == 'pdfattach.pdf' || attach2Name == 'pdfattach.pdf');




    } 



 /*
  * -ve Test
  */  
  static testMethod void negativeTest() {

        // Create dummy lead


        // Test Sending Email against a record
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',INVALIDID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body',BODY);

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);

        System.assertEquals(result.outputparameters.get('Status'),'ERROR');

    }  


 /*
  * This test is to test the describe() method
  */ 
        static testMethod void describeTest() {

                SendEmail aSendEmailPlugin = new SendEmail();
                Process.PluginDescribeResult result = aSendEmailPlugin.describe();

                System.AssertEquals(result.inputParameters.size(), 8);
                System.AssertEquals(result.OutputParameters.size(), 2);

        }


}
4
  • In order to help, we need to see the other class that is actually throwing the error and which line its throwing the error on. The line will be a SOQL statement. May 28, 2014 at 17:28
  • try to limit your SOQL query to 1 so that it returns one record. I think your soql returns more than one row. But, you should check if your logic needs more than a record.
    – Bforce
    May 28, 2014 at 17:30
  • Thanks guys, I added the test class that is throwing the error, along with the specific line it says is erring.
    – McD
    May 28, 2014 at 17:32
  • You should mark the answer below as correct. Jun 11, 2014 at 22:24

1 Answer 1

5

You're SOQL query is returning more than one record. This line:

Task aTask = [select Subject from Task where WhoID = :testLead.ID];

should be

List<Task> aTasks = [select Subject from Task where WhoID = :testLead.ID];

If your SOQL query is being set to just a single record, it must always return only a single record (not 0 or 2+) or a runtime exception occurs. Documentation to explain it more:

SOQL queries can be used to assign a single sObject value when the result list contains only one element. When the L-value of an expression is a single sObject type, Apex automatically assigns the single sObject record in the query result list to the L-value. A runtime exception results if zero sObjects or more than one sObject is found in the list. For example:

List<Account> accts = [SELECT Id FROM Account];

// These lines of code are only valid if one row is returned from
// the query. Notice that the second line dereferences the field from the
// query without assigning it to an intermediary sObject variable.
Account acct = [SELECT Id FROM Account];
String name = [SELECT Name FROM Account].Name;
2
  • Thanks Jesse, I'm getting this error when trying to change the code: "Initial term of field expression must be a concrete SObject"
    – McD
    May 28, 2014 at 17:41
  • 1
    You may have to change other aspects of your code to handle a list. I didn't go through all of your code to make sure it would work with a list. Does that make sense? May 28, 2014 at 18:17

You must log in to answer this question.

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