+ Start a Discussion
Sachin KumarSachin Kumar 

System.AuraHandledException: Script-thrown exception

Hi All,

I am new to Salesforce. I am getting below exception on my test class.
Can you please suggest me here ?

@isTest
    static void test_releaseInitiative()
    {
        init();

        Test.startTest();

        String errorRelease = ViewInitiativesController.releaseInitiative(testInitiative.Id);

        Initiative__c testInitiativeCheck = [SELECT Status__c, Initiative_Released__c FROM Initiative__c WHERE Id = :testInitiative.Id];
        System.assertEquals(true, testInitiativeCheck.Initiative_Released__c, 'Wrong Initiative_Released__c: ' + errorRelease);

        Test.stopTest();
    }

Error is -- System.AuraHandledException: Script-thrown exception

Any help is really appriacted. 
Thanks in advance.

 
Best Answer chosen by Sachin Kumar
Maharajan CMaharajan C
Hi Sachin,

In your apex class which method you have used the throw new AuraHandledException(ex.getMessage());    ? 

So for that method calling in your test class use the try catch block like below that will clear this error.

For Example : in your ViewInitiativesController apex class if your are using the throw new AuraHandledException(ex.getMessage()); at releaseInitiative method then in the test class use like below:

@isTest
    static void test_releaseInitiative()
    {
        init();
             
// Am not seeing any test data insertion in your method if needed do that.
 Initiative__c testInitiative = new Initiative__c(Status__c = ' enter you picklist data' , Initiative_Released__c  = ' Enter your  proper value');
insert testInitiative;

        Test.startTest();
       try
      {
        String errorRelease = ViewInitiativesController.releaseInitiative(testInitiative.Id);
      }
      catch(exception e)
         {
}   
 
        Test.stopTest();
    }

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

All Answers

Sachin KumarSachin Kumar
This test class is also having same error-- System.AuraHandledException: Script-thrown exception

    @isTest
    static void test_getFormAndUpsertInitiative()
    {
        init();

        Test.startTest();
        ViewInitiativesController.FieldSetForm fsForm = ViewInitiativesController.getForm(testInitiative.Id, 'MI_', false);
        
        System.assert(!fsForm.fsNamesToFields.isEmpty(), 'Fields were not retrieved');
        
        ViewInitiativesController.fetchUserInfo();

        testInitiative.Overall_status__c = 'Creative Services';
        ViewInitiativesController.upsertInitiative(testInitiative);
        testInitiative = [SELECT Overall_status__c FROM Initiative__c WHERE Id = :testInitiative.Id];
        System.assertEquals('Creative Services', testInitiative.Overall_status__c, 'Overall Status was not upated: ' + testInitiative.Overall_status__c);

        Test.stopTest();

        //System.assertEquals('Creative Services', initiativeCtrl.initiativeHistories[0].NewValue, 'Field tracking history was not retrived');         
    }
Maharajan CMaharajan C
Hi Sachin,

In your apex class which method you have used the throw new AuraHandledException(ex.getMessage());    ? 

So for that method calling in your test class use the try catch block like below that will clear this error.

For Example : in your ViewInitiativesController apex class if your are using the throw new AuraHandledException(ex.getMessage()); at releaseInitiative method then in the test class use like below:

@isTest
    static void test_releaseInitiative()
    {
        init();
             
// Am not seeing any test data insertion in your method if needed do that.
 Initiative__c testInitiative = new Initiative__c(Status__c = ' enter you picklist data' , Initiative_Released__c  = ' Enter your  proper value');
insert testInitiative;

        Test.startTest();
       try
      {
        String errorRelease = ViewInitiativesController.releaseInitiative(testInitiative.Id);
      }
      catch(exception e)
         {
}   
 
        Test.stopTest();
    }

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
This was selected as the best answer
Sachin KumarSachin Kumar
Hi Maharajan,

It worked as per your suggestion.

Thanks a lot.

With Best Regards,
Sachin