+ Start a Discussion

SOQL Exception


Scenario : When New contact added with custom field Conference (Lookup to conference Obj), expected attendee fiels update by one on conference obj.


Triiger : 

trigger ConferenceNumberUpdateTrigger on Contact (before insert, before update) {

   list <contact> con = trigger.new;



public class ConfNumberClass{

     public static void updateConf(list<contact> con){
      for(contact c : con){
           conference__c c1 = [select Expected_Attendes__c from conference__C where id=:c.conference__r.id limit 1];
           c1.Expected_Attendes__c = c1.Expected_Attendes__c + 1;
           update c1; 


|EXCEPTION_THROWN|[9]|System.QueryException: List has no rows for assignment to SObject
08:57:45.165 (165430000)|METHOD_EXIT|[5]|01pF0000002bU1H|ConfNumberClass.updateConf(LIST<Contact>)
08:57:45.165 (165736000)|FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject

  Appreciate your help.





Ahh, come on this is the problem asked in interview by Appirio. Wondering why people are posting company quizes here


Well there is better way to resolve this but here something I can explain yo, if you re-read the log carefully you will find 


1. List has no rows


 Here is your code


list <contact> con = trigger.new;

 You are trying to populate list but this is not making and SOQL call ( is it  ?) henceforth the 'con' list is empty cleraly 


Now you make a call to static method/function called


updateConf(list<contact> con)

 This will be empty call, henceforth you get brake in the code 



Possible Solution -:


Make a for loop on Contacts over Trigger.New and populate the list using SOQL Query


Other way


for(Sobject  _sobj : Trigger.New)



List<Contact> oldContact = [Select id from Contact where ContactId=: _sobje.id];    // populate the list on event


Once you reach here, you got run another for loop for each oldcontact you get in list  and update the corresponding field


and at last update Sobject




I hope this might be of help for you to resolve the current error

Chamil MadusankaChamil Madusanka
trigger ConferenceNumberUpdateTrigger on Contact (before insert, before update) {

   list <contact> con = trigger.new;
if(con.size() > 0) { ConfNumberClass.updateConf(con); }else { //populate related error here }


 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.


Bit skeptical about this approach, not sure but whenever you try populate without making any SOQL call it would be possibly be always empty, would be needing more advice here



Another sample piece could be of help


trigger testReqTrigger on Account (after update) {
    for(Account acc : Trigger.New){
    List<Contact> listCon = [Select Fax from Contact where AccountId =: acc.id];        for(Contact con : listCon){
        con.Fax = acc.Fax;
    update listCon;




I hope this makes more sense, this is something exactly what you need and was asked in Appirio Company Interview