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
faceroy123faceroy123 

List has more than 1 row for assignment to SObject

Hi,

 

I have a trigger that send an email when a checkbox is set to true on a task. The check box is set to true via a time based workflow.

However where 2 or 3 tasks are updated at the same time the email trigger is failing with this error message:

 

System.Que​ryExceptio​n: List has more than 1 row for assignment to SObject Trigger.Wo​rkflowEmai​l: line 5, column 1

 

Any ideas how I modify to handle more than 1 record at a time?

 

Thanks

 

 

 

Niket SFNiket SF

Please update update the Query and result

 

List<Task> lsttas = [select id, t.Owner.Email, t.initial_assigned_to__c, t.title__c, t.description, t.subject, t.consignment_number__c, t.Owner.Name, t.whatid, t.number_of_updates__c  from Task t where id = :trigger.new];



 try{
      
    for(Task objTask : lsttas)
{
        messaging.Singleemailmessage mail=new
    messaging.Singleemailmessage();
    mail.setSaveAsActivity(false);
    
        mail.setSenderDisplayName('System Administrator');
        mail.setReplyTo(objTask.Owner.email);
        mail.setsubject('A task has escalated to you');
        mail.setTargetObjectId(trigger.new[0].testid__c);
        mail.setPlainTextBody('A task relating to a case has ecalated to you:' +'\r' + 'Subject:' +tas.subject +'\r' + 'Consignment number: ' + tas.consignment_number__c +'\r'+ 'Description:'+ tas.description + '\r' + 'Escalated from:' + tas.initial_assigned_to__c + ' ' + tas.title__c +'\r' + 'click this link to view the case' + ' https://city-link.my.salesforce.com/'+tas.whatid  +' or review the task directly by clicking the following link' + ' https://city-link.my.salesforce.com/'+tas.Id);   
       
          try{
        messaging.sendEmail(new messaging.Singleemailmessage[]{mail}) ;
         trigger.new[0].number_of_updates__c = trigger.new[0].number_of_updates__c + 1;
            trigger.new[0].Ownerid = trigger.new[0].testid__c;
}

 

 

Please check that you are using the Trigger.new[0], It means you are using only first value... It's not the bulk safe :(

 

 

 

faceroy123faceroy123

Thanks for the response.

 

I'm fairly new to this so not sure how to update my trigger with the above.

do i need to keep trigger.new[0] ?

 

Thanks

steve456steve456
Task tas = [select id, t.Owner.Email, t.initial_assigned_to__c, t.title__c, t.description, t.subject, t.consignment_number__c, t.Owner.Name, t.whatid, t.number_of_updates__c  from Task t where id = :trigger.new LIMIT 1];
 
 
Give this way it should wrk
Saravanan @CreationSaravanan @Creation

Hi,

 

List<Task> lsttas = [select id, t.Owner.Email, t.initial_assigned_to__c, t.title__c, t.description, t.subject, t.consignment_number__c, t.Owner.Name, t.whatid, t.number_of_updates__c  from Task t where id = :trigger.new];

 

you are using the query right,In bulk operation the trigger.new is more than one then the query will return more than one value

so use like below 

 

List<Task> lsttas =new List<Task>();

 lsttas = [select id, t.Owner.Email, t.initial_assigned_to__c, t.title__c, t.description, t.subject, t.consignment_number__c, t.Owner.Name, t.whatid, t.number_of_updates__c  from Task t where id = :trigger.new];

 

now it will not throw the error List has more than one 1 row.And it will suitable for bulk operation.

 

there is one more option like below 

 

List<Task> lsttas = [select id, t.Owner.Email, t.initial_assigned_to__c, t.title__c, t.description, t.subject, t.consignment_number__c, t.Owner.Name, t.whatid, t.number_of_updates__c  from Task t where id = :trigger.new  limit 1];

 

but this query will always return 1 value even in bulk operation.