+ Start a Discussion
rakhi78rakhi78 

execution of AfterUpdate caused .QueryException: List has no row for assignment to SObject:Trig

trigger PhyPortalPatientApexSharing on Patient__c (after insert, after update)
{
    // CREATE A NEW LIST OF SHARING OBJECTS FOR PATIENT.
    List<Patient__Share> patientShrs  = new List<Patient__Share>();
  
    // DECLARE VARIABLES FOR PATIENT SHARING.
    Patient__Share physicianShr;
    Patient__Share patientShr;
      
    for(Patient__c p : Trigger.new)
    {
        // for associated prescription associated protal user.
       for(Prescription__c prescriptions : [Select id,Patient_MRN__c,r.Physician_Contact__r.Portal_User_ID__c   From Prescription__c r where r.Physician_Contact__r.Portal_User_ID__c!=null])
       {
       if(prescriptions != NULL)
       {
       // select the id of patient that needs to be shared with the user whcih is attched with prescription
          Patient__c Patient = [Select id, name From Patient__c Where name=:prescriptions.Patient_MRN__c];
           
            // INSTANTIATE THE SHARING OBJECTS.
          
            physicianShr = new Patient__Share();
           
            // SET THE ID OF THE RECORD BEING SHARED.
            physicianShr.ParentId = Patient.id;
           
            // GET THE ID OF THE RELATED USER BEING GRANTED ACCESS. THERE MAY BE MORE THAN 1
            // PHYSICIAN PORTAL USER TO SHARE TO.
           //Prescription__c pe = [Select Physician_Contact__r.Portal_User_ID__c From Prescription__c Where Id=:prescriptions.Id];
                             
            // SET THE ID OF THE USER BEING GRANTED ACCESS.
            physicianShr.UserOrGroupId = prescriptions.Physician_Contact__r.Portal_User_ID__c ;
                                   
            // SET THE ACCESS LEVEL.
            physicianShr.AccessLevel = 'read';
                       
            // SET THE APEX SHARING REASON FOR PHYSICIAN.
            physicianShr.RowCause = Schema.Patient__Share.RowCause.Physician_Portal_User__c;
                       
            // ADD OBJECTS TO LIST FOR INSERT.
            patientShrs.add(physicianShr);
                           }//end of for loop
       }//edn if loop
            }
                 
    // INSERT SHARING RECORDS AND CAPTURE SAVE RESULT.
    // FALSE PARAMETER ALLOWS FOR PARTIAL PROCESSING OF MULTIPLE RECORDS.
    Database.SaveResult[] lsr = Database.insert(patientShrs,false);
    // CREATE COUNTER
    Integer i=0;
       
    // PROCESS SAVE RESULTS
    for(Database.SaveResult sr : lsr)
    {
        if(!sr.isSuccess())
        i++;  
    }
}

 

 

 

Can some one please tell me why I am getting no rows for assignment to SObject: Trigger

aalbertaalbert

One of your queries in the trigger is throwing the exception when no rows are returned. Can't tell from your post which query.

 

Here is a link to the docs that talk about handling query results: Link

rakhi78rakhi78

Thanks for you reply 

I have modified my 2nd query as below

 

 Patient__c Patient = [Select id, name From Patient__c Where id=:prescriptions.Patient_MRN__c];

 

  Now i am getting System.Exception: Too many SOQL queries: 21: Trigger.PhyPortalPatientApexSharing: line 20, column 32

aalbertaalbert

You are hitting a governor limit since you have some queries inside for loops - therefore executing during each iteration of the loop.You want to move your queries outside the for loop and query the proper records you need to process.

 

I recommend these two articles:

http://wiki.developerforce.com/index.php/Governors_in_Apex_Code

http://wiki.developerforce.com/index.php/Apex_Code_Best_Practices