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
ilewi121ilewi121 

Data changed by trigger for field Owner ID: owner cannot be blank

I am adding a trigger to default a Contact owner to the Account owner. It works fine and passes the test I wrote for it. HOWEVER, it fails a test from another piece of apex, telling me:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, reassignCtOwnerToAcOwner: data changed by trigger for field Owner ID: owner cannot be blank: []

Here is the trigger I'm trying to deploy:

trigger reassignCtOwnerToAcOwner on Contact (before insert, before update) {

    List<Id> accountIds = new List<Id>();
    Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();

    // all the accounts whose owner ids to look up
    for ( Contact c : Trigger.new ) {
        accountIds.add( c.accountId );
    }
   
    // look up each account owner id
    for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
        accountOwnerIdMap.put( acct.id, acct.ownerId );
    }
   
    // change contact owner to its account owner
    for ( Contact c : Trigger.new ) {
        c.ownerId = accountOwnerIdMap.get( c.accountId );
    }
}

Here is the test class for the OTHER trigger that fails when I try to insert a contact:

public static testMethod void validateContactCountsAndActivities() {

        //Create Account
  Account ac = new Account(
   Name='Test Account',
   Initial_Lead_Source__c='Cold Call',
   Initial_Lead_Source_Detail__c='Cold Call',
   Account_Type__c='Prospect',
   Account_Status__c='Not Contacted'   );
  insert ac;

        //Create Contact
        Contact ct = new Contact(
                LastName='Test Contact',
                Email='testcontact@email.com',
                Account= ac);
        insert ct;



RajaMohanRajaMohan
Hi There,

Please check whether the user is having the appropriate permission on Contact. If it is happening in production the check the permissions on production.

Follow the link for your understanding on this error,

http://kb.omni-ts.com/entry/68/

Let me know if this didn't help.

Regards
Raja
ilewi121ilewi121
It shouldn't be an owner permissions issue. The other apex doesn't even reference ownership and the the test has the following wrapper:

@isTest(SeeAllData=true)
private class TP_CountsAndActivities_TestClass{
public static testMethod void validateLeadCountsAndActivities() {
//Test Goes here
}
}


ilewi121ilewi121
Has anyone developed a solution to this issue?
Fabrice76Fabrice76
I wrote exactly the same trigger but i've got one difference

trigger reassignCtOwnerToAcOwner on Contact (before insert, before update) {

    List<Id> accountIds = new List<Id>();
    Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();

    // all the accounts whose owner ids to look up
    for ( Contact c : Trigger.new ) {
        if (c.accountId != null)               ///////////////////////////////////////////////////// <-- The Difference
           accountIds.add( c.accountId );
    }
   
    // look up each account owner id
    for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
        accountOwnerIdMap.put( acct.id, acct.ownerId );
    }
   
    // change contact owner to its account owner
    for ( Contact c : Trigger.new ) {
        c.ownerId = accountOwnerIdMap.get( c.accountId );
    }
}


Paul ScottPaul Scott
The problem is in your test code.

change
Contact ct = new Contact(
                LastName='Test Contact',
                Email='testcontact@email.com',
                Account= ac);
to
Contact ct = new Contact(
                LastName='Test Contact',
                Email='testcontact@email.com',
                Accountid = ac.id);
Assigning the Contact's Account parent object alone wont set the accountid field.

If you make the change Fabrice76 suggests then you will also want to change
for ( Contact c : Trigger.new ) {
  c.ownerId = accountOwnerIdMap.get( c.accountId );
 }
 to
for ( Contact c : Trigger.new ) {
         if(accountOwnerIdMap.containsKey(c.accountid))
	        c.ownerId = accountOwnerIdMap.get( c.accountId );
}