0

:) I am writing test class for controller extensions, but I get the error: System.TypeException: Invalid conversion from runtime type Asset to Account.

Can you please look at my code, and tell me what I am doing wrong?

Controller Extensions:

    public class assetonaccount{

    public List<Asset> device{
        get;
        set;
    }
    
    public assetonaccount(
        ApexPages.StandardController controller
    ){  
       
        device = 
            [
                SELECT 
                    AssetLevel,
                    Id,
                    Name, SerialNumber, InstallDate, Last_Calibration__c, Next_Calibration__c, Asset_tag_no__c
                FROM   
                    Asset
                WHERE  AssetLevel = 1 AND
                    AccountId = 
                        :((Account)controller.getRecord()).Id
            ];
    }
}

Apex Test Class:

    @isTest
private class AssetOnAccountTest {
    
  static testMethod void TestAccountOnAssetExtensions()
  {
     date installdate = System.today() - 20;
     date nextcal = System.today() + 15;
     date lastcal = System.today() - 7;

      //Insert Account
      Account acct = new Account(Name='TestClassAccount', Customer_Segment__c = 'Hospitals', Calibration_Intervals__c = 'Half year');
      insert acct;

      // Retrieve the new Account
      acct = [SELECT Id,Name,Customer_Segment__c, Calibration_Intervals__c FROM Account WHERE Id =:acct.Id];

      //Insert Asset
      Asset asst = new Asset(Name='Asset test', SerialNumber = '1234', InstallDate=installdate, Last_Calibration__c=lastcal, Next_Calibration__c=nextcal, Asset_tag_no__c='12', AccountId=acct.id);
      insert asst;

     //Retrieve the new asset
      asst = [SELECT Id, AssetLevel, Name, SerialNumber, InstallDate, Last_Calibration__c, Next_Calibration__c, Asset_tag_no__c from Asset WHERE Id =:asst.Id];

      
      ApexPages.StandardController stdController = new ApexPages.StandardController(asst);
      
      assetonaccount controller = new assetonaccount(stdController);
      
      system.assertNotEquals(controller.device, null);
      system.assertEquals(controller.device.size(), 1); 
      system.assertEquals(controller.device[0].Last_Calibration__c, lastcal);
      
  }
}

1 Answer 1

1

When you're instantiating the StandardController, your passing an Asset record. In the assetonaccount class your constructor method calls controller.getRecord() which will return the record your passed in when creating the StandardController instance (docs), which was an Asset record. You then attempt to Cast that record to an Account with the following code:

((Account)controller.getRecord()).Id

Which is not allowed, because and Asset is never a type of Account. It appears your intention is to retrieve all Assets for a particular Account in which case you should use the AccountId field from the Asset record. i.e.

// Get the account Id for the current asset
Id accountId = ((Asset)controller.getRecord()).AccountId;  

// Load all other assets for the account
device = [
  SELECT 
    AssetLevel,
    Id,
    ...
  FROM   
    Asset
  WHERE
    AssetLevel = 1
  AND
    AccountId = :accountId
];
2
  • Hi! :) I changed the code as you wrote, but now I am getting new error message: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Asset.AccountId I added the accountId into query. The error message shows on line 11, below device query where I added the Id accountId = ((Asset)controller.getRecord()).AccountId;
    – ApexNewbie
    Sep 9 at 8:52
  • It works! Thank you! I added the accountId into device query on test class :)
    – ApexNewbie
    Sep 9 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.