Skip to content

Instantly share code, notes, and snippets.

@djuang1
Created December 8, 2014 20:50
Show Gist options
  • Save djuang1/1d5df9e1bc7b3b3b25d5 to your computer and use it in GitHub Desktop.
Save djuang1/1d5df9e1bc7b3b3b25d5 to your computer and use it in GitHub Desktop.
Mule - Groovy - MSCRM 4.0 SOAP Request - Update Account
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.xpath.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.params.AuthPNames;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.AuthPolicy;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import groovy.json.JsonSlurper;
import java.text.SimpleDateFormat;
def slurper = new JsonSlurper();
def result = slurper.parseText(payload);
def myFirstServiceResponse = result['myf:myFirstServiceResponse'];
def rows = myFirstServiceResponse['myf:rows'];
def row = rows['myf:row'];
String ACCOUNT_UPDATE_SOAP_TEMPLATE =
" <soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:web=\"http://schemas.microsoft.com/crm/2006/WebServices\" xmlns:cor=\"http://schemas.microsoft.com/crm/2006/CoreTypes\"> " +
" <soap:Body>" +
" <entity xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\" xmlns:ns2=\"http://schemas.microsoft.com/crm/2006/Query\" xmlns:ns3=\"http://schemas.microsoft.com/crm/2006/CoreTypes\" xmlns:ns4=\"http://schemas.microsoft.com/crm/2006/Scheduling\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"account\">" +
" <accountid>" + row['myf:CompanyNativeID'] + "</accountid>" +
" <name>" + row['myf:CompanyName'] + "</name>" +
" <address1_line1>" + row['myf:AddressLine1'] + "</address1_line1>" +
" <address1_city>" + row['myf:AddressCity'] + "</address1_city>" +
" <address1_stateorprovince>" + row['myf:AddressState'] + "</address1_stateorprovince>" +
" </entity>" +
" </soap:Body>" +
" </soap:Envelope>";
String soapMessage = ACCOUNT_UPDATE_SOAP_TEMPLATE;
String soapaction = "http://schemas.microsoft.com/crm/2006/WebServices/Update";
DefaultHttpClient httpClient = new DefaultHttpClient();
// Authentication
List<String> authPreferences = new ArrayList<String>();
authPreferences.add(AuthPolicy.NTLM);
authPreferences.add(AuthPolicy.BASIC);
httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authPreferences);
NTCredentials credentials = new NTCredentials("${ntlm.user}", "${ntlm.password}", "${ntlm.workstation}", "${ntlm.domain}");
httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
// Prepare Request
HttpPost request = new HttpPost( "/MSCrmServices/2006/CrmService.asmx" );
request.setEntity(new StringEntity(soapMessage));
request.addHeader("Content-Type", "text/xml;charset=UTF-8");
request.addHeader("SOAPAction", soapaction);
// Execute
HttpResponse response = httpClient.execute(new HttpHost("${mscrm.host}"), request);
HttpEntity entity = response.getEntity();
// Process Response
InputStreamReader inputStreamReader = new InputStreamReader(entity.getContent());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String nextLine = null;
while ((nextLine = bufferedReader.readLine()) != null) {
stringBuilder.append(nextLine);
}
return stringBuilder.toString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment