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
Lee_CampbellLee_Campbell 

Assigning Record Owner Programmatically

Hi folks,

 

I'm trying to conditionally set the OwnerID of a record based upon who the owner of certain other records is. I have two custom objects, Procurement__c and Demo__c. Procurement__c has a lookup field for an Account and one for Opportunity. A Demo__c record is created upon creating a Procurement__c when it meets a certain criteria (picklist value), or updating a Procurement__c record if its status is changed to one of the picklist values that would have created a Demo__c upon inserting a Procurment__c. I want to set the owner of the Demo__c according to the following criteria:

 

If the Account recorded on the Procurement__c is not blank, make the Demo__c's owner that Account owner.

(demo.OwnerId = proc.Account__r.OwnerId;)

 

If it is blank, and the Opportunity recorded on the Procurement__c is not blank, use that Opportunity owner.

(demo.OwnerId = proc.Opportunity__r.OwnerId;)

 

If both the Opportunity and Account are blank, just use the Procurement__c owner.

(demo.OwnerId = proc.OwnerId;)

 

If I try that logic, I get an error message:

 

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]: and then my trigger name.

 

Can anyone think why that might be? I've tried both before and after insert and update, and none of the permutations work. Any tips?

 

Thanks,

Lee

Best Answer chosen by Admin (Salesforce Developers) 
Jeff MayJeff May

The trigger contains the fields from the proc object but not all the fields on all related objects.

 

You'll want to get Account and Opp fields into their own Maps <prodID, Account> and <prodID, Opportunity>

 

then when you iterate through the procs, you can look up the appropriate Account and Opp fields.

All Answers

Jeff MayJeff May

The easiest way to track this down will be to use system.debug() statements and use the Dev Console or Debug Logs to see what they show. 

Lee_CampbellLee_Campbell
My system.debug results for proc.Account__r.OwnerID,
proc.Opportunity__r.OwnerID; and proc.OwnerID are all null, which, to
me, seems impossible...
Jeff MayJeff May

are you including those fields in your SELECTS?

Lee_CampbellLee_Campbell
I don't need to, do I? Aren't they're implicit in the trigger if all of
the logic takes place within:



for(Procurement__c proc : trigger.new){



...



}



I don't need a SELECT statement to make, say, demo.Name and proc.Name
equal...
Jeff MayJeff May

The trigger contains the fields from the proc object but not all the fields on all related objects.

 

You'll want to get Account and Opp fields into their own Maps <prodID, Account> and <prodID, Opportunity>

 

then when you iterate through the procs, you can look up the appropriate Account and Opp fields.

This was selected as the best answer
Lee_CampbellLee_Campbell
Cool. Thanks. I'm replying by email at the moment, but I'll surely mark
that as the solution once I get it working doing that.