-
Notifications
You must be signed in to change notification settings - Fork 1
/
MergeLeads.java
129 lines (114 loc) · 4.24 KB
/
MergeLeads.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
MergeLeads.java
Marketo REST API Sample Code
Copyright (C) 2016 Marketo, Inc.
This software may be modified and distributed under the terms
of the MIT license. See the LICENSE file for details.
*/
package com.tadigital.leadActions;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.NoSuchElementException;
import java.util.Scanner;
import javax.net.ssl.HttpsURLConnection;
import com.eclipsesource.json.*;
//the Java sample code on dev.marketo.com uses the minimal-json package
//minimal-json provides easy and fast representations of JSON
//for more information check out https://github.com/ralfstx/minimal-json
public class MergeLeads {
public String marketoInstance = "https://876-swj-450.mktorest.com";// Replace this with the host from Admin Web
// Services
public String marketoIdURL = marketoInstance + "/identity";
public String clientId = "d72450d9-2bbb-4116-b7af-79db718cedc7"; // Obtain from your Custom Service in
// Admin>Launchpoint
public String clientSecret = "5Y1JEIL3UR3iKJyPeBt7XVhVbsk5GbRM"; // Obtain from your Custom Service in
// Admin>Launchpoint
public String idEndpoint = marketoIdURL + "/oauth/token?grant_type=client_credentials&client_id=" + clientId
+ "&client_secret=" + clientSecret;
public int[] leadIds; // one or more lead IDs of losing records, required
public int winningId; // the winning Lead ID
public boolean mergeInCrm; // Optional to force a merge in instances with a native CRM sync
// Make Request
public String postData() {
String result = null;
try {
// Assemble the URL to retrieve data from
StringBuilder endpoint = new StringBuilder(
marketoInstance + "/rest/v1/leads/" + winningId + "/merge.json?access_token=" + getToken());
if (leadIds.length > 1) {// if more than 1 id, make a comma separated string of IDs
endpoint.append("&leadIds=" + csvString(leadIds));
} else {
endpoint.append("&leadId=" + leadIds[0]);
}
// append optional parameters
if (mergeInCrm) {
endpoint.append("&mergeInCrm=true");
}
URL url = new URL(endpoint.toString());
HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setRequestProperty("Content-type", "application/json");// "application/json" content-type is
// required.
urlConn.setRequestProperty("accept", "text/json");
// urlConn.setDoOutput(true);
int responseCode = urlConn.getResponseCode();
if (responseCode == 200) {
InputStream inStream = urlConn.getInputStream();
result = convertStreamToString(inStream);
} else {
result = "Status Code: " + responseCode;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
// takes an array of fields as strings and concatenates them with alternating
// commas to use in a URL param
private String csvString(int[] fields) {
StringBuilder fieldCsv = new StringBuilder();
for (int i = 0; i < fields.length; i++) {
fieldCsv.append(fields[i]);
if (i + 1 != fields.length) {
fieldCsv.append(",");
}
}
return fieldCsv.toString();
}
public String getToken() {
String token = null;
try {
URL url = new URL(idEndpoint);
HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
urlConn.setRequestMethod("GET");
urlConn.setRequestProperty("accept", "application/json");
int responseCode = urlConn.getResponseCode();
if (responseCode == 200) {
InputStream inStream = urlConn.getInputStream();
Reader reader = new InputStreamReader(inStream);
JsonObject JSONObject = JsonObject.readFrom(reader);
token = JSONObject.get("access_token").asString();
} else {
throw new Exception("Status: " + responseCode);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return token;
}
private String convertStreamToString(InputStream inputStream) {
try {
return new Scanner(inputStream).useDelimiter("\\A").next();
} catch (NoSuchElementException e) {
return "";
}
}
}