1

We have a custom object called Lead__c for which I'm trying to execute DML on it to insert a record. However, I need to assign the owner to it.

I've got the following query to search for an owner:

User owner = [SELECT Id from USER WHERE FirstName = 'MyAccount' LIMIT 1][0];

Which pulls back a single record, as expected, in Workbench.

I then have the following DML:

Lead__c l = new Lead__c(
  //Owner = owner,
  OwnerId = owner.Id,
  External_Company__c = company,
  Name__c = name,
  Email__c = email,
  Source__c = source,
  RecordTypeId = rt.Id
);

INSERT l;

I can't assign the owner directly (hence why it's commented out) because I get the following error:

Field is not writeable: Owner

Removing that line and it compiles fine, but when the DML is executed I'm getting the following error in the debug logs:

Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]

What am I missing here, I understand Owner is polymorphic but I can't see why it wouldn't assign the queried user to the field?

2
  • can u add system.debug(owner.id) and see what you get in the logs.. Commented Nov 2, 2015 at 13:08
  • @VamsiKrishna Thanks for responding. I found the cause of the error and posted an answer as a result. Just a stack trace error rather than something to do in this particular class itself.
    – Dan Jones
    Commented Nov 2, 2015 at 13:13

1 Answer 1

0

Fired this too early here, more or less found the problem right after I posted this!

This was linked to another Trigger which was creating an activity off the back of new Leads__c, assigning the owner of that activity based off another custom field.

Looks like this:

activity.OwnerId = n.Assigned_To__c;

I've got a bit of tidying up to do. I'll read the debug logs properly going forward (><)

You must log in to answer this question.

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