SOQL Query not returning results when running from RestResource

151 Views Asked by At

I have a webService (EventBriteWebhookListener.cls) for Eventbrite. Eventbrite sends a POST payload to the Resource containing the URI of any newly created / modified event.

EventBriteWebhookListener.cls then parses the payload, containing the eventbrite EventID, and calls on another class (ghfEventBriteCallout2.cls), that does a callout to Eventbrite using that EventID to get all details of the Event.

From here the ghfEventBriteCallout2 class parses the detials, and a SOQL query is run to check IF an event is already in salesforce with the same EventID. IF true it should update it.

Otherwise if no event is found with this eventID then a new event is created.

The issue is, when testing live by modifying an existing event on Eventbrite, the SOQL query always returns back no results, so a new event is created every time.

Alternatively, if I simply go into developer console and pass the event to ghfEventBriteCallout2.cls directly, the SOQL returns a result as it should, and the proper action is taken.

I am confused as to why the class is able to create new records, but is unable to query for existing ones when kicked off from Webhook > EventBriteWebhookListener.cls

Classes are below:

@RestResource (urlMapping = '/v1/eblisten/*') global without sharing class EventBriteWebhookListener {

global Final Static String EVENTBRITEMSG = 'GHF IS LISTENING FOR EVENTBRITE WEBHOOKS';


@httpPost
global static void postMethod() {        

    
    RestResponse response = RestContext.response;
    response.addHeader('Content-Type', 'text/plain');
    String responseString = RestContext.request.requestBody.toString();
    System.debug(System.LoggingLevel.Debug, '**** \n '+responseString);
    response.responseBody = Blob.valueOf('{success:true, event:"On Hood"}');
    response.statusCode = 200;

    //Parses the payload
    eventPayloadDeserializer varPayload = (eventPayloadDeserializer)JSON.deserialize(responseString, eventPayloadDeserializer.class);        
    system.debug('!! Debugging api_url: '+varPayload.api_url);

    //Sets the payload URI as string
    String passURL = varPayload.api_url;
    system.debug('!! Debugging passURL: '+passURL);

    //Passes the URI to a class that calls back out to eventbrite for full details, and creates the event in SF
    ghfEventBriteCallout2.getEventfromURI(passURL);          


}

}

<<<<<<<>>>>>>>>>>>>

public with sharing class ghfEventBriteCallout2{

public static final String EVENTBRITEAPI = 'https://www.eventbriteapi.com/v3/'; 


public static string getEventfromURI(String wholeURI){

    String errorMessage = '';

    String endpoint = wholeURI;   



    Http http = new Http();      

    httpRequest httpReq = new httpRequest();
    httpReq.setMethod('GET');
    httpReq.setHeader('Accept', 'application/json');
    httpReq.setHeader('Authorization', '***********');
    httpReq.setEndpoint(endpoint);    
    
    
    System.debug(UserInfo.getUserId());
    UserInfo.getUserId();
    
    HttpResponse response = new HttpResponse();

    try{
        response = http.send(httpReq);
        if(response.getStatusCode()==200){
            System.Debug('<<<<<<<<<<<<<<<<<<<<<RESPONSE 200 DATA RETURNED SUCCESSFULLY >>>>>>>>>>>>>>>>>>>>>>>>>>>');
            String responsebody = response.getBody();
            String body = response.getBody();
            System.debug(System.LoggingLevel.DEBUG, ' Response from Server '+responsebody);   

            ghfEventDeserial evid = (ghfEventDeserial)JSON.deserialize(responsebody, ghfEventDeserial.class);
            
            String eventIdParse = evid.id;
            System.Debug('!!!!!! Event ID Parse : '+ eventIdParse);

            Map<ID, Eventbrite_Event__c> eventToUpdateMap = new Map<ID, Eventbrite_Event__c>([SELECT Id, Name__c, EventrbiteId__c FROM Eventbrite_Event__c WHERE EventrbiteId__c =:eventIdParse]);
            system.debug('!eventToUpdateMap.isEmpty evalueates to >>>>>> '+!eventToUpdateMap.isEmpty());
            if(!eventToUpdateMap.isEmpty()){
                
                System.Debug('This Map is NOT Empty!!!!!!');
                

            }else{

                system.debug('!eventToUpdateMap.isEmpty evalueates to >>>>>> '+!eventToUpdateMap.isEmpty());
                System.Debug('This Map is Empty!!!!!!');
                System.Debug('!!!!!! EVENT MAP : '+ eventToUpdateMap);
                List<Eventbrite_Event__c> eventList = new List<Eventbrite_Event__c>();            
            
                Eventbrite_Event__c event = new Eventbrite_Event__c ();
                event.Name__c = evid.name.text;
                event.EventrbiteId__c = evid.id;
                event.Event_URL__c = evid.url;
                event.API_URL__c = evid.resource_uri;           
                
    
                eventList.add(event);
    
                insert eventList;
            }    


            

            
        }else{
            System.debug('ELSE FIRING! StatusCode not 200. Instead it is :'+ response.getStatusCode());
            errorMessage = 'Unexpected Error while communicating with API. '
            + 'Status '+response.getStatus()+ 'and Status Code '+response.getStatuscode();
            System.debug(System.loggingLevel.DEBUG, 'Exception Executed '+errorMessage); 
        }
    }catch(System.Exception e){
        if (String.valueOf(e.getMessage()).StartsWith('Unauthorized endpoint')){
            errorMessage = 'Unauthorized endpoint: Please add '+endpoint+ 'to the organizations remote site settings';
        }else{errorMessage = 'Unexpected Error while communicating with API. '
                + 'Status '+response.getStatus()+' and Status Code '+response.getStatusCode();
            }
            System.debug(LoggingLevel.DEBUG, 'Exception Executed '+errorMessage);
        }

    return response.getBody();

    } 
    
        
}
0

There are 0 best solutions below