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
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
Srinjana ShandilyaSrinjana Shandilya
Hi, this my test class below and i am getting the error : System.AuraHandledException: Script-thrown exception

@istest
public class SanctionIntimationDealerLetter_sls_Test {
    
    @isTest static void SanctionLetterforDealer_sls(){
        set<Id> setIdLead = new set<Id>();
        Id leadRecTypeId = Schema.SObjectType.Lead__c.getRecordTypeInfosByName().get('Individual').getRecordTypeId();
        Lead__c lead = new Lead__c(Constitution__c  ='Individual', Subconstitution__c ='Individual',Stage__c='Credit Appraisal', FirstName__c='TestFirstName',
                                   LastName__c='TestLastName', MiddleName__c ='TestMiddle', SuiteCRMId__c='Test12311',IsKYCDocumentsCollected__c =false,
                                   recordtypeId = leadRecTypeId, QueryStatus__c = 'Raised',QueryRaisedDateTime__c=system.now()-20,
                                   FTNRNonFTNRCase__c = 'FTNR',CustomerType__c='Prime');
        Insert lead;
        setIdLead.add(lead.Id);
        Id dealerRecTypeId = Schema.Sobjecttype.Account.getRecordTypeInfosByName().get('Dealer').getRecordTypeId();
        Account dealer = New Account(Name='Test Dealer', RecordTypeId=dealerRecTypeId,LeadSourcedTillDate__c = 5,OwnerId=UserInfo.getuserid());
        Insert dealer;
        System.assertEquals(dealer.Name, 'Test Dealer');
        
        AssetDetail__c ast = new AssetDetail__c(Exshowroomprice__c = 50000, Loan_Details__c = lead.Id,TypeOfAsset__c='NEW',Usage__c='Personal',
                                                Dealer__c=Dealer.Id);
        Insert ast;
        
        Loan__c loanRecord = new Loan__c();
        loanRecord.LoanAmount__c = 5000;
        loanRecord.AssetDetail__c = ast.Id;
        loanRecord.Lead__c = lead.Id;
        loanRecord.EMIAmount__c=500;
        loanRecord.EMIPattern__c='Monthly';
        loanRecord.EMIStartDate__c=Date.newInstance(2021, 05, 20);
        loanRecord.EMIEndDate__c=Date.newInstance(2025, 10, 21);
        loanRecord.RequestIdLMS__c='100000011';
        loanRecord.Tenure__c = 12;
        loanRecord.PushedToLMS__c = FALSE;
        loanRecord.EMIType__c='Advance';
        loanRecord.RepaymentMode__c='PDC';
        
        insert loanRecord;
        
        
        ApexPages.currentPage().getParameters().put('id', lead.Id);
        SanctionIntimationDealerLetter_sls sancDeal = New SanctionIntimationDealerLetter_sls();
        sancDeal.getilead();
        sancDeal.getlstast();
        SanctionIntimationDealerLetter_sls.createPdf(setIdLead);
        lead.Stage__c='Hands Off';
        update lead;
    }
}

Can anybody please help me to find the solution?
 
Koppalli BhuvaneswariKoppalli Bhuvaneswari
Hi, this my test class below and i am getting the error : System.AuraHandledException: Script-thrown exception
@isTest
private class BookMeetingForExistingContact_CC_Test {

    @isTest
    static void testIsThisLinkUsed() {
        // Create test data
        
        URL__c urlObj = new URL__c(
            Encrypted_Id__c = 'test-encrypted-id==',
            Decrypted_Id__c = 'test-decrypted-id',
            Key__c = 'test-key',
            ExpirationDurationInMinutes__c = 60,
            is_Invitation_Accpted__c = false
        );
        insert urlObj;
                 
        // Call the method being tested
        Boolean result = BookMeetingForExistingContactController.isThisLinkUsed('test-encrypted-id');
        
        // Verify the result
        System.assertEquals(false, result);
    }

    @isTest
    static void testDecryptHostUserData() {
        // Create test data
        /*User host = new User(
            FirstName = 'Test',
            LastName = 'User',
            Email = 'testuser@test.com'
        );
        insert host;*/
          User host = [SELECT FirstName, LastName,Email from User Where Id=:UserInfo.getUserId()];
        URL__c urlObj = new URL__c(
            
            Encrypted_Id__c = 'test-encrypted-id==',
            Decrypted_Id__c = 'test-decrypted-id',
            Key__c = EncodingUtil.base64Encode(Blob.valueOf('test-key')),
            ParticipantAsUser__c = host.Id,ExpirationDurationInMinutes__c=30
        );
        insert urlObj;
        
        // Call the method being tested
        User result = BookMeetingForExistingContactController.decryptHostUserData('test-decrypted-id==');
        
        // Verify the result
        System.assertEquals(host.Id, result.Id);
        System.assertEquals(host.Email, result.Email);
    }

    @isTest
    static void testDecryptParticipantsUserData() {
        // Create test data
        Account account = new Account(
            Name = 'Test Account'
        );
        insert account;

        Contact contact = new Contact(
            FirstName = 'Test',
            LastName = 'Contact',
            Email = 'testcontact@test.com',
            AccountId = account.Id
        );
        insert contact;

        URL__c urlObj = new URL__c(
            
            Encrypted_Id__c = 'test-encrypted-id==',
            Decrypted_Id__c = 'test-decrypted-id',
            Key__c = EncodingUtil.base64Encode(Blob.valueOf('test-key')),
            ParticipantAsContact__c = contact.Id,ExpirationDurationInMinutes__c=30
        );
        insert urlObj;
        
        // Call the method being tested
        Map<String, String> result = BookMeetingForExistingContactController.decryptParticipantsUserData('test-encrypted-id==');
        
        // Verify the result
        System.assertEquals(contact.Id, result.get('Id'));
        System.assertEquals(contact.Email, result.get('Email'));
        System.assertEquals(contact.Account.Name, result.get('Company'));
    }

    @isTest
    static void testCreateNewMeetingForExistingUser() {
        // Create test data
        /*User host = new User(
           FirstName = 'Test',
            LastName = 'User',
             Email = 'testuser@test.com'
        );
         insert host;*/
        User host = [SELECT FirstName, LastName,Email from User Where Id=:UserInfo.getUserId()];

        Account account = new Account(
            Name = 'Test Account'
        );
        insert account;

        Contact contact = new Contact(
            FirstName = 'Test',
            LastName = 'Contact',
            Email = 'testcontact@test.com',
            AccountId = account.Id
        );
        insert contact;
    }
}