0

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.

0

1 Answer 1

3
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.

3
  • I've changed the "Asset_Autopopulate_Test" Test Class to now initially create and insert an Account object called "partner_acc", I've changed the original account to be called "asset_acc", but now when I try to I'm receiving a failure condition stating: "Illegal Assignment from Account to Id". I figured it has to be one of these two statements, but I cannot seem to figure out which one: asset_acc.Partner_Client_Name__c = partner_acc; ass.Account = asset_acc;
    – Kyle Walsh
    May 18, 2016 at 19:42
  • @KyleWalsh asset_acc.Partner_Client_Name__c = partner_acc.Id; ass.AccountId = asset_acc.Id; The point is that you should be assigning ID values to fields, not relationship references.
    – sfdcfox
    May 18, 2016 at 19:46
  • As soon as I did it and commented it I realized my mistake. Thanks for the help!
    – Kyle Walsh
    May 18, 2016 at 19:49

You must log in to answer this question.

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