[SalesForce] CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY on Apex Test Class

I've been trying to create and implement an Apex Trigger/Apex Test Class for the Asset object to automatically populate an Asset custom lookup field, "Partner_Client__c", with the same information as is in the Account custom lookup field "Partner_Client_Name__c".

Apex Trigger "Asset_Autopopulate":

trigger Asset_Autopopulate on Asset (before insert, before update) {
    Set<String> clientIds = new Set<String>();
    for (Asset ass : Trigger.new) {
        clientIds.add(ass.AccountId);
    }
    List<Account> clients = [SELECT Id, Partner_Client_Name__r.Id FROM Account WHERE Id =: clientIds];
    Map<String, Asset> assetMap = new Map<String, Asset>();
    for (Asset ass : Trigger.new) {
        assetMap.put(ass.AccountId, ass);
    }
    for (Account acc : clients) {
        if (assetMap.containsKey(acc.Id)) {
            assetMap.get(acc.Id).Partner_Client__r.Id = acc.Partner_Client_Name__r.Id;
        }
    }
    update clients;
}

Associated Apex Test Class "Asset_Autopopulate_Test":

@isTest
private class Asset_Autopopulate_Test {
    static testMethod void Asset_Autopopulate_TestMethod() {

        Account acc = new Account();
        acc.Name = 'Test Client #1';
        acc.Rating = 'Not Started';
        acc.OwnerId = '005E00000050KGS';
        acc.Website = 'TestLeads-Company#1.com';
        acc.Phone = '0000000000';
        acc.Other_Phone__c = '1111111111';
        acc.Industry = 'Other';
        acc.Description = 'Test Client #1 Description';
        acc.Partner_Client_Name__c = '001E000001EczmwIAB';
        insert acc;

        Asset ass = new Asset();
        ass.Name = 'Test Asset #1';
        ass.AccountId = acc.Id;
        // ass.OwnerId = '005E00000050KGS';

        test.startTest();
        insert ass;
        update ass;
        test.stopTest();
    }
}

The trigger itself seems to be working fine, and most of the Test Class seems to be working, but every time I attempt to validate these two and deploy them, I am met with this failure condition:

"Asset_Autopopulate_Test" Apex Test Class Failure Condition(s)

Anything that could help me better understand how/why this is happening and what measures I should take in order to solve this problem would be greatly appreciated and apologies for my liberal use of commas.

Best Answer

acc.Partner_Client_Name__c = '001E000001EczmwIAB';

You're trying to assign a hard-coded ID value without having created the record or using SeeAllData=true. This means that the database "can't see" the record and will result in an error when you try to use a DML operation with it.

Create your "Partner Client" record first, then use the value from that record for the Partner_Client_Name__c field.

Additionally, you shouldn't be using __r.Id to assign a value. Technically, you're trying to assign a value to a null object, which should be causing errors. Change every instance of __r.Id to __c in your code.

Related Topic