1

I'm very new to Salesforce development & I'm trying to implement Einstein Sentiment Analysis in a project where it gives the sentiment based on a Contact's latest Case Subject & Description. I made a field Customer_Mood__c on the Case object to update the feedback.

I'm facing two problems:

  1. The Sentiment Analysis works fine for Happy Emotion. But for anything other than Happy it shows Neutral. Even for the subject words like "Sad","Bad" or "Worst" shows Neutral.

  2. I'm trying to pass both Subject and Description of a Case for the Analysis but it only runs for the Subject and then shows the error "You have uncommitted work pending. Please commit or rollback before calling out"

This is the debug log:

08:56:54:002 USER_DEBUG [11]|DEBUG|CaseId: ------> 5000900000DcBRSAA3
08:56:54:012 USER_DEBUG [18]|DEBUG|subjText: ------> Worst
08:56:54:012 USER_DEBUG [19]|DEBUG|descText: ------> Worst experience
08:56:54:013 USER_DEBUG [40]|DEBUG|body: ------> {"modelId":"CommunitySentiment","document":"Worst"}  

08:56:54:143 USER_DEBUG [45]|DEBUG|req: ------> System.HttpRequest[Endpoint=https://api.einstein.ai/v2/language/sentiment, Method=POST]

08:56:54:146 USER_DEBUG [50]|DEBUG|resp: ------> SentimentResponse:[probabilities=(Probabilities:[label=neutral, probability=0.9604924], Probabilities:[label=positive, probability=0.026220037], Probabilities:[label=negative, probability=0.013287586])]

08:56:54:147 USER_DEBUG [51]|DEBUG|resp.Probabilities: ------> (Probabilities:[label=neutral, probability=0.9604924], Probabilities:[label=positive, probability=0.026220037], Probabilities:[label=negative, probability=0.013287586])

08:56:54:150 USER_DEBUG [64]|DEBUG|labelWithProbablity: ------> (Probabilities:[label=neutral, probability=0.9604924], Probabilities:[label=positive, probability=0.026220037], Probabilities:[label=negative, probability=0.013287586])

08:56:54:151 USER_DEBUG [65]|DEBUG|mapLabelProbablity: ------> {0.013287586=negative, 0.026220037=positive, 0.9604924=neutral}

08:56:54:206 USER_DEBUG [74]|DEBUG|updateCASE: ------> Case:{Id=5000900000DcBRSAA3, Customer_Mood__c=neutral}

08:56:54:207 USER_DEBUG [40]|DEBUG|body: ------> {"modelId":"CommunitySentiment","document":"Worst experience"}

This is the fetchSentiments class:

public class fetchSentiments {
    public static String subjText = '';
    public static String descText = '';
    public static String updateCaseId;
    public static string sentimentModelId = 'CommunitySentiment';



@AuraEnabled
public static string getCaseStatus(Id CaseId){

    system.debug('CaseId: ------> ' + CaseId); 

    for(Case selectedCase : [SELECT Id, Subject, Description FROM Case WHERE Id =: CaseId]){
       
        subjText = selectedCase.Subject;
        descText = selectedCase.Description;
       
        system.debug('subjText: ------> ' + subjText);
        system.debug('descText: ------> ' + descText);
      
        updateCaseId = selectedCase.Id;
        updateCase(subjText);
        updateCase(descText);
    
    }
    return 'Success';
}

public static void updateCase(String text){
    Http http = new Http();
    HttpRequest req = new HttpRequest();
    req.setMethod( 'POST' );
    req.setEndpoint( 'https://api.einstein.ai/v2/language/sentiment' );
    req.setHeader( 'Authorization', 'Bearer ' + 'A Valid Token' );
    req.setHeader( 'Content-type', 'application/json' );
    
    String body = '{\"modelId\":\"'+ sentimentModelId + '\",\"document\":\"' + text + '\"}';
    req.setBody( body );
    
    system.debug('body: ------> ' + body);


    HTTPResponse res = http.send( req );
    
    system.debug('req: ------> ' + req);

    SentimentResponse resp = ( SentimentResponse ) JSON.deserialize( res.getBody(), SentimentResponse.class );

    
    system.debug('resp: ------> ' + resp);
    system.debug('resp.Probabilities: ------> ' + resp.Probabilities);

    Map<Double, String> mapLabelProbablity = new Map<Double, String>();
    List<SentimentResponse.Probabilities> labelWithProbablity = new List<SentimentResponse.Probabilities>();

    for(SentimentResponse.Probabilities selected : resp.Probabilities){

        SentimentResponse.Probabilities newSelected = new SentimentResponse.Probabilities();
        newSelected.label = selected.label;
        newSelected.probability = selected.probability;
        mapLabelProbablity.put(newSelected.probability, newSelected.label);
        labelWithProbablity.add(newSelected);
    }
    system.debug('labelWithProbablity: ------> ' + labelWithProbablity);
    system.debug('mapLabelProbablity: ------> ' + mapLabelProbablity);


    if(labelWithProbablity.size() > 0){
        Case updateCASE = new Case();
        updateCASE.Id = updateCaseId;
        updateCASE.Customer_Mood__c = mapLabelProbablity.get(labelWithProbablity[0].probability);
        update updateCASE;

        system.debug('updateCASE: ------> ' + updateCASE);
      
    }          
    }
}

My Component Any help would be highly appreciated! Thanks.

1 Answer 1

4

The problem here is that the text you are using for negative assertion is not descriptive enough for the model (CommunitySentiment) to identify as negative. I'd argue it is not descriptive enough even for a human being to classify as 'negative'. Try testing it with REAL negative reviews from your community (if you already have any) or from elsewhere in the www. Here's a negative review I pulled from amazon:

'these are junk. i bought them for gym but not even good enough to work in the yard. did not make it through the 1st wash without unraveling. not even good enough for rags because they are not cotton. do NOT waste your money.'

On a final note, I would append your case subject and description and use that as an input to your sentiment method for better results.

String wholeFeedback = selectedCase.Subject + '. ' + selectedCase.Description;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .