Skip to content

Instantly share code, notes, and snippets.

@shrutis22
Created September 5, 2017 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shrutis22/5a5c506d1b5c5e34b4ccd1096b1f55f8 to your computer and use it in GitHub Desktop.
Save shrutis22/5a5c506d1b5c5e34b4ccd1096b1f55f8 to your computer and use it in GitHub Desktop.
This class is created to assign the cases according to the context of the case issue. The context is found out with the help of Intent APIs.
/**
* This class is created to assign the
* cases according to the context of
* the case issue. The context is
* found out with the help of Intent
* APIs.
*
* @author Shruti Sridharan
* @since 05.09.2017
* @revisions N/A
**/
public class IntelligentCaseRouter {
/**
* This method is created to update
* the Case Owner of the recently
* created Case with respect to the
* type of case.
*
* @param caseIds Id of the Case whose Case Owner needs to be
* updated according to the type of the Case
**/
@future( callout = TRUE )
public static void assignOwners( Set<Id> caseIds ) {
List<Case> cases = [
SELECT Description
FROM Case
WHERE Id IN :caseIds
];
Einstein_API_Settings__c settings = Einstein_API_Settings__c.getInstance( UserInfo.getOrganizationId() );
User technicalUser = [
SELECT Name
FROM User
WHERE Name = 'John Doe'
];
User productUser = [
SELECT Name
FROM User
WHERE Name = 'Shruti Sridharan'
];
List<Case> casesToUpdate = new List<Case>();
EinsteinAPI api = new EinsteinAPI();
for( Case cse : cases ) {
PredictionResponse predictionResp = api.predictIntent( settings.Intent_Model_Id__c, cse.Description );
if( predictionResp != NULL ) {
Double largest = 0;
Integer position = 0;
for( PredictionResponse.Probabilities prob : predictionResp.Probabilities ) {
for( Integer i = 0; i < predictionResp.probabilities.size(); i++ ) {
if( largest <= predictionResp.probabilities[i].probability ) {
largest = predictionResp.probabilities[i].probability;
position = i;
}
}
String issueLabel = predictionResp.probabilities[position].label;
if( issueLabel == 'Technical Issue' ) {
cse.OwnerId = technicalUser.Id;
}
else if( issueLabel == 'Product Issue' ) {
cse.OwnerId = productUser.Id;
}
}
}
casesToUpdate.add( cse );
}
UPDATE casesToUpdate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment