daugaard47's avatar

Send and Receive data from an external source

I have a fairly large, existing application. I need to start sharing data between Salesforce and my app.

  1. I need to receive data from Salesforce
  2. I need to send data to Salesforce

Receiving Salesforce data: To receive data, would I simply create a webhook? Like.. https://myapp.com/api/webhook/salesforce This is how I do it with Stripe. I use a webhook to listen for events and receive data. But similar to Stripe, how do I create the API key? Do I need to do this? I'm assuming I would get this info from Salesforce.

Sending data to Salesforce: Then the reverse of this? Sending data to Salesforce. I get that I would create a Post Request and send the data via a Salesforce endpoint, but how do I get started building something like this?

  • Do I send the data via my controller
  • Do I need to create a resource
  • What are Headers? Do I need Headers
  • Do I need to create an authenticated link to give Salesforce. If so, How?

I'm not expecting anyone to break it all down for me. But rather point me in the right direction on my path to learning how to do this. I tend to make things more harder than they really are. I feel this might be the case.

In my existing app, what is the best way to do something like this? I'm researching API crash courses and articles, but not even sure if I really need an API.

Are there any real world scenarios courses that explain how to do this with Laravel? Any good packages I should look in to.

Any help or tips would be greatly appreciated. Currently using Laravel 8.

0 likes
7 replies
Tray2's avatar

Are we talking Salesforce or Oracle Apex Salesforce here?

If it's the latter you can use the ORDS (Oracle Rest Data Services) to build your web API.

webrobert's avatar

@daugaard47

the term is api. And they’re all different per your questions.

Google, “salesforce api” And read their api documentation.

There is a very basic api series here. Building a video game aggregator.

Some api calls are so simple others perhaps merit a whole package. I tend lean toward just writing what I need.

daugaard47's avatar

@webrobert I'm having success with the Receiving by using a single webhook that listens for event types from Salesforce.

I just need to make sure the Saleforce can structure the payload how I need it. Example:

{
	"data": {
	"type": "UpdateRecord",
	"attributes": {
		"id": 147,
		"program_id": null,
		"in": "HA-826188",
		"first_name": "John",
		"last_name": "Doe",
		}
	}
}

Single Endpoint for SF: https://myApp.com/api/salesforce/webhook Listen for the payload type name.

    public function handleWebhook(Request $request): Response
    {
        $payload = json_decode($request->getContent(), true);
        $method = 'handle' . Str::studly(str_replace('.', '_', $payload['data']['type']));

        // Run related webhook method, or run missingMethod
        if (method_exists($this, $method)) {
            // IF Method Found, Go To Method
            $this->{$method}($payload);
        } else {
            Log::info('method not found', $payload['data']['type']);
            $this->missingMethod($payload);
        }

        return new Response;
    }

    public function handleUpdateRecord($payload): \Illuminate\Http\JsonResponse {
        Log::info('Hit the UpdateRecord method');
        // Do Logic to Update the Record
        //...
    }

I'm thinking If it starts to get to bloated I could export the methods out to services.

As for sending Data to Salesforce I'm still working on that. But yes I agree. Only use what I need. Thanks for the advice.

webrobert's avatar
Level 51

@daugaard47,

oh good. salesforce may let you modify the webhook attributes, idk. but often you use the webhook to trigger your own call to their API to get the UpdateRecord. So on to the questions...

Do I send the data via my controller

What helped me was looking at external services like MVC where the service is the M. So it is just another place to get data. I make a services folder add em there... services/salesforce and then a couple of methods, one for the setup, one to map the response, and then whatever methods.. getRecord()

there's no pride in authorship...

public function handleUpdateRecord($payload): \Illuminate\Http\JsonResponse {
    Log::info('Hit the UpdateRecord method');

    // or queue a job
    $updatedRecord = (new Salesforce)->getRecord($payload['data']['attributes']['id']); 

    // Do Logic to Update the Record

 ...

Do I need to create a resource

not sure what you mean here.

What are Headers? Do I need Headers

Common to just use Laravel's guzzle wrapper and you can do whatever salesforce dictates.

Do I need to create an authenticated link to give Salesforce. If so, How?

whatever auth they require you can make a question for or find a quick answer searching.

1 like
daugaard47's avatar

@webrobert This is all great info. Thanks for helping me out. I've been looking into Guzzle and I think that will be perfect for sending my data.

When I was talking about the resources I was referring to this: https://laravel.com/docs/8.x/eloquent-resources#resource-responses

Looks like it's a way to format the Json in the structure I want to send of receive. I'll keep this thread updated on my progress. If anything it might help others in the future.

1 like
webrobert's avatar

@daugaard47, you're welcome.

As for Laravel Api Resource...

When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application's users.

In your case, you are just making and receiving requests from a service not serving an api of your own.

If you got what you needed please mark a best answer on this thread. And you can always come back around and update your progress.

1 like

Please or to participate in this conversation.