2

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;
        }


        }
    }

1 Answer 1

2

The problem will be with the line:

Property__c property= [select id, Primary_Contact2__c from Property__c 
                       WHERE id =:ownership.Property__r.id limit 1];

Change it to:

Property__c property= [select id, Primary_Contact2__c from Property__c 
                       WHERE id = :ownership.Property__c limit 1];

In the trigger context, ownership.Property__r.id will likely be null. Hence when the query executes there will be nothing to assign back to property. However, since this is a relationship field you can use the case field to get the Id.

You should probably also:

  • Check that ownership.Property__c is not null.
  • Assign the SOQL query result back to a List<Property__c>. You can then check the size() of the resulting list. This will avoid the common "List has no rows for assignment to SObject" QueryException that occurs when the query doesn't return any records. See Using SOQL Queries That Return One Record
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .