Your support team creates Cases all day. Some are routine questions. Some are ticking time bombs from customers about to file BBB complaints, post negative reviews, or cancel contracts. The problem? They all look the same in the queue.
This tutorial shows you how to add automatic risk scoring to every new Salesforce Case using the RiskDetect API. When a Case is created, an Apex trigger sends the description to our AI analysis endpoint and stores the result as a Risk Assessment record linked to the Case.
How It Works
The trigger uses @future(callout=true) so it runs asynchronously. Case creation isn't slowed down at all. The risk score appears on the record within a few seconds.
What You'll Build
- Risk_Assessment__c - Custom object that stores scores, risk levels, detected signals, and AI summaries
- RiskRadarService - Apex class that calls the RiskDetect API and saves results
- CaseRiskTrigger - Trigger that auto-analyzes every new Case
- riskScoreBadge - Lightning Web Component that displays the score on record pages
Skip the build?
If you want all of this pre-built and ready to install, the RiskDetect Salesforce package includes everything in this tutorial plus email triggers, task triggers, an account-level dashboard, and a permission set. Install in under 10 minutes.
Step 1: Create the Custom Object
First, create a custom object to store risk assessment results.
Setup > Object Manager > Create > Custom Object
Label: Risk Assessment
Plural Label: Risk Assessments
Object Name: Risk_Assessment
Record Name: Risk Assessment Name (Auto Number: RA-{0000})
Fields:
Risk_Score__c Number(3, 0) - Score from 0-100
Risk_Level__c Picklist - low, medium, high, critical
Signals__c Long Text Area - Detected risk signals
Summary__c Long Text Area - AI-generated summary
Source_Type__c Picklist - case, email, call, task
Source_Text__c Long Text Area - Original analyzed text
Related_Case__c Lookup(Case) - Link back to source Case
Related_Account__c Lookup(Account) - Link to Account
Step 2: Create the Service Class
This Apex class handles the API call and stores results.
RiskRadarService.cls
public class RiskRadarService {
private static final String API_URL = 'https://riskdetect.app/api/analyze';
@future(callout=true)
public static void analyzeInteraction(
String text, String sourceType,
Id relatedCaseId, Id relatedAccountId
) {
HttpRequest req = new HttpRequest();
req.setEndpoint(API_URL);
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
Map<String, Object> body = new Map<String, Object>{
'text' => text,
'type' => sourceType
};
req.setBody(JSON.serialize(body));
Http http = new Http();
HttpResponse res = http.send(req);
if (res.getStatusCode() == 200) {
Map<String, Object> result =
(Map<String, Object>) JSON.deserializeUntyped(
res.getBody()
);
Risk_Assessment__c assessment = new Risk_Assessment__c(
Risk_Score__c = (Decimal) result.get('score'),
Risk_Level__c = (String) result.get('risk_level'),
Signals__c = JSON.serialize(result.get('signals')),
Summary__c = (String) result.get('summary'),
Source_Type__c = sourceType,
Source_Text__c = text.left(131072),
Related_Case__c = relatedCaseId,
Related_Account__c = relatedAccountId
);
insert assessment;
}
}
}
Why @future?
Salesforce doesn't allow HTTP callouts from trigger context directly. The @future(callout=true) annotation runs the method asynchronously, which means the API call happens in a separate transaction. The Case saves instantly. The risk score appears moments later.
Step 3: Create the Trigger
CaseRiskTrigger.trigger
trigger CaseRiskTrigger on Case (after insert) {
for (Case c : Trigger.new) {
String text = '';
if (c.Subject != null) text += c.Subject + '\n';
if (c.Description != null) text += c.Description;
if (String.isNotBlank(text)) {
RiskRadarService.analyzeInteraction(
text, 'case', c.Id, c.AccountId
);
}
}
}
That's it. Every new Case with a description now gets automatically scored.
Step 4: Add the Remote Site Setting
Salesforce requires you to whitelist external API endpoints before making callouts.
- Go to Setup > Remote Site Settings
- Click New Remote Site
- Set the name to
RiskDetect_API - Set the URL to
https://riskdetect.app - Check Active and click Save
Step 5: Test It
Create a new Case with this description:
This is unacceptable. I've called three times about this issue
and nobody can give me a straight answer. If this isn't resolved
by end of week, I'm filing a BBB complaint and leaving reviews
on every platform I can find.
Wait 5-10 seconds, then check the Risk Assessments related list on the Case. You should see:
- Score: 80-95 (critical range)
- Risk Level: Critical
- Signals: ultimatum language, BBB mention, repeated contact frustration, timeline pressure
- Summary: AI-generated description of the risk and recommended action
Now create a second Case with a normal question:
Hi, could you send me a copy of my latest invoice? Thanks!
This should score 5-15 with a "low" risk level. The system correctly differentiates between routine requests and escalation risks.
Going Further
Add email and call transcript triggers
The same pattern works for EmailMessage and Task objects. When an inbound email arrives or a call log is saved, fire the same analyzeInteraction method. The full RiskDetect package includes these triggers pre-built.
Build an account-level dashboard
Create a Lightning Web Component that queries all Risk Assessments for an Account, shows aggregate stats, and highlights the highest-risk interactions. This gives CSMs a single view of account health based on real communication signals.
Set up Slack alerts for critical scores
Add a check in the service class: if the score is above 70, use the RiskDetect Alert API to send a Slack notification to your CS channel. Critical risks get surfaced in seconds, not the next morning.
Want all of this pre-built?
The RiskDetect Salesforce package includes triggers for Cases, Emails, and Tasks, plus an account dashboard, score badges, and a permission set. Install in under 10 minutes.
View Install GuideKey Takeaways
- A single Apex trigger + service class adds AI risk scoring to every Case
@future(callout=true)keeps the trigger async so Case creation stays fast- The API returns a 0-100 score, risk level, specific signals, and an action summary
- Extend the pattern to EmailMessage and Task objects for full coverage
- High scores can trigger Slack alerts for immediate intervention