1

I created a test platform event with checkbox, added my custom object to Change Data Capture and did this:

empAPI.cmp:

<aura:component implements="flexipage:availableForAllPageTypes" access="global" >
        <!-- A part of my page -->
        <lightning:empApi aura:id="empApi"/>
        <aura:handler name="init" value="{!this}" action="{!c.onInit}" />
        <aura:attribute name="subscription" type="Map"/>

        <lightning:input aura:id="channel" label="channel" name="channel" type="text" value="/event/TestEvent__e"/>
        <lightning:button label="Subscribe" onclick="{!c.subscribe}" variant="brand"/>
        <lightning:button label="Unubscribe" onclick="{!c.unsubscribe}" disabled="{!empty(v.subscription)}" variant = "destructive"/>
    </aura:component>

empAPIController.js:

({
        onInit : function(component, event, helper) {
            const empApi = component.find('empApi');
            empApi.setDebugFlag(true);
            empApi.onError($A.getCallback(error => {
                console.error('EMP API error: ', error);
            }));
        },

        subscribe : function(component, event, helper) {
            const empApi = component.find('empApi');
            const channel = component.find('channel').get('v.value');
            const replayId = -1;
            empApi.subscribe(channel, replayId, $A.getCallback(eventRecieved => {
                console.log('Recieved event ', JSON.stringify(eventRecieved));
            })).then(subscription => {
                console.log('Subscription to channel ', subscription.channel);
                component.set('v.subscription', subscription);
            });     
        },

        unsubscribe : function(component, event, helper) {
            const empApi = component.find('empApi');
            const subscription = component.get('v.subscription');
            empApi.unsubscribe(subscription, $A.getCallback(unsubscribed => {
                console.log('Unsubscribed from channel ' + unsubscribed.subscription);
                component.set('v.subscription', null);
            }))
        },
    })

In Execute Anonimous Window I have:

    TestEvent__e testEvent = new TestEvent__e();
    testEvent.Modified__c = true;  //My checkbox
    EventBus.publish(testEvent);

It works: I see it in my console.

Now. I have Callout:

public static void createTasks(){
        List<CustomTask__c> newCardsList = new List<CustomTask__c>();

        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('MyApiCall');
        request.setMethod('GET'); 
        HttpResponse response = http.send(request);
        Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());

        List<Object> receivedCards = (List<Object>) results.get('cards');

        for(Object eachCard : receivedCards){
            String cardInText = String.valueOf(eachCard);
            String name = cardInText.substringBetween('), name=', '}');
            String description = cardInText.substringBetween('desc=', ',');

            CustomTask__c newCard = new CustomTask__c(
                Name__c = name,
                Description__c = description,
            );
            newCardsList.add(newCard);
        } 
        try {
            upsert newCardsList CardId__c;
        } catch (DmlException e) {
            System.debug(e.getMessage());
        }
    }

If I reload my Lightning page, a record of my custom object will upsert. But I need it without page reloading. How can I connect the functionality of Callout and lightning:empApi? What should I do?

2

As I can see from your code

<lightning:input aura:id="channel" label="channel" name="channel" type="text" value="/event/TestEvent__e"/>

You are subscribing to the wrong event. CDC Events are streamed on /data/<Custom_Object_Name>__ChangeEvent

Thus in Your case as you are upserting CustomTask__c you code will be

<lightning:input aura:id="channel" label="channel" name="channel" type="text" value="/data/CustomTask__ChangeEvent"/>

Src: https://developer.salesforce.com/docs/atlas.en-us.change_data_capture.meta/change_data_capture/cdc_subscribe_channels.htm

  • The problem is, I still get my new record only after page reloading. – MichaelLev19 Jul 2 '19 at 6:54
  • I think I know where the problem is. I still try to run event with TestEvent__e testEvent = new TestEvent__e();. I shouldn't, right? If we use /data/<Custom_Object_Name>__ChangeEvent. But then how to provoke its launch? I use API of a third-party resource. – MichaelLev19 Jul 2 '19 at 7:26
  • And is it even possible to set up empApi for streaming integration with third party service? Maybe I need some another tool... – MichaelLev19 Jul 2 '19 at 8:33
  • @MichaelLev19 you wanna capture CDC event or custom TestEvent__e event? – Pranay Jaiswal Jul 2 '19 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.