You need to sign in to do that
Don't have an account?
ilewi121
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:
Here is the test class for the OTHER trigger that fails when I try to insert a contact:
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 );
}
}
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;
//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;
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
@isTest(SeeAllData=true)
private class TP_CountsAndActivities_TestClass{
change
to
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
to