Skip to content

Instantly share code, notes, and snippets.

@axeda
Created September 14, 2011 20:01
Show Gist options
  • Save axeda/1217623 to your computer and use it in GitHub Desktop.
Save axeda/1217623 to your computer and use it in GitHub Desktop.
ServiceMax Integration with the Axeda Platform
import org.apache.commons.httpclient.Credentials
import org.apache.commons.httpclient.HostConfiguration
import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.UsernamePasswordCredentials
import org.apache.commons.httpclient.auth.AuthScope
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.commons.httpclient.methods.PostMethod
import org.apache.commons.httpclient.NameValuePair
import org.apache.commons.httpclient.*;
import java.util.*;
import java.text.*;
def sessionId;
try
{
String soap = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com">
<soapenv:Header>
<urn:LoginScopeHeader>
<urn:organizationId></urn:organizationId>
<!--Optional:-->
<urn:portalId></urn:portalId>
</urn:LoginScopeHeader>
</soapenv:Header>
<soapenv:Body>
<urn:login>
<urn:username>user@axeda.com</urn:username>
<urn:password>notmypassword</urn:password>
</urn:login>
</soapenv:Body>
</soapenv:Envelope>"""
//println(soap)
String sfdcHost = 'na1-api.salesforce.com'
HostConfiguration hc = new HostConfiguration()
hc.setHost(sfdcHost , 443, "https")
def url = "/services/Soap/c/20.0/00D30000000o8up/0DF30000000PJSJ'"
def client = new HttpClient()
PostMethod post = new PostMethod(url);
post.addRequestHeader("Content-type", "text/xml; charset=utf-8")
post.addRequestHeader("SOAPAction", "\"\"");
post.setRequestBody soap
try {
client.executeMethod(hc, post);
//get response from POST to SFDC
String responseBody = post.getResponseBodyAsString();
def xml = new XmlSlurper().parseText(responseBody).declareNamespace(soapenv:"http://schemas.xmlsoap.org/soap/envelope/")
sessionId = xml.'soapenv:Body'.loginResponse.result.sessionId.text()
println(sessionId)
def alarmname = alarm.name
def priority = alarm.severity
def root = """{"Origin" : "${alarmname}", "priority" : "${priority}","Subject" : "Fire in the hallway", "AccountID" : "0013000000f9EMC",
"Description" : "Hallway WC2 - West Wing Building 2 fire Alarm detected",
"SVMXC__Component__c" : "a0930000007Ha9K"}"""
String sfdcHost2 = 'na1.salesforce.com'
HostConfiguration hc2 = new HostConfiguration()
hc2.setHost(sfdcHost2 , 443, "https")
def url2 = "/services/data/v20.0/sobjects/Case/"
def client2 = new HttpClient()
PostMethod post2 = new PostMethod(url2);
post2.addRequestHeader("Content-type", "application/json")
post2.addRequestHeader("Authorization", "OAuth " + sessionId)
post2.addRequestHeader("X-PrettyPrint", "1")
post2.setRequestBody root
try {
client2.executeMethod(hc2, post2);
post2.releaseConnection();
} catch (IOException e) {
logger.error e.message
}
if(post2.getStatusCode() != 200) {
logger.info("Error in post --> " + post2.getStatusCode());
logger.info ("Response" + post2.getResponseBodyAsString());
}
} catch (HttpException e) {
logger.error e.message
} catch (IOException e) {
logger.error e.message
}
post.releaseConnection();
} catch (IOException e) {
logger.error e.message
}
return true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment