Hello friends, today we are going to discuss QuickBooks Integration With Salesforce Rest API Using Named Credentials From Flow. Here we will create the Apex class and use Rest API to integrate with QuickBooks. QuickBooks and Salesforce Integration using Apex are very simple. We invoke Apex class from the Lightning flow.
What is QuickBooks
Quickbooks is accounting software for your small business which allows you to create and send invoices, track sales and inform you about how business is doing at a point in time, with accounting data reading in the cloud.
Why We Need to Integrate with Salesforce to Quickbook
- Increase productivity by sharing the data between these.
- Achieve better forecasting.
- Give better approch for how we complete our goal.
To Integrate the Quickbooks with Salesforce lets layout some Key details First
- Requirement : We need some automation in Salesforce so that whenever an Account is created in salesforce that Account should also be created in Quickbooks as well.
- Process in QuickBooks : First Sign Up for a developer account with Quickbooks then create an App there then we will create an auth provider and named credential for QuickBooks in sales force.
- Process in Salesforce : After setting up Auth provider and named credentials. We will create a flow that will invoke an Apex class and that class will make a call out to the Quickbooks API with a POST request to create a new account in Quickbooks with details sent from salesforce.
Named credentials blog link here.
So in Salesforce After setting up the named credential we will write an Apex class with the Invokable method which we will invoke via the flow.
Code :
QuickbooksIntegration.cls:
public class QuickbooksIntegration{
@InvocableMethod
public static void CreateAccount(List<Id> accountIds){
//get account
List<Account> accountList = [SELECT id, name from account where id =:accountIds[0]];
//Data map Preparation
map<String,Object> mainMap = new map<String,Object>();
mainMap.put('Name', accountList[0].name);
mainMap.put('AccountType', 'Accounts Receivable');
if(mainMap!= null && !mainMap.isEmpty()){
//Calling Async method for callout
insertOnQuickBook(JSON.serializePretty(mainMap));
}
}
@future(callout =true)
public static void insertOnQuickBook(String jsonData){
//new request
HttpRequest req = new HttpRequest();
string endPointURL = 'callout:quickbook_named_credentials/v3/company/4620816365165921730/account?minorversion=59';
req.setEndpoint(endPointURL);
req.setMethod('POST');
req.setHeader('Content-Type','application/json');
req.setHeader('Accept','application/json');
req.setHeader('Content-Length', '512');
req.setBody(jsonData);
req.setTimeout(120000);
system.debug('--req--'+req);
Http http = new Http();
HTTPResponse response = http.send(req);
//Response Handle
if(response.getStatusCode() == 200 ){
system.debug('-Response--'+response.getBody());
system.debug('--deserialize'+JSON.deserializeUntyped(response.getBody()));
Map<String, Object> serializeResponseBody = new Map<String, Object>();
serializeResponseBody = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
System.debug('serializeResponseBody>>'+JSON.serializePretty(serializeResponseBody));
}
}
}
Create Flow:
After creating the class we are going to create a flow that will run on the creation of Accounts in salesforce. That flow will have an Action type element which will refer to the QuickbooksIntegration class and we also will set the input parameter for the invokable method which will be the current record Id.
4 comments
Great article thanks, have you done the integration of invoice and invoice lines to quick book invoice? Please let me know
Hi Laxman Rao, you can try other API as well in this class
Hi, Thanks for the article. I’ve setup the named credentials for the quickbooks but When I am trying to sync the customer I am gettting 401 unauthorized error. Can you please help me in fixing that?
HI I am not able to fetch the customer, Item from quickbook end to salesforce using apex