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 

Integration with C# SFDC Error 'List has more than 1 row for assignment to SObject'

All -

 

I have coded the C# side of things and all compiles and runs well. I have no issues inserting 1 record at a time. I am now trying to insert multiple records and am running into an issue. I generated a WSDL off if my class. The following code is proving to be a strruggle for me. I am sure I am structuring this incorrectly somehow, but am at a loss as to how. Can I get some assisteance?

global class TrialSession {
		WebService String strPatientData;
		WebService String strOrderID;
		WebService Date dtmTestDate;
		WebService String strTypeofTest;
		WebService String strReviewerName;
		WebService String strReviewerComments;
		WebService Date dtmDateofBirth;
		WebService Decimal numHeight;
		WebService Decimal numWeight;
		WebService String strGender;
		WebService String strEthnicity;
		WebService String strAsthma;
		WebService String strCOPD;
		WebService String strSmoker;
		WebService Date dtmDateReviewed;
		WebService Decimal numLungAge;
		WebService String strQualityGrade;
		WebService String strQualityGradeOriginal;
		WebService String strSoftwareVersion;
	}
	
	webservice static Trial_Session__c LoadTrialSession(TrialSession TSInfo)
	{
		//Look for existing Order_ID__c being passed in
		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;
         }
         else
         {
         	//Get List of Trial Sessions
         	
         	Trial_Session__c insertTrialSession = new Trial_Session__c();
         	
         	insertTrialSession.Patient_Data__c = TSInfo.strPatientData; 
         	insertTrialSession.External_ID__c = TSInfo.strOrderID;
         	insertTrialSession.Test_Date__c = TSInfo.dtmTestDate;
         	insertTrialSession.Type_of_Test__c = TSInfo.strTypeofTest;
         	insertTrialSession.Reviewer_Name__c = TSInfo.strReviewerName;
         	insertTrialSession.Reviewer_Comments__c = TSInfo.strReviewerComments;
         	insertTrialSession.Date_of_Birth__c = TSInfo.dtmDateofBirth;
         	insertTrialSession.Height__c = TSInfo.numHeight;
         	insertTrialSession.Weight__c = TSInfo.numWeight;
         	insertTrialSession.Gender__c = TSInfo.strGender;
         	insertTrialSession.Ethnicity__c = TSInfo.strEthnicity;
         	insertTrialSession.Asthma__c = TSInfo.strAsthma;
         	insertTrialSession.COPD__c = TSInfo.strCOPD;
         	insertTrialSession.Smoker__c = TSInfo.strSmoker;
         	insertTrialSession.Date_Reviewed__c = TSInfo.dtmDateReviewed;
         	insertTrialSession.Lung_Age__c = TSInfo.numLungAge;
         	insertTrialSession.Quality_Grade__c = TSInfo.strQualityGrade;
         	insertTrialSession.Quality_Grade_Original__c = TSInfo.strQualityGradeOriginal;
         	insertTrialSession.Software_Version__c = TSInfo.strSoftwareVersion;
         	
         	insert insertTrialSession;
        
            return insertTrialSession;   
         }
     }

 The class has multiple objects like this and I need to account for multiple children.

 

The error I am getting is: List has more than 1 row for assignment to SObject

 

Please help. I would need an example if that would work. The records being inserted will have parents and children as well.

 

WolfyWolfy

It's just an error in your apex -

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];

If the select returns more than one row, then you're basically trying to assign List<sObject> to sObject. So change the type of the variable you are assigning to from Trial_Session__c to List<Trial_Session__C>. Then iterate through it to assign the values and update the collection afterwards.

Also you are making the same SOQL select twice for no apparant reason - do it only once.