I am trying to update this apex trigger from an earlier thread: One Primary Ownership on Property
What I want to do seems like it should be simple to me--I simply want it to update the Primary contact field on the Property information with the contact field from the Ownership object. But I am getting an error: System.QueryException: List has no rows for assignment to SObject
I don't understand why this would be happening. It seems like a simple query to me.
trigger primaryContactOwnership on MRG_Ownership__c (after insert,after update)
{
ownerTrigger();
public void ownerTrigger(){
List<Id> primaryOwnershipIdList = new List<Id>();
List<Id> relatedPropertyIdList = new List<Id>();
for(MRG_Ownership__c ownership : Trigger.New)
{
if(ownership.Primary_Contact__c == true && (Trigger.Old == null || Trigger.OldMap.get(ownership.Id) == null
|| Trigger.OldMap.get(ownership.Id).Primary_Contact__c != true)) // To handle null scenario
{
primaryOwnershipIdList.add(ownership.Id);
relatedPropertyIdList.add(ownership.Property__c);
Property__c property= [select id, Primary_Contact2__c from Property__c WHERE id =:ownership.Property__r.id limit 1];
property.Primary_Contact2__c=ownership.contact__c;
update property;
}
}
List<MRG_Ownership__c> relatedOwnershipList = [Select Id, Primary_Contact__c
From MRG_Ownership__c
Where Property__c in :relatedPropertyIdList
And Primary_Contact__c = true
And Id Not in :primaryOwnershipIdList];
if(relatedOwnershipList.size() > 0)
{
for(MRG_Ownership__c ownership : relatedOwnershipList)
ownership.Primary_Contact__c = false;
update relatedOwnershipList;
}
}
}