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
TechEd_ProgrammerTechEd_Programmer 

Help with 'List has more than 1 row for assignment to SObject'

Ok, spoke too soon. I am getting this error and need some assistance in restructuring my code.

 

Below is the code that I have:

List<Trial_Session__c> objTrialSession = new List<Trial_Session__c>();
         
         objTrialSession = [SELECT External_ID__c FROM Trial_Session__c WHERE External_ID__c = :TSInfo.strOrderID];
         
         if(objTrialSession.size() > 0)
         {
            // Existing Session Check
            
            Trial_Session__c updateTrialSession = [SELECT Patient_Data__c, External_ID__c, Test_Date__c, Type_of_Test__c, Reviewer_Name__c, Reviewer_Comments__c, Date_of_Birth__c, Height__c, Weight__c, Gender__c, Ethnicity__c, Asthma__c, COPD__c, Smoker__c, Date_Reviewed__c, Lung_Age__c, Quality_Grade__c, Quality_Grade_Original__c, Software_Version__c FROM Trial_Session__c
            WHERE External_ID__c = :TSInfo.strOrderID];
            
            updateTrialSession.External_ID__c = TSInfo.strOrderID;
            updateTrialSession.Test_Date__c = TSInfo.dtmTestDate;
            updateTrialSession.Type_of_Test__c = TSInfo.strTypeofTest;
            updateTrialSession.Reviewer_Name__c = TSInfo.strReviewerName;
            updateTrialSession.Reviewer_Comments__c = TSInfo.strReviewerComments;
            updateTrialSession.Date_of_Birth__c = TSInfo.dtmDateofBirth;
            updateTrialSession.Height__c = TSInfo.numHeight;
            updateTrialSession.Weight__c = TSInfo.numWeight;
            updateTrialSession.Gender__c = TSInfo.strGender;
            updateTrialSession.Ethnicity__c = TSInfo.strEthnicity;
            updateTrialSession.Asthma__c = TSInfo.strAsthma;
            updateTrialSession.COPD__c = TSInfo.strCOPD;
            updateTrialSession.Smoker__c = TSInfo.strSmoker;
            updateTrialSession.Date_Reviewed__c = TSInfo.dtmDateReviewed;
            updateTrialSession.Lung_Age__c = TSInfo.numLungAge;
            updateTrialSession.Quality_Grade__c = TSInfo.strQualityGrade;
            updateTrialSession.Quality_Grade_Original__c = TSInfo.strQualityGradeOriginal;
            updateTrialSession.Software_Version__c = TSInfo.strSoftwareVersion;

            update updateTrialSession;

            return updateTrialSession;
         }

 It is erroring out on the Update updateTrialSession line.

 

The error is:

'List has more than 1 row for assignment to SObject'

 

Thank you for any assistacne you can prodive. FYI the external ID is set to unique

 

 

kerwintangkerwintang

I am not sure of the reason as of now.. but to debug. Can you try to remove the update fields (e.g.  updateTrialSession.Test_Date__c = TSInfo.dtmTestDate;) and try to perform update; then add fields one by one to see where the update fails.

AdrianCCAdrianCC

Hello TechEd!

 

Try adding a 'LIMIT 1' to your soqls, so that they always return a max of 1 record.

 

Regards,

Adrian 

ForceMantis (Amit Jain)ForceMantis (Amit Jain)

Hello TechEd

 

'List has more than 1 row for assignment to SObject' occurs when your query returns more than 1 record and you try to assign it to a object rather than collection of objects(e.g List).

 

At first glace you code looks, a possible error is in following query if it return more than 1 row:

 

[SELECT Patient_Data__c, External_ID__c, Test_Date__c, Type_of_Test__c, Reviewer_Name__c, Reviewer_Comments__c, Date_of_Birth__c, Height__c, Weight__c, Gender__c, Ethnicity__c, Asthma__c, COPD__c, Smoker__c, Date_Reviewed__c, Lung_Age__c, Quality_Grade__c, Quality_Grade_Original__c, Software_Version__c FROM Trial_Session__c
            WHERE External_ID__c = :TSInfo.strOrderID]

Try to change your query to following, and comment rest of the code related to field assignment and update statement.

 

List<Trial_Session__c> updateTrialSession = [SELECT Patient_Data__c, External_ID__c, Test_Date__c, Type_of_Test__c, Reviewer_Name__c, Reviewer_Comments__c, Date_of_Birth__c, Height__c, Weight__c, Gender__c, Ethnicity__c, Asthma__c, COPD__c, Smoker__c, Date_Reviewed__c, Lung_Age__c, Quality_Grade__c, Quality_Grade_Original__c, Software_Version__c FROM Trial_Session__c
            WHERE External_ID__c = :TSInfo.strOrderID];
System.Debug('***' + updateTrialSession.size());

 

Check you debug logs if you see size of list is 2 you need to either assign your query result either to list or have Limit clause if that work with business logic you are implementing.

TechEd_ProgrammerTechEd_Programmer

Amit -

 

I will always have multiple records. How will this code account for that, or how will I structure it if I use Limit 1. I am quite new to integration.