+ Start a Discussion
RiyajRiyaj 

Update the field error??

Hi
When executing  the follwing aspx code it display the error....

 

Apex Code 

global class UpdateAttendanceList{
WebService static void UpdateAttendanceFromClassname(string classname,string studentname,string attendance){
Attendance__c conts=[Select student_Name__c,attendance_Roll__c from Attendance__c where class_Name__r.Name= :classname AND student_Name__r.Name= :studentname];
conts.attendance_Roll__c= attendance;
update conts;
}
}

 

Aspx Code

 

updateAttendanceObj.SessionHeaderValue = new UpdateAttendanceList.SessionHeader();

updateAttendanceObj.SessionHeaderValue.sessionId = sessionId;
updateAttendanceObj.UpdateAttendanceFromClassname(sClassName, sStudentname, attendanceText);

System.QueryException: List has no rows for assignment to SObject

SFDC_EvolveSFDC_Evolve

Hi Riyaj,

 

This error comes where Ur Query returns Zero value .. . i,e 

 

Attendance__c conts=[Select student_Name__c,attendance_Roll__c from Attendance__c where class_Name__r.Name= :classname AND student_Name__r.Name= :studentname]

 

there is no record in the Attendance Object . which satisfy this condition . ..  

 

class_Name__r.Name= :classname AND student_Name__r.Name= :studentname

 

 

Thanks

SFDC_Evolve


vishal@forcevishal@force

Hi,

 

In such cases where you are directly assigning an object through a query, if there is a possibility that the query may return ZERO records, you should instead use a list, query records in the list and if there are any records found, assign the object in the list to the variable. Something like this:

 

Apex Code 

global class UpdateAttendanceList{
WebService static void UpdateAttendanceFromClassname(string classname,string studentname,string attendance){

List<Attendance__c> lstConts = new List<Attendance__c>();

lstConts=[Select student_Name__c,attendance_Roll__c from Attendance__c where class_Name__r.Name= :classname AND student_Name__r.Name= :studentname LIMIT 1];

if(lstConts.size() > 0)

{

Attendance__c conts = new Attendance__c();

conts = lstConts[0];

conts.attendance_Roll__c= attendance;
update conts;

}
}

RiyajRiyaj

HI..

 

Thanks Error is clea..but data is not updated.... Why??

 

vishal@forcevishal@force

can you debug the list "lstConts". I guess there were no records found, and hence it did not update anything.

RiyajRiyaj

when I am debug it got a record.... 

linghongyong12@163.comlinghongyong12@163.com

You can change Like as:

List<Attendance__c> Alist=[Select student_Name__c,attendance_Roll__c from Attendance__c where class_Name__r.Name= :classname AND student_Name__r.Name= :studentname];

if(Alist.size()>0)

{

      Attendance__c conts=Alist.get(0);

     conts.attendance_Roll__c= attendance;
     update conts;

}