You need to sign in to do that
Don't have an account?
LloydC
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.
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; } } }
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.