Call the Infusionsoft API from Salesforce - Part II

In the last blog post I described the Infusionsoft API and how to get an access token.  In this blog post I will show one example on how to use that access token to make an Infusionsoft API call.

There are a wide variety of API calls that can be made.  One cool feature on the Infusionsoft site is this page which allows you to test out their API without having to write any code.

The example API call that I would like to show you involves pulling back the fields from a report.  Here is an example report URL from Infusionsoft:
https://vm921.infusionsoft.com/Reports/searchTemplate.jsp?filterId=999&reportClass=ClassName

where 999 is the user who created this report and ClassName is the name of this report.  Note that sometimes the filterId is 0, but that means everybody can run it.  When you call the API as I describe below send a 1 instead of a 0.

We want to use the ‘SearchService’ options within the API.  The getAllReportColumns() call will retrieve all of the columns available in a report.  That could be useful in creating some generic reporting routines.  The getSavedSearchResultsAllFields() call will return all fields in the report.  That call returned too much data for me so I decided to use the getSavedSearchResults() call so I could limit the number of fields returned. 

One important item to remember when calling reports is that the report parameters cannot be changed when calling the API.  So we cannot dynamically change a report to bring back payments from the last 10 days instead of the last 30 days.  Keep that limitation in mind when designing your integration.  Also note that in the getSavedSearchResults() api call that a pageNumber must be specified.  This start with 0 for the first 1000 records and then 1 will be the next 1000 and so on.  So you need to be able to design your calls so that you keep making calls by incrementing this pageNumber parameter until you do not get anymore rows back.

So finally onto the code to make our call:


public static string callWebCart(string accessToken){
		
       string xmlBody = '<?xml version="1.0" encoding="UTF-8"?>' +
			'<methodCall>' +
				'<methodName>SearchService.getSavedSearchResults</methodName>' +
					'<params>' +
						'<param><value><string>myapiKey</string></value></param>' + //apiKey
						'<param><value><int>999</int></value></param>' + //Saved Search ID
						'<param><value><int>1</int></value></param>' + //userID
						'<param><value><int>0</int></value></param>' + //page number, start at 0 for first 1000 records
						'<param><value><array><data>' +
							'<value><string>Id</string></value>' + //Fields for report
							'<value><string>ContactId</string></value>' +
						'</data></array></value></param>' +
					'</params>' +
				'</methodCall>';
		
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('Content-Type','application/xml'); 
        req.setHeader('Content-Type','text/xml;charset=UTF-8');
        req.setHeader('Content-Length',string.valueOf(xmlBody.length()));     
        req.setEndpoint('https://api.infusionsoft.com/crm/xmlrpc/v1?access_token=' + accessToken);
        req.setMethod('POST');
	req.setTimeout(60000);
	req.setBody(xmlBody);
	      
	string bodyValue = '';
	if(test.isRunningTest()){
	  bodyValue = getExamplePaymentXMLResult();
	}else{
	  HttpResponse res = h.send(req);
	  bodyValue = res.getBody();
	}

	if((bodyValue == '<h1>Developer Inactive</h1>' || bodyValue == '<h1>Not Authorized</h1>')){
	   //Get new Access Token using the refresh token and call the api again
	}

	return bodyValue;
}

The fields to pull back are placed in the XML in a repeated fashion after the page number section.  It takes a bit of getting used to in that the order of the parameters drives what they are.  We do not get to set a ‘pageNumber’ xml node to define the page number.  We have to know that the page number goes into the 4th piece of data after the UserID.

Once you get the bodyValue back you can use various XML parsing methods to get out your data and then save it into Salesforce per your requirements.

Salesforce provides so many integration points and options.  Please get in touch with me if you have any questions about how to make Salesforce integrate with other related data within your organization.

Comments

Returning visitor? Please login or register.

Leave A Comment