// match existing contact Contact[] cs = [select id, accountid, account.ownerid, account.owner.userRoleId from Contact where email = :email]; if(String.isNotBlank(accountId)){ for (Account a : [select Id, Owner.UserRoleId from Account where Id = :accountId]) { if(a.Owner.UserRoleId == null) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Account Owner Missing Role')); return null; } } }