+ Start a Discussion
LaaralLaaral 

@isTest

Hey, can you help me with this problem with @isTest. I'm new in writing test class, but I created a batch that updates case-objects data for example Fault_Classification. Now I'm not sure if my code is right but it's shouting an error Save error: Invalid field subject for SObject Account in the marked area.

@isTest
private class ImplementServiceAvailabilityTest {

    static testMethod void ImplementServiceAvailabilityTest() {
        // TO DO: implement unit test
        List<Account> accounts = new List<Account>();
        
        Account mai = new Account(Name = 'LauT', RecordTypeId = '0503KfG66');
        accounts.add(mai);
        Account cc = new Account(Name = 'Cost Account', RecordTypeId = '012D0000000NK4p', BillingStreet = 'testikatu 7', BillingPostalCode = '007', BillingCity = 'testcity', BillingCountry = 'Bahama', Address_for_Email_Invoices__c = 'laarl@test.com');
        accounts.add(cc);
        Account si = new Account(Name = 'Site Account', RecordTypeId = '012D0000000NK5f', ShippingStreet = 'testikatu 4', ShippingPostalCode = '003', ShippingCity = 'testcity', ShippingCountry = 'Marokko');
        accounts.add(si);
        
        insert accounts;
        
        Account c1 = new Account(subject = 'Blaah', Type = 'Wiuh', RecordTypeId = '012200000005LN1');
        insert c1;
        
        Case c2 = new Case (Id = '2', Fault_classification__c = 'MAJOR',Setup_Name__c ='tessti', CaseNumber = '3', Related_Setup__c = 'jkkj', Status_Explanation__c = 'kdgjsfi' );
        insert c2;
        
        SetupFieldIdSettings__c fieldIdSettings = SetupFieldIdSettings__c.getInstance('SetupPageFieldIds');
        if(fieldIdSettings == null || fieldIdSettings.Id == null){
            fieldIdSettings = new SetupFieldIdSettings__c(Name = 'SetupPageFieldIds');
            fieldIdSettings.accounts_Field_Id__c = 'accountsId';
            fieldIdSettings.Cost_Center_Field_Id__c = 'ccId';
            fieldIdSettings.Site_Field_Id__c = 'siteId';
            fieldIdSettings.Setup_Object_Id__c = 'objectId';
            insert fieldIdSettings;
        }
        
        string currentPageUrl = '/apex/NewSetupPage?id='+c1.Id;
        PageReference pageRef = new PageReference(currentPageUrl );
        Test.setCurrentPage(pageRef);
        
        NewSetupFromCaseController controller = new NewSetupFromCaseController();
        PageReference newPage = controller.autoRun();
        System.AssertNotEquals(null, newPage);

        
    }
    
}

Best Answer chosen by Admin (Salesforce Developers) 
georggeorg

HI,

 

please check in your instance that subject field is there under account custom fields and then make the field name as subject__c  in the code..

 

**Note: All the custom fileds should be populated with __c if you want to use that field in code/validation rule etc..

 

Thanks,

George

All Answers

Puja_mfsiPuja_mfsi

Hi,

In salesforce there is no standard field name "Subject" in account object. 

Account c1 = new Account(subject = 'Blaah', Type = 'Wiuh', RecordTypeId = '012200000005LN1');
insert c1;

 

And if this is a custom field then please use the __c behind the field Name. And in account the "Name" field is mandatory.

And never hardcoded any Id within test class as u have use for recordTypeId.

 

 

Please let me know if u have any problem on same and if this post helps u plz give KUDOS by click on star at left.

georggeorg

HI,

 

please check in your instance that subject field is there under account custom fields and then make the field name as subject__c  in the code..

 

**Note: All the custom fileds should be populated with __c if you want to use that field in code/validation rule etc..

 

Thanks,

George

This was selected as the best answer
LaaralLaaral

Thanks for the answer, but I had to change tha code a bit and tried to run it in IDE and it gave me this error log :

 

10:39:44.900 (54900103000)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Record Type ID: this ID value isn't valid for the user: 012D0000000NK6eIAG: [RecordTypeId]

 

Here is the code :

 

@isTest
private class ImplementServiceAvailabilityTest {

    static testMethod void ImplementServiceAvailabilityTest() {
        // TO DO: implement unit test
        List<Account> accounts = new List<Account>();
        
        Account ma = new Account(Name = 'Main Account', RecordTypeId = '012D0000000NK6e');
        accounts.add(ma);
        Account cc = new Account(Name = 'Cost Account', RecordTypeId = '012D0000000NK4p', BillingStreet = 'testikatu 7', BillingPostalCode = '007', BillingCity = 'testcity', BillingCountry = 'Bahama', Address_for_Email_Invoices__c = 'laarl@test.com');
        accounts.add(cc);
        Account si = new Account(Name = 'Site Account', RecordTypeId = '012D0000000NK5f', ShippingStreet = 'testikatu 4', ShippingPostalCode = '003', ShippingCity = 'testcity', ShippingCountry = 'Marokko');
        accounts.add(si);
        
        insert accounts;
     
        Case c2 = new Case (Id = '2', Fault_classification__c = 'NORMAL',Setup_Name__c ='tessti', Related_Setup__c = 'null', Status_Explanation__c = 'kdgjsfi' );
        insert c2;
           
    }
    
}

 

What means that it's not valid for User? How can I fix this. Thanks a lot already for answering :)