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
DannyTKDannyTK 

Receiving an Invalid field initializer error on Test Class

Good afternoon,

I'm trying to write a test class for a trigger where if there's an open Task on a record (custom object, Sales_Order__c), then the status of the record (Sales Order) = "Pending Question".  When the task is complete, then the status updates to "Ready for Review".  The trigger works fine, but I can't seem to get the test class to save:

@isTest
private class TriggerSalesOrderUpdateonTask {

static testMethod void validateTriggerSalesOrderUpdateonTask () {
List<Sales_Order__c> so = new List<Sales_Order__c> {new Sales_Order__c (
      so.name = '000001')};
        insert so;

List<task> t = new List<task> {new task(
        WhatID = Sales_Order__c[0].id,
        subject = 'test trigger',
        Status = 'Not Started')};
        insert t;

List<task> taskstoupdate = New List<task>{[select id from task where id in :t]};
    for (task tok:taskstoupdate)
    tok.Status = 'Completed';
   
    update taskstoupdate;
   


        system.assertEquals(so.Status__c, 'Sales Order Ready for Review');

    }
}


I'm receiving a Compile Error: Invalid field initializer:so.name at line 6 column 7.  Can someone take a look and see where i'm going wrong, and let me know if you need me to post my trigger on here as well.  Thanks community.
Best Answer chosen by DannyTK
Saurabh DhobleSaurabh Dhoble
List<Sales_Order__c> so = new List<Sales_Order__c> {new Sales_Order__c (Name = '000001')};
Replace Line 6 with the above and this will work. Pls mark as answer if it is, well, the answer :)

All Answers

Saurabh DhobleSaurabh Dhoble
List<Sales_Order__c> so = new List<Sales_Order__c> {new Sales_Order__c (Name = '000001')};
Replace Line 6 with the above and this will work. Pls mark as answer if it is, well, the answer :)
This was selected as the best answer
DannyTKDannyTK
Thanks Saurabh, 

so i made the following updates to bypass the various error messages:
1) updated line 6 to name='000001', had to change to Status__c = 'Sales Order Submitted' (Name isn't a writable field)
2) updated WhatID = Sales_Order__c[0].id to so[0].id

looks like now the "system.assertEquals(so.Status__c, 'Sales Order Ready for Review');" line is throwing a "Initial term of field expression must be a concrete SObject:List<Sales_Order__c> " error

should i now have used the List command, or is there a way to change the last line to be consistent to bypass this error....didn't realize that my test class was that far off...
Saurabh DhobleSaurabh Dhoble
Use so[0].status__c .... so is a list, remember?
DannyTKDannyTK
Thanks for your help Saurabh.