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
Andrew Hoban 6Andrew Hoban 6 

Error from test class when trying to deploy a changeset

Hi all,

Iam trying to deploy a small changeset into my production org. I have a test class and normla class and these both work fine. However, there is a test class already in the org that seems to be failing. The error message it is giving me is:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You do not have permissions to create a Contact without an Account: [] 
Stack Trace: Class.TalentIntegrationTestSuite.testContactInsertTrigger: line 13, column 1

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You do not have permissions to create a Contact without an Account: [] 
Stack Trace: Class.TalentIntegrationTestSuite.testContactUpdateTrigger: line 22, column 1

This particular test class was created several years ago by a different person. I have tried disabling all validation rules in Accounts and Contacts however the error messgae is still the same. Any help would be much appreciated. Thanks.

Test Class:
@isTest
private class TalentIntegrationTestSuite {
   
    static testMethod void testContactInsertTrigger() {
        activateIntegration();
        
        Contact c = new Contact();
        
        c.FirstName = 'John';
        c.LastName = 'Smith';
        
        Test.startTest();
        insert c;
    }
    
    static testMethod void testContactUpdateTrigger() {
        activateIntegration();
        
        Contact c = new Contact();
        c.FirstName = 'John';
        c.LastName = 'Smith';        
        insert c;
        
        c = [Select Id, FirstName, LastName from Contact where Id=:c.Id];
        
        c.FirstName = 'Mary';
        Test.startTest();        
        update c;
    }

    static testMethod void testAccountInsertTrigger() {
        activateIntegration();
        
        Account a = new Account();
        a.RecordTypeId = [Select Id From RecordType Where isPersonType=true and sObjectType='Account' Limit 1].Id;
        a.FirstName = 'John';
        a.LastName = 'Smith';
        
        Test.startTest();
        insert a;
    }
    
    static testMethod void testAccountUpdateTrigger() {
        activateIntegration();
        
        Account a = new Account();
        a.RecordTypeId = [Select Id From RecordType Where isPersonType=true and sObjectType='Account' Limit 1].Id;
        a.FirstName = 'John';
        a.LastName = 'Smith';        
        insert a;
        
        a = [Select Id, FirstName, LastName from Account where Id=:a.Id];
        
        a.FirstName = 'Mary';
        Test.startTest();        
        update a;
    }
    
    static testMethod void testBusinessAccountUpdateTrigger() {
        activateIntegration();
        
        Account a = new Account();
        a.RecordTypeId = [Select Id From RecordType Where isPersonType=false and sObjectType='Account' Limit 1].Id;
    a.Name = 'Fabrikam';       
        
        insert a;
        
        Contact c = new Contact();
        c.FirstName = 'John';
        c.LastName = 'Smith';
        c.AccountId = a.Id;
        
        insert c;
        
                
        a = [Select Id, BillingPostalCode from Account where Id=:a.Id];
        
        a.BillingPostalCode = 'NE30 2PL';
        Test.startTest();        
        update a;        
    }
    
    static void activateIntegration()
    {
      CustomIntegrationSetting__c setting = CustomIntegrationSetting__c.getValues('CustomerTalentIntegration');      
    if (setting.isActive__c==false)
    {
      setting.isActive__c = true;
      update setting;  
    }  
    }
}

 
Best Answer chosen by Andrew Hoban 6
lakslaks
In testContactInsertTrigger(), testContactUpdateTrigger(), you can make the following modifications -

static testMethod void testContactInsertTrigger() {
        activateIntegration();

        Account acct = new Account(name='test account');
        insert acct;
        
        Contact c = new Contact();
        
        c.FirstName = 'John';
        c.LastName = 'Smith';
        c.AccountId = acct.Id;
        
        Test.startTest();
        insert c;
    }
    
    static testMethod void testContactUpdateTrigger() {
        activateIntegration();
        
        Account acct = new Account(name='test account');
        insert acct;
        
        Contact c = new Contact();
        c.FirstName = 'John';
        c.LastName = 'Smith';
        c.AccountId = acct.Id;        
        insert c;
        
        c = [Select Id, FirstName, LastName from Contact where Id=:c.Id];
        
        c.FirstName = 'Mary';
        Test.startTest();        
        update c;
    }

 

All Answers

lakslaks
Hi Andrew,

I am not sure why you are getting the error even after turning off the validation rules in the Production org.

However if you want to move in your code without modifying the above mentioned older test class, the way to go would be to modify the validation rules to bypass the Profile that you are using for moving in the code.

I wouldn't recommend the above unless it is absolutely essential though. The better way would be to alter the test class so that it doesn't throw the validation error. However if you are not supposed to modify those classes, you can try the above method.

Regards,
Lakshmi.
Sfdc CloudSfdc Cloud
Hi Andrew,

There will be some validation rule on contact object like Account name should not be blank while creating contact record.So first insert account record in test class.
Please let me know if you will face further issue

If this will helps you out.Mark it to help others.
Thanks :)
 
Andrew Hoban 6Andrew Hoban 6
Thankyou to both. Yes I have diabled all of the validation rules in Account on Contact but have still had no luck. Sall I edit the test class in the sandbox to the one below and try to deploy it from a changeset/ Thanks?
 
@isTest
private class TalentIntegrationTestSuite {
   
    

static testMethod void testAccountInsertTrigger() {
        activateIntegration();
        
        Account a = new Account();
        a.RecordTypeId = [Select Id From RecordType Where isPersonType=true and sObjectType='Account' Limit 1].Id;
        a.FirstName = 'John';
        a.LastName = 'Smith';
        
        Test.startTest();
        insert a;
    }
    
    static testMethod void testAccountUpdateTrigger() {
        activateIntegration();
        
        Account a = new Account();
        a.RecordTypeId = [Select Id From RecordType Where isPersonType=true and sObjectType='Account' Limit 1].Id;
        a.FirstName = 'John';
        a.LastName = 'Smith';        
        insert a;
        
        a = [Select Id, FirstName, LastName from Account where Id=:a.Id];
        
        a.FirstName = 'Mary';
        Test.startTest();        
        update a;
    }
        
static testMethod void testContactInsertTrigger() {
        activateIntegration();

        Contact c = new Contact();
        
        c.FirstName = 'John';
        c.LastName = 'Smith';
        
        Test.startTest();
        insert c;
    }
    
    static testMethod void testContactUpdateTrigger() {
        activateIntegration();
        
        Contact c = new Contact();
        c.FirstName = 'John';
        c.LastName = 'Smith';        
        insert c;
        
        c = [Select Id, FirstName, LastName from Contact where Id=:c.Id];
        
        c.FirstName = 'Mary';
        Test.startTest();        
        update c;
    }

    
    
    static testMethod void testBusinessAccountUpdateTrigger() {
        activateIntegration();
        
        Account a = new Account();
        a.RecordTypeId = [Select Id From RecordType Where isPersonType=false and sObjectType='Account' Limit 1].Id;
    a.Name = 'Fabrikam';       
        
        insert a;
        
        Contact c = new Contact();
        c.FirstName = 'John';
        c.LastName = 'Smith';
        c.AccountId = a.Id;
        
        insert c;
        
                
        a = [Select Id, BillingPostalCode from Account where Id=:a.Id];
        
        a.BillingPostalCode = 'NE30 2PL';
        Test.startTest();        
        update a;        
    }
    
    static void activateIntegration()
    {
      CustomIntegrationSetting__c setting = CustomIntegrationSetting__c.getValues('CustomerTalentIntegration');      
    if (setting.isActive__c==false)
    {
      setting.isActive__c = true;
      update setting;  
    }  
    }
}

 
lakslaks

Changing the test class so that the Account test methods are executed before will not solve the issue because test classes do not do an actual insert. It's scope is only within the execution.

You will have to create an Account object test data within the test methods - testContactInsertTrigger(), testContactUpdateTrigger() before the Contact object insert is done in both of them.

lakslaks
In testContactInsertTrigger(), testContactUpdateTrigger(), you can make the following modifications -

static testMethod void testContactInsertTrigger() {
        activateIntegration();

        Account acct = new Account(name='test account');
        insert acct;
        
        Contact c = new Contact();
        
        c.FirstName = 'John';
        c.LastName = 'Smith';
        c.AccountId = acct.Id;
        
        Test.startTest();
        insert c;
    }
    
    static testMethod void testContactUpdateTrigger() {
        activateIntegration();
        
        Account acct = new Account(name='test account');
        insert acct;
        
        Contact c = new Contact();
        c.FirstName = 'John';
        c.LastName = 'Smith';
        c.AccountId = acct.Id;        
        insert c;
        
        c = [Select Id, FirstName, LastName from Contact where Id=:c.Id];
        
        c.FirstName = 'Mary';
        Test.startTest();        
        update c;
    }

 
This was selected as the best answer
Andrew Hoban 6Andrew Hoban 6
So inside my testContactInsertTrigger(), testContactUpdateTrigger() methods I should put: Thanks for you help.
 
static testMethod void testContactInsertTrigger() {
        activateIntegration();
        
        Contact c = new Contact();
        
        c.FirstName = 'John';
        c.LastName = 'Smith';
        c.account= a.id;    
        Test.startTest();
        insert c;
    }
    
    static testMethod void testContactUpdateTrigger() {
        activateIntegration();
        
        Contact c = new Contact();
        c.FirstName = 'John';
        c.LastName = 'Smith';   
        c.account= a.id;     
        insert c;
        
        c = [Select Id, FirstName, LastName from Contact where Id=:c.Id];
        
        c.FirstName = 'Mary';
        Test.startTest();        
        update c;
    }

 
lakslaks
Please check the modified code I posted above.