Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to resolve the error - System.UnexpectedException: Script-thrown exception?

Asked by David Edmunds May 15, 2023 5.5K views 11 answers
Share

About this question

I'm trying to create an Apex callout and running into the following error message: System.UnexpectedException: Script-thrown exception My analysis shows that System.UnexpectedException is not catchable in Apex. There are some reports of exceptions with “Script-thrown exception” message by other users, but the exception type in those cases happen to be System.CalloutException. [1] The only reported case of a System.UnexpectedException with the same message appears to be triggered by log level of Callouts. (Note: the recommended workaround did not solve the problem in our case.) [2]

This is the code that causes the error:

HttpRequest req = new HttpRequest(); req.setEndpoint('callout:AmazonConnectAPI/contact/start-recording'); req.setMethod('POST'); String body = String.format( ''{'"ContactId": "{0}","InitialContactId": "{1}","InstanceId": "{2}","VoiceRecordingConfiguration": '{'"VoiceRecordingTrack": "{3}"'}''}'', new List{ contactId, initialContactId, instanceId, voiceRecordingTrack } ); if (body != '') { req.setBody(body); } Http http = new Http(); HttpResponse resp = http.send(req);

Other notable facts:

  • The HTTP callout is made to a Named Credential called AmazonConnectAPI
  • AmazonConnectAPI is configured for "https://connect.us-east-1.amazonaws.com“
  • The authentication method of the Named Credential is Named Principal with protocol "AWS Signature Version 4
  • The authentication method uses the credentials for an IAM user with a full-access managed policy to Amazon Connect.
  • The formatting of the body is correct. There is no syntax error.
  • The error comes from this line HttpResponse resp = http.send(req);

Your answer

11 Answers

Aaron Dixon Latest answer

Answered on Apr 16, 2026

It's interesting you're hitting this UnexpectedException, especially with the AWS Signature Version 4 auth. I've seen similar issues when the AWS policy isn't quite right, even with full access. Maybe double-check that the IAM user has explicit permissions for the specific Connect API actions (start-recording in this case)? Also, sometimes small changes in the payload can cause issues, are you sure there is no unexpected characters? Even the slightest slope in the JSON formatting could be a culprit. Hope that helps!



Was this helpful?

Lukewendt

Answered on Feb 26, 2026

This Apex error is truly puzzling! It reminds me of trying to get a vintage Love Tester machine to work; sometimes the issue isn't obvious. Hope you find a solution soon, David.



Was this helpful?

Diegochi

Answered on Nov 23, 2025

It sounds like you're encountering a tricky issue with your Apex callout. The "System.UnexpectedException" can indeed be frustrating, especially when you’ve checked your formatting and permissions. One potential approach could be to double-check the Named Credential settings and ensure everything aligns with the API's requirements. If you're looking for a distraction while troubleshooting, why not take a break with a fun game like Solar Smash? It might help clear your mind!



Was this helpful?

Jackihudson

Answered on Oct 6, 2025

Okay, folks! Seeing "System.UnexpectedException: Script-Thrown Exception" can be frustrating. Often, it points to a null value or a faulty formula within your script. Check your data inputs and validation rules carefully. Could also be a permission issue, so double-check object-level security. This reminds me of solving quests on Poptropica- meticulous attention to detail always uncovers the answer. Good luck debugging!
Was this helpful?

Alexanderjohn

Answered on Oct 28, 2024

Thanks for sharing this! It's interesting how sometimes debugging feels like the iq test - solving puzzles to uncover the root cause. I'll definitely double-check the Named Credential name and the URL encoding!

Was this helpful?

Gerald Flynn

Answered on Jul 30, 2024

To resolve the System.UnexpectedException: Script-thrown exception error in your Apex callout, you should consider several potential issues and debugging steps. 

Ensure the JSON body of the request is correctly formatted. You are using String.format dinosaur game, which is correct, but sometimes the escaping of quotes can be tricky. Verify the final JSON structure:

String body = String.format(

    ''{'"ContactId": "{0}","InitialContactId": "{1}","InstanceId": "{2}","VoiceRecordingConfiguration": {'"VoiceRecordingTrack": "{3}"}}'}'',

    new List { contactId, initialContactId, instanceId, voiceRecordingTrack }

);

Was this helpful?

Whoopigoldberg

Answered on Jul 2, 2024

That is, as Ranjana said. Must carry out Locate the Code Object Look over the script -> Review Debugging Data and Logs ->Deal with Errors ->Run the script test ->Recurrence Monitoring -> gorilla tag



Was this helpful?

Elliot Zucker

Answered on Jun 6, 2024

As Ranjana mentioned. Need to perform Identify the Script-> Review the Script-> Check Logs and Debugging Information->Handle Exceptions->Test the Script->Monitor for Recurrence->

And finally wait for the results of fish eat fish

Was this helpful?

Ranjana Admin JanBask Expert

Answered on May 3, 2024

The error message "System.UnexpectedException: Script-Thrown Exception" typically indicates that an unexpected exception occurred in a script. To resolve this error, you'll need to identify the script causing the exception and then address the issue causing the exception. Here are some general steps you can take:


Identify the Script: Determine which script is causing the exception. This could be a script written in a programming language such as JavaScript, Python, or Apex, depending on the context in which you encountered the error.

Review the Script: Carefully review the code in the script to identify any potential issues such as syntax errors, logic errors, or unexpected behavior. Look for any lines of code that could be causing the exception.

Check Logs and Debugging Information: Look for any logs, error messages, or debugging information that can provide more context about the exception. This may include stack traces, error codes, or additional details about where the exception occurred.

Handle Exceptions: If the script is written in a language that supports exception handling (such as try-catch blocks in JavaScript or Python), consider adding exception handling code to catch and handle any potential errors gracefully. This can help prevent the script from crashing and provide more meaningful error messages to users.

Test the Script: Once you've identified and addressed the issue causing the exception, test the script to ensure that the error no longer occurs. Verify that the script behaves as expected and that it handles any potential errors or edge cases correctly.

Monitor for Recurrence: Keep an eye on your system to ensure that the error does not recur. Monitor logs and error reports for any signs of the exception reoccurring, and be prepared to investigate and address any new issues that arise.

By following these steps and thoroughly investigating the cause of the "System.UnexpectedException: Script-Thrown Exception" error, you should be able to resolve the issue and prevent it from affecting your system in the future.

Was this helpful?

Phillip Floyd

Answered on May 1, 2024

To add to "1. Named Credential Name", this exception may occur when a callout refers to named credentials that are part of a package with a namespace. Even if APEX code is included in the same package.


When this occurs, the callout works in a Development org with the same namespace as the package containing the named credentials, but it throws "System.UnexpectedException: Script-thrown exception" when deployed basketball stars to a different namespace. So you only need to add the package namespace to the callout.

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:__/some-path');
Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.