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
LloydCLloydC 

trigger List has no rows for assignment to SObject

Hi,
I am writing a before insert trigger that updates a field on the account whenever an attachment is added but only if the person adding the attachment is not the owner.  I try to select the owner of the associated account based on the attachment.parentid but it doesn't return any rows.  I tried to hardcode the parentid and it still doesn't return any rows.  The row is definitely there because I can query it and it does return a row.  Why does it not return the row.  Below are the trigger definition and the test class.  Thank you.
trigger AttachmentTrigger on Attachment (before insert) {
	List<Account> accountList = new List<Account>();

   Set<Id> accIds = new Set<Id>(); 
     for(Attachment att : trigger.New){
         //Check if added attachment is related to Account or not
         if(att.ParentId.getSobjectType() == Account.SobjectType){
      	Account a	= [select id,OwnerId from account where id = '001M000000Y2Rbx']; // :att.ParentId];
      	if (a.OwnerId != att.OwnerId)
              accIds.add(att.ParentId);}

    }
    accountList = [select id, has_Attachment__c from Account where id in :accIds];
    if(accountList!=null && accountList.size()>0){
        for(Account acc : accountList){
            acc.has_Attachment__c = true; 
        }
        update accountList;
    }         
	

}
private class TestAttachmentTrigger {

    static testMethod void myUnitTest() {
//    	Account[] a = [Select Id,name from account where Id = '001M000000Y2Rbx'];
    	User u2 = [select id from User where alias='administrator']; 
    	system.runAs(u2){
    	Blob body;
    	body = Blob.valueOf('test');
    	Attachment Attach = new Attachment();
    	Attach.ParentId = '001MxyzbwY2Rbx';  //id of an existing account
    	Attach.OwnerId = u2.Id;
    	Attach.Name = 'newattach';
    	Attach.Body = body;
    	insert Attach;
    	}

    }
}



 
Best Answer chosen by LloydC
Romeo Ngo 1Romeo Ngo 1
When test start, it will not have any records in any objects.  If you want to use the existed data in your Salesforce then you can use add "IsTest(SeeAllData=true)" to your test class/method.  I would not recommend this.  Simply add new account and then run your test.

Simply add your test data before you run your test.  This will separate your environment in case someone accidentally change your test data and then your test fail in the future.
private class TestAttachmentTrigger {

    static testMethod void myUnitTest() {

Account acc1 = new Account(Name='a1');
insert acc1;

//    	Account[] a = [Select Id,name from account where Id = '001M000000Y2Rbx'];
    	User u2 = [select id from User where alias='administrator']; 
    	system.runAs(u2){
    	Blob body;
    	body = Blob.valueOf('test');
    	Attachment Attach = new Attachment();
    	Attach.ParentId = acc1;
    	Attach.OwnerId = u2.Id;
    	Attach.Name = 'newattach';
    	Attach.Body = body;
    	insert Attach;
    	}

    }
}