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
Rung41Rung41 

Fails in to deploy to Production (List has more than 1 row for assignment to SObject) - Help Please

I have a trigger that captures the day and time a}n event was completed and then add that day/time to field on the Account Screen. I ran my tests in the Sandbox and got no errors and a 100% coverage. When I go to deploy it to production I get the following:

 

Failure Message: "System.QueryException: List has more than 1 row for assignment to SObject", Failure Stack Trace: "Class.OSC_Event_Test.myUnitTest: line 6, column 20 External entry point"

Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

 

Here is my Trigger and Test Class

 

 

Trigger

trigger OSC_Activty_Event on Event (after update) {

for (Event updatedEvent : trigger.new) {
    for(Event olduEvent : trigger.old){
        if (updatedEvent.Sales_Call_Completed__c != olduEvent.Sales_Call_Completed__c){
            for(Account a : [SELECT id, Last_Sales_Call__c FROM Account WHERE Account.id = :updatedEvent.AccountId]){

 a.Last_Sales_Call__c = updatedEvent.Completed_Date_Time__c;
  update a;

  }}}}

 Test Class

@isTestprivate class OSC_Event_Test {
    static testMethod void myUnitTest() {       

 

Event ev = [Select id,Sales_Call_Completed__c from Event where Sales_Call_Completed__c = 'No'];       

ev.Sales_Call_Completed__c = 'Yes'; ev.All_Communities_Objective__c = 'Sell a Paid Model';       

try{       

update ev;         

//System.assertEquals('This is a test');        }       

catch(System.DmlException e){           

 System.debug('we caught a dml exception: ' + e.getDmlMessage(0));        

}    }}

Best Answer chosen by Admin (Salesforce Developers) 
tom_patrostom_patros

This line in your test:

 

Event ev = [Select id,Sales_Call_Completed__c from Event where Sales_Call_Completed__c = 'No'];

 could be returning more than one event, so you could be essentially trying to set "ev" to a List of Events. Try to add "limit 1" to your query, like:

 

Event ev = [Select id,Sales_Call_Completed__c from Event where Sales_Call_Completed__c = 'No' limit 1];

 

 

All Answers

tom_patrostom_patros

This line in your test:

 

Event ev = [Select id,Sales_Call_Completed__c from Event where Sales_Call_Completed__c = 'No'];

 could be returning more than one event, so you could be essentially trying to set "ev" to a List of Events. Try to add "limit 1" to your query, like:

 

Event ev = [Select id,Sales_Call_Completed__c from Event where Sales_Call_Completed__c = 'No' limit 1];

 

 

This was selected as the best answer
Rung41Rung41

Brilliant!!! Thank you so much!