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
Deja BondDeja Bond 

System list out of bounds when accessing user

Hi all, I have been trying to figure this out for a while. I have this test and I keep getting that my list is out of bounds. I cannot tell what I am doing wrong.
Here is my class
@auraEnabled
    public static void saveValues(String lvFields, String dvSettings, String adSettings){

        List<Matrix_User_Setting__c> userSettings = MTX_MatrixAccessor.getSettingsForCurrentUser(UserInfo.getUserId());

        userSettings[0].Additional_Detail_Settings_Selected__c = adSettings;
        userSettings[0].Detail_View_Settings_Selected__c = dvSettings;
        userSettings[0].List_View_Fields_Selected__c = lvFields;

        update userSettings;
    }
Here is my test class
@isTest
    public static void savingSettings(){

        Test.startTest();


        List<Matrix_User_Setting__c> matrixuserSettings = [SELECT Id, Additional_Detail_Settings_Selected__c, Detail_View_Settings_Selected__c, List_View_Fields_Selected__c, Current_Matrix__c FROM Matrix_User_Setting__c];
        System.debug(matrixuserSettings);
        //add to the first user's setting
        matrixuserSettings[0].Additional_Detail_Settings_Selected__c = 'Highlight';
        matrixuserSettings[0].Detail_View_Settings_Selected__c = 'City';
        matrixuserSettings[0].List_View_Fields_Selected__c = 'Current Employer';
        update matrixuserSettings[0];

        System.assert('Highlight'== matrixuserSettings[0].Additional_Detail_Settings_Selected__c);

        Test.stopTest();
    }
Can someone help?
Raj VakatiRaj Vakati
You need insert the Matrix_User_Setting__c like below 

 
Matrix_User_Setting__c setting = new Matrix_User_Setting__c();
setting.Name = 'Test Setting';
setting.SetupOwnerId=UserInfo.getUserId() ; 
setting.Value__c = 'Whatever';
insert setting;

here is the code change class name
 
@isTest
    public static void savingSettings(){

        Test.startTest();


Matrix_User_Setting__c setting = new Matrix_User_Setting__c();
setting.Name = 'Test Setting';
setting.SetupOwnerId=UserInfo.getUserId() ; 
setting.Value__c = 'Whatever';
insert setting;






        List<Matrix_User_Setting__c> matrixuserSettings = [SELECT Id, Additional_Detail_Settings_Selected__c, Detail_View_Settings_Selected__c, List_View_Fields_Selected__c, Current_Matrix__c FROM Matrix_User_Setting__c];
        System.debug(matrixuserSettings);
        //add to the first user's setting
        matrixuserSettings[0].Additional_Detail_Settings_Selected__c = 'Highlight';
        matrixuserSettings[0].Detail_View_Settings_Selected__c = 'City';
        matrixuserSettings[0].List_View_Fields_Selected__c = 'Current Employer';
        update matrixuserSettings[0];


YOURCLASSNAE.saveValues('Highlight','City','Current Employer');

        Test.stopTest();
    }

 
Deja BondDeja Bond
I tried your above example, but I  run into an  error that wont allow me to update

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FO_UserTrigger: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object