function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lxchlxch 

Google API via webhook

Hi all, I'm working on Google Chat API. Using this example. Successfully posted into my Google Chat Room (via webhook) but failed to show the card. 
https://github.com/muenzpraeger/salesforce-google-hangouts-chat-apex-webhook
Anyone knows what I'm missing?

User-added image

But result is this...

User-added image
global class GoogleChatWebhook {
    
    global class GoogleChatData {
        @InvocableVariable
        public String webhookUrl;
        @InvocableVariable
        public Id accountId;
        @InvocableVariable
        public String accountName;
        @InvocableVariable
        public String accountNo;
    }
    
    @InvocableMethod
    public static void sendChat(List<GoogleChatData> chatData) {
        for (Integer i=0;i<chatData.size();i++) {
            sendOutputMessageToRoom(chatData.get(i).webhookUrl,
                                    chatData.get(i).accountId, 
                                    chatData.get(i).accountName,
                                    chatData.get(i).accountNo);
        }
    }
    
    @future(callout=true)
    public static void sendOutputMessageToRoom(String webhookUrl,
                                               String accountId,
                                               String accountName,
                                               String accountNo) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint(webhookUrl);
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json;charset=UTF-8');
        
        String url = System.Url.getSalesforceBaseUrl().toExternalForm();
        String accountUrl = url + '/lightning/r/Account/' + accountId + '/view'; // Using the new URL format!
        
        String message = '{"cards":[{"header":{"title":"Salesforce Account alert","subtitle":"via Webhooks"},"sections":[{"widgets":[{"keyValue":{"topLabel":"Account No.","content":"' + accountNo + '"}},{"keyValue":{"topLabel":"Account Name","content":"' + accountName + '"}}]},{"widgets":[{"buttons":[{"textButton":{"text":"Open Account record","onClick":{"openLink":{"url":"' + accountUrl + '"}}}}]}]}]}]}';
        
        req.setBody(message);
        system.debug(message);

        Http http = new Http();
        HttpResponse res = http.send(req);
        system.debug(res);
    }
    
}
Best Answer chosen by lxch
lxchlxch
Account ID is not given on the time when it's sent via webhook. This breaks the cards. I manually put the AccountNo 1111 as placeholder, then the card is properly made. This is, I guess, the AccountNumber is only given when the record is saved, but the number is not ready when it's sent via webhook.

User-added image