Quantcast
Channel: Active questions tagged chatter-feed - Salesforce Stack Exchange
Viewing all articles
Browse latest Browse all 191

Chatter post not getting saved when calling postToChatter() method from exception catch and throwing AuraHandledException

$
0
0

this is my class and i am calling this method from LWC button

The code is working fine when there is no exception.But in the exception and else if() case while i am trying to throw AuraHandledException then the chatter post is not visible on the feed.I checked it with debug all the variables are having values and the execution is going to the last line of the postToChatter() method but still the post is not visble.

In the debug logs i am just getting this error - 'FATAL_ERROR System.AuraHandledException: Script-thrown exception'

Please let me what I am doing wrong here

public with sharing class valid{

@AuraEnabled//calling this method from lwc buttonpublic static void validate(String accNumber,Id targetId,Id userMentionId){

try {System.debug('Inside try apex invoice method');String url =''

        Http http = new Http();        HttpRequest request = new HttpRequest();        request.setEndpoint(url);        request.setMethod('GET');        request.setTimeout(120000);        request.setHeader('Content-Type', 'application/json');        request.setHeader('client_id', CLIENT_ID);        request.setHeader('client_secret', CLIENT_SECRET);        System.debug(request);        Case updateCase =new Case();        if (!Test.isRunningTest()) {                HttpResponse response = http.send(request);                if (response.getStatusCode() == 200) {                    List<customObj> pppp = (List<customObj>)JSON.deserialize(response.getBody(), List<customObj>.class);                    String shipToAcc=pppp[0].shipto;                    Account acct= [SELECT Id,Name,Account_Number__c from Account where Account_Number__c =:shipToAcc Limit 1];                    updateCase.Id=targetId;                    updateCase.Ship_To__c=acct.Id;                    update updateCase;                    System.debug('update done');                    //this chatter post is working                    postToChatter('\n validation successful. ',targetId,userMentionId);                }                else if (response.getStatusCode() != 200){                //this chatter post not working                    postToChatter(' validation unsuccessful.',targetId,userMentionId);//this postToChatter method is getting called properly but the post is not visible                     System.debug('Failed');                    throw new AuraHandledException('No Matching  found');                }        }    }     catch (Exception e) {     //this chatter post also not working        postToChatter(' validation unsuccessful',targetId,userMentionId);//this postToChatter method is getting called properly but the post is not visible         system.debug('exception happened');        throw new AuraHandledException(e.getMessage());    }}@AuraEnabledpublic static void postToChatter(String messageBody, Id targetId, Id userMentionId) {    system.debug('method called');    ConnectApi.FeedItemInput feedItemInput             = new ConnectApi.FeedItemInput();    ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();    ConnectApi.MessageBodyInput messageBodyInput       = new ConnectApi.MessageBodyInput();    ConnectApi.TextSegmentInput textSegmentInput       = new ConnectApi.TextSegmentInput();    messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();    mentionSegmentInput.id = userMentionId;    messageBodyInput.messageSegments.add(mentionSegmentInput);    textSegmentInput.text = messageBody;    messageBodyInput.messageSegments.add(textSegmentInput);    feedItemInput.body = messageBodyInput;    feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;    feedItemInput.subjectId = targetId;     ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(null, feedItemInput);    System.debug('posting chatter done');}private class invoiceWrapper{    String custpo;    String shipto;} }

Viewing all articles
Browse latest Browse all 191

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>