1
votes

I am trying to do an APEX Trigger Callout using POST to Google Cloud. The problem I have is: whenever the trigger happened, I see the authentication is happening fine, but message is coming as "Undefined" only from Stackdriver logs, hence the cloud function will fail since the message should be in JSON format. Not sure why it is happening. I am pretty much new to Salesforce. Please provide some hints on this.

If I test the cloud function directly (Using Testing option inside cloud function), I see the insert to BigQuery table as expected, but from Salesforce trigger it seems, the message is not being captured properly. PFB Codes used in Salesforce.Expecting the same message which we are inserting into LEAD table in Salesforce as Rest API request

Salesforce Class:

Public class Callout {
  @future(callout=true)
  Public static void httpcallout(){
    Lead c = [Select Name from Lead Limit 1] ;
    system.debug('Halo Trigger');
    JSONGenerator gen = JSON.createGenerator(true);   
    gen.writeStartObject();     
    gen.writeStringField('Name', c.Name);
    gen.writeEndObject();   
    String jsonS = gen.getAsString();
    System.debug('jsonMaterials'+jsonS);
    String endpoint = 'https://us-central1-valid-weaver- 235212.cloudfunctions.net/Salesforce-GCP';
    HttpRequest req = new HttpRequest();
    req.setEndpoint(endpoint);
    req.setMethod('POST');
    req.setbody(jsonS);
    Http http = new Http();
    HTTPResponse response = http.send(req);
  }
} 

Salesforce Trigger

trigger SFGCP on Lead (after insert) {
  callout.httpcallout();
  system.debug('Hello');
}

Debug from Salesforce

Lead c = new Lead(Company ='Test',LastName='Admin');
insert c;
1

1 Answers

0
votes

Below code works fine with some minor change in Cloud Function . Thanks

Public class Callout {
    @future(callout=true)
    Public static void httpcallout()
    {
        Lead c = [select Name, LeadSource, Company from Lead order by createdDate DESC limit 1] ;
        system.debug('Halo Trigger');
        JSONGenerator gen = JSON.createGenerator(true);   
        gen.writeStartObject();     
        gen.writeStringField('Name', c.Name);
        gen.writeStringField('LeadSource', c.LeadSource);
        gen.writeStringField('Company', c.Company);
        gen.writeEndObject();   
        String jsonS = gen.getAsString();
        System.debug('jsonMaterials'+jsonS);
        String endpoint = 'https://us-central1-valid-weaver-235212.cloudfunctions.net    /Salesforce-GCP';
        HttpRequest req = new HttpRequest();
        req.setEndpoint(endpoint);
        req.setMethod('POST');
        req.setbody(jsonS);
        Http http = new Http();
        HTTPResponse response = http.send(req);
    }
}