Thursday, 26 March 2020

Salesforce API SOAP endpoint - SoapUi

Hey all,
so I got to recently try out salesforce API and how to hit them up.
This is a summary of how to do it.
So, the first thing we need to make any call is authentication. I am pretty sure all of you know what is authentication but nonetheless, authentication is basically verification of your identity.
Once we authenticate, we get a session token. This session token can be used to make all other calls.

For authentication, you need username, password and security token.

Step 1: Download WSDL for your organization. Now, this is the file containing the relevant info of your org. There are two different types of WSDL available in salesforce.  It is more specific to your org than Partner WSDL which is a bit more generic.

To generate WSDL -
1. Log in to Salesforce as 'System Administrator' or as a user who has the 'Modify All Data Permission'.
2. Setup > Search for 'API' in the quick find box.
3. Select API.
4. Click Generate Enterprise WSDL.
5. Save the XML WSDL file to your computer.
https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_quickstart_get_WSDLs.htm

To make soap calls, you need a soap UI. You can make post calls using postman as well but I have used soap UI. you can download it from here.
https://www.soapui.org/downloads/latest-release.html


Step 2: Once that is done, create a new project and import your WSDL.
Since WSDL is like a MAP to your org, if you changed your org or updated it, for example, created a new custom endpoint in your org, then you need to update this WSDL by downloading and importing it again. So, if you installed a package, then you need to update this as well.





Step 3:  Explore your new project. You will see a lot of new methods in your project. All of them have a sample request call. Update the required info according to each call.

Step 4: Authenticate and use the token to make new calls.
use the login call to authenticate.
Use a password appended with your security token for login.


Step 5: DescibeSobject. This method describes all fields associated with a particular 'SObject'. Use the token obtained in step 4.


Step 6: Query Method. Use the token obtained in step 4.


Step 7: Similarly, you can use the 'create' method to create new SObjects like a case, account in salesforce. Use the token obtained in step 4. I have a custom SObject in my org. Trying to create it.

So, This is brief about testing Salesforce API using SOAP calls.

PS - Stay hydrated. In this season of coronavirus. Maintain social distance but please feel free to reach out to me over the mail in case of doubts.




Friday, 20 March 2020

Salesforce API Rest Endpoints

Salesforce has exposed a soap library as well as Rest Endpoints. In the following doc, we will talk about Rest endpoints.
I hope all of you know what is Rest and why it is favored over soap lots of times.
To know more about it, check out the following link.  https://restfulapi.net/
Read Soap vs Rest if you have time as well. https://restfulapi.net/soap-vs-rest-apis/
Following is the standard salesforce Rest API documentaion - https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/quickstart_prereq.htm


Firstly you need a tool through which you can hit the API. You can hit GET API using a browser and POST using a terminal using a curl command.  I have used the Postman app. It is wildly popular for API testing. It makes the job easier. You can find it here - https://www.postman.com/downloads/

So, to start with salesforce API, you will first need to know what are the endpoints to hit. If you have custom code in your org and you have exposed some new rest endpoints then you can hit them up as well. The important condition being you know what path to hit.

At first, we will talk about the default REST endpoints. Salesforce has detailed documentation on which API to hit but it could take time to figure it out. So, I am here to help you with it.

So, Salesforce has a tool called Workbench where you can execute REST queries and get to know its path. That's how I got this as well. I have found an excellent Salesforce doc for this. https://trailhead.salesforce.com/en/content/learn/modules/api_basics/api_basics_rest
Play around with it figure it out.

Once you are done with it, if you want to still make API calls using POSTMAN, you need to Authenticate yourself first.
For that, you need to create a connected app in your salesforce org with API access enabled. It is a POST call. here is a great link which shows how to do it in detail -(https://docs.datawatch.com/swarm/desktop/Generating_a_Client_ID_and_ClientSecret_Key_for_Salesforce_Connections.htm)

Following is the standard document from salesforce to gain more knowledge about authentication flows. Use password appended with the security token.
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_oauth_and_connected_apps.htm

This returns a token, which you can use for making other calls.
For example, 

This is just for a token, you still need to input parameters that you are using for the API under the params tab for the GET call. Like,

However, for POST call you need to provide data that you want to send over under the tab named 'Body' in raw format. and give the content type under the 'Headers' tab as 'JSON'.
At least that's what I did.
I am trying to create the Community user object from the managed package I have installed using POST API.



Add token, add headers, add body and you are good to go. Hit the API and see the response. 
The response depends on what is the call made. If it was for creating an SObject like community user, it will return the id of the object created.
If you hit a GET call asking for a bunch of contacts, then the response will contain that. You can see the format of response while playing around with Workbench anyways. (https://workbench.developerforce.com/login.php)
Use it effectively. It is a great tool.

You can as well create your own rest endpoints in salesforce.
You will see how it hit such customized endpoints as well. 
Although to know correct path for hitting such endpoints declared inside installed managed package, follow the link - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_methods.htm

Para from the above link - 
Apex REST methods can be used in managed and unmanaged packages. When calling Apex REST methods that are contained in a managed package, you need to include the managed package namespace in the REST call URL. For example, if the class is contained in a managed package namespace called packageNamespace and the Apex REST methods use a URL mapping of /MyMethod/*, the URL used via REST to call these methods would be of the form https://instance.salesforce.com/services/apexrest/packageNamespace/MyMethod/. For more information about managed packages, see What is a Package?



So, I think this is it, guys. You can do REST API salesforce testing on your own then. Take care. Stay Hydrated. 
PS - I always forget to drink water.