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
bouscalbouscal 

Can't create test object

Hello gurus,
I'm trying to be a better developer by writing the test code first.  I can't create all the needed test objects.  Account, Contact, Case are created and I can see the IDs in the debug log but get an error when creating the Asset.  What am I overlooking?

There is no other code created yet.
 
@IsTest(SeeAllData=True)

public class AssetUpdateTest {
    
    public static testmethod void testAssetLink()    {
        Account acc = createAccount(); 
        Contact con = createContact(acc); 
        Case cas = createCase(acc,con); 
        Asset ast = createAsset(acc,con,cas); 

        System.debug('**************************   Case ID: ' + cas.id); 
        System.debug('**************************   Case.Asset: ' + cas.AssetId);
        
    }
    
    public static Account createAccount(){
        Account theAccount = new Account(
            recordtypeid='01270000000Q7rQ', 
            name='Test Account', 
            Status__c='Client',
            Line_of_Business__c='CRS',
            phone='8005551212', 
            currencyisocode='USD'); 
        
        insert theAccount;
        return theAccount; 
    }    
    
    public static Contact createContact(Account theAccount){
        Contact theContact = new Contact(
            recordtypeid='01270000000Q7ra',
            email='tim.bouscal@audatex.com', 
            firstname='George', 
            lastname='Washington',
            accountid=theAccount.id);
        insert theContact;        
        return theContact; 
    }
    
    public static Case createCase(Account acc, Contact con){
        Case theCase = new Case(
            recordtypeid='01270000000Dy9u', 
            AccountId = acc.Id, 
            ContactId = con.Id,             
            
            Requestor__c=con.Id, 
            Reason='New Account Setup', 
            Reason_Detail__c='New - ADXE Shop 02',
            Status='New', 
            Origin='Sales Request', 
            Subject='AssetUpdateTest Sample BisOps Case', 
            Description='This is a sample case for testig the AssetUpdate class'
        ); 
        insert theCase; 
        return theCase;
    }    
    
    public static Asset createAsset(Account acc, Contact con, Case cas){
        System.debug('***** ***** ***** Account ID: ' + acc.id + '     Contact Id: ' + con.id + '     Case ID: '+ cas.Id); // all 3 values show in debug log
        Asset theAsset = new Asset(
            Name='Test Asset', 
            Account = acc, 
            Contact = con, 
            Case_Number__c = cas.CaseNumber, 
            Type__c='New', 
            Status='Purchased', 
            Description='This is a test asset for testing the AssetUpdate class'
        );
        insert theAsset; 
        return theAsset; 
    }
}

Error: 
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Every asset needs an account, a contact, or both.: [AccountId, ContactId]

Class.AssetUpdateTest.createAsset: line 69, column 1
Class.AssetUpdateTest.testAssetLink: line 9, column 1

 
Best Answer chosen by bouscal
Shawn Reichner 29Shawn Reichner 29
Looks like you trouble is on 62,63 and 64....small suggestion...it could be because you are providing the vaiable name, but not .ID..so try acc.Id and con.Id and cas.CaseNumber.Id

Did this help?



Shawn

All Answers

Shawn Reichner 29Shawn Reichner 29
Looks like you trouble is on 62,63 and 64....small suggestion...it could be because you are providing the vaiable name, but not .ID..so try acc.Id and con.Id and cas.CaseNumber.Id

Did this help?



Shawn
This was selected as the best answer
Alain CabonAlain Cabon
Shawn has the beginning of the solution.

Account and Contact are the names of the relationships.

AccountId = acc.id,
ContactId = con.id,


User-added image

 
bouscalbouscal
Thank you Shawn and Alain, I knew it was something I'd overlooked.  :-)