Created
January 23, 2017 19:58
-
-
Save amulhai/8c524f8774ae0a6a20a23e7cedabb97a to your computer and use it in GitHub Desktop.
sfdc-jira integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Author: Amul Baranwal | |
//CaseTriggerHandler | |
//Call the JIRA Create Issue API | |
public class CaseTriggerHandler{ | |
@future | |
public static void FutureCreateJIRAIssue(Id CaseRecId) | |
{ | |
Case CaseRec=new Case(); | |
CaseRec.id=CaseRecId; | |
CreateJIRAIssue(CaseRec); | |
} | |
@future | |
public static void FutureUpDateJIRAIssue(Id CaseRecId) | |
{ | |
Case CaseRec=new Case(); | |
CaseRec.id=CaseRecId; | |
UpDateJIRAIssue(CaseRec); | |
} | |
@future | |
public static void FutureAddAttachment(Id attachId) | |
{ | |
Attachment attach=new Attachment(); | |
attach.id=attachId; | |
AddAttachment(attach); | |
} | |
@future | |
public static void FutureAddJIRAComment(Id CaseRecCommentId) | |
{ | |
CaseComment CaseRecComment=new CaseComment(); | |
CaseRecComment.id=CaseRecCommentId; | |
addJIRAComment(CaseRecComment); | |
} | |
public static void CreateJIRAIssue(Case CaseRec){ | |
// get Connection Detail | |
UtilClass objUtilClass=new UtilClass(); | |
UtilClass.ConnectionWrp obj=objUtilClass.getConnectionDetail(); | |
// get Case Detail | |
UtilClass.CaseWrapper objCase=objUtilClass.getCaseDetail(CaseRec); | |
// get Case Record Dynamic | |
case CaseDetail=new case(); | |
CaseDetail=UtilClass.getCaseRecordDetail(CaseRec); | |
//get Project key | |
string sProjKey=UtilClass.sProjectKey; | |
system.debug(sProjKey); | |
//get Field Mapping Detail | |
Map<string, string> MapFieldMapping=new Map<string,string>(); | |
MapFieldMapping=UtilClass.getFieldMapping(); | |
//customfield_10119--SFDCCASE Field Id in JIRA | |
//Create Request Body | |
//Working request | |
//string reqbody='{"fields":{"project":{"key":"' +sProjKey+ '"},"customfield_10119":"'+ objCase.CaseNumber +'","summary":"'+ objCase.Subject +'","description":"'+ objCase.Description.trim()+ '","issuetype":{"name":"Bug"}}}'; | |
// Hard coded Request | |
//string reqbody='{"fields":{"project":{"key":"SFDCIN"},"summary": "Please ignore.","description": "Creating of an issue using project","issuetype":{"name":"Bug"}}}'; | |
string reqbody='{"fields":{"project":{"key":"' +sProjKey+ '"}'; | |
for(string strTemp:MapFieldMapping.keySet()){ | |
reqbody= reqbody +',' + '\"'+ strTemp + '\":' + '\"' + CaseDetail.get(MapFieldMapping.get(strTemp)) + '\"'; | |
} | |
reqbody= reqbody + ',"issuetype":{"name":"Bug"}}}'; | |
system.debug(reqbody); | |
//Send http Request | |
Http h = new Http(); | |
HttpRequest req = new HttpRequest(); | |
String auth_header = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(obj.strusername + ':' + obj.strpassword)); | |
req.setHeader('Content-Type','application/json'); | |
req.setHeader('accept','application/json'); | |
req.setHeader('Authorization',auth_header); | |
req.setMethod('POST'); | |
req.setEndpoint(UtilClass.CreateJIRAURL); | |
req.setBody(reqbody); | |
req.setTimeout(120000); | |
HttpResponse res = h.send(req); | |
system.debug(res.getBody()); | |
JSONParser parser = JSON.createParser(res.getBody()); | |
string JIRAKey=''; | |
while (parser.nextToken() != null) { | |
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && | |
(parser.getText() == 'key')) { | |
// Get the value. | |
parser.nextToken(); | |
JIRAKey = parser.getText(); | |
} | |
} | |
//{"errorMessages":[],"errors":{"10119":"Field '10119' cannot be set. It is not on the appropriate screen, or unknown."}} | |
//Add Error Handling Code | |
Case updateCase=new Case(); | |
updateCase.id=CaseRec.id; | |
updateCase.JIRAKEY__c=JIRAKey; | |
updateCase.JIRA_Project_Key__c=sProjKey; | |
update updateCase; | |
} | |
public static void UpDateJIRAIssue(Case CaseRec){ | |
// get Connection Detail | |
UtilClass objUtilClass=new UtilClass(); | |
UtilClass.ConnectionWrp obj=objUtilClass.getConnectionDetail(); | |
// get Case Detail | |
UtilClass.CaseWrapper objCase=objUtilClass.getCaseDetail(CaseRec); | |
//get Project key | |
string sProjKey=UtilClass.sProjectKey; | |
system.debug(sProjKey); | |
//Create Request Body | |
string reqbody='{"fields":{"summary":"'+ objCase.Subject +'","description":"'+ objCase.Description.trim()+ '"}}'; | |
//string reqbody='{"fields":{"project":{"key":"SFDCIN"},"summary": "Please ignore.","description": "Creating of an issue using project","issuetype":{"name":"Bug"}}}'; | |
system.debug(reqbody); | |
//Send http Request | |
Http h = new Http(); | |
HttpRequest req = new HttpRequest(); | |
String auth_header = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(obj.strusername + ':' + obj.strpassword)); | |
req.setHeader('Content-Type','application/json'); | |
req.setHeader('accept','application/json'); | |
req.setHeader('Authorization',auth_header); | |
req.setMethod('PUT'); | |
string sEndPoint=UtilClass.CreateJIRAURL+'/' + objCase.JIRAKEY ; | |
system.debug(sEndPoint); | |
req.setEndpoint(UtilClass.CreateJIRAURL+'/'+objCase.JIRAKEY); | |
req.setBody(reqbody); | |
req.setTimeout(120000); | |
HttpResponse res = h.send(req); | |
system.debug(res.getBody()); | |
// You should just receive a response with a status of "204 No Content" | |
system.debug(res.getStatusCode()); | |
/*JSONParser parser = JSON.createParser(res.getBody()); | |
string JIRAKey=''; | |
while (parser.nextToken() != null) { | |
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && | |
(parser.getText() == 'key')) { | |
// Get the value. | |
parser.nextToken(); | |
JIRAKey = parser.getText(); | |
} | |
} | |
Case updateCase=new Case(); | |
updateCase.id=CaseRec.id; | |
updateCase.JIRAKEY__c=JIRAKey; | |
updateCase.JIRA_Project_Key__c=sProjKey; | |
update updateCase;*/ | |
} | |
public static void AddAttachment(Attachment attch){ | |
// get Connection Detail | |
UtilClass objUtilClass=new UtilClass(); | |
UtilClass.ConnectionWrp obj=objUtilClass.getConnectionDetail(); | |
String jira_host = 'https://host.atlassian.net'; | |
String issue = 'SFDCIN-5'; | |
Attachment attach = [select Name, Body from Attachment where id=:attch.Id limit 1]; | |
Blob file_body = attach.Body; | |
String file_name = attach.Name; | |
String auth_header = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(obj.strusername + ':' + obj.strpassword)); | |
//Attachment attach = [select Name, Body from Attachment limit 1]; | |
String url = UtilClass.CreateJIRAURL+'/' + issue + '/attachments'; | |
String boundary = '----------------------------741e90d31eff'; | |
String header = '--' + boundary + '\n' + | |
'Content-Disposition: form-data; name="file"; filename="' + file_name + '";\n' + | |
'Content-Type: application/octet-stream'; | |
String footer = '--' + boundary + '--'; | |
String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header + '\r\n\r\n')); | |
while (headerEncoded.endsWith('=')){ | |
header += ' '; | |
headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n')); | |
} | |
String bodyEncoded = EncodingUtil.base64Encode(file_body); | |
Blob bodyBlob = null; | |
String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length()); | |
if (last4Bytes.endsWith('==')) { | |
last4Bytes = last4Bytes.substring(0, 2) + '0K'; | |
bodyEncoded = bodyEncoded.substring(0, bodyEncoded.length() - 4) + last4Bytes; | |
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer)); | |
bodyBlob = EncodingUtil.base64Decode(headerEncoded + bodyEncoded + footerEncoded); | |
} else if (last4Bytes.endsWith('=')) { | |
last4Bytes = last4Bytes.substring(0, 3) + 'N'; | |
bodyEncoded = bodyEncoded.substring(0, bodyEncoded.length()-4) + last4Bytes; | |
footer = '\n' + footer; | |
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer)); | |
bodyBlob = EncodingUtil.base64Decode(headerEncoded + bodyEncoded + footerEncoded); | |
} else { | |
footer = '\r\n' + footer; | |
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer)); | |
bodyBlob = EncodingUtil.base64Decode(headerEncoded + bodyEncoded + footerEncoded); | |
} | |
HttpRequest req = new HttpRequest(); | |
req.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary); | |
req.setHeader('Authorization', auth_header); | |
req.setHeader('X-Atlassian-Token', 'nocheck'); | |
req.setMethod('POST'); | |
req.setEndpoint(url); | |
req.setBodyAsBlob(bodyBlob); | |
req.setTimeout(120000); | |
Http h = new Http(); | |
HTTPResponse res = h.send(req); | |
} | |
public static void AddJIRAComment(CaseComment CaseRecComment){ | |
//SELECT CommentBody,Id,ParentId FROM CaseComment | |
// get Connection Detail | |
UtilClass objUtilClass=new UtilClass(); | |
UtilClass.ConnectionWrp obj=objUtilClass.getConnectionDetail(); | |
// get Case Detail | |
Case CaseRec=new Case(); | |
CaseRec.Id=CaseRecComment.ParentId; | |
UtilClass.CaseWrapper objCase=objUtilClass.getCaseDetail(CaseRec); | |
//get CaseComment Detail | |
CaseRecComment=objUtilClass.getCaseComment(CaseRecComment); | |
//get Project key | |
string sProjKey=UtilClass.sProjectKey; | |
system.debug(sProjKey); | |
//Create Request Body | |
//string reqbody='{"fields":{"project":{"key":"' +sProjKey+ '"},"summary":"'+ objCase.Subject +'","description":"'+ objCase.Description.trim()+ '","issuetype":{"name":"Bug"}}}'; | |
//string reqbody='{"fields":{"project":{"key":"SFDCIN"},"summary": "Please ignore.","description": "Creating of an issue using project","issuetype":{"name":"Bug"}}}'; | |
//Add Comment Body | |
//string reqbody='{"update":{"comment":[{"add":{"body": "It is time to finish this task"}}]}}'; | |
string reqbody='{"body":"'+ CaseRecComment.CommentBody+'"}'; | |
//string reqbody='{"update":{"comment":[{"add":{"body":"' +CaseRecComment.CommentBody+ '"}}]}}'; | |
system.debug(reqbody); | |
//Send http Request | |
Http h = new Http(); | |
HttpRequest req = new HttpRequest(); | |
String auth_header = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(obj.strusername + ':' + obj.strpassword)); | |
req.setHeader('Content-Type','application/json'); | |
req.setHeader('accept','application/json'); | |
req.setHeader('Authorization',auth_header); | |
req.setMethod('POST'); | |
string sEndPoint=UtilClass.CreateJIRAURL+'/' + objCase.JIRAKEY +'/comment'; | |
system.debug(sEndPoint); | |
req.setEndpoint(UtilClass.CreateJIRAURL+'/' + objCase.JIRAKEY +'/comment'); | |
req.setBody(reqbody); | |
req.setTimeout(120000); | |
HttpResponse res = h.send(req); | |
system.debug(res.getBody()); | |
system.debug(res.getStatusCode()); | |
//You should just receive a response with a status of "201" with the full json representation of the added comment | |
/*JSONParser parser = JSON.createParser(res.getBody()); | |
string JIRAKey=''; | |
while (parser.nextToken() != null) { | |
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && | |
(parser.getText() == 'key')) { | |
// Get the value. | |
parser.nextToken(); | |
JIRAKey = parser.getText(); | |
} | |
} | |
Case updateCase=new Case(); | |
updateCase.id=CaseRec.id; | |
updateCase.JIRAKEY__c=JIRAKey; | |
updateCase.JIRA_Project_Key__c=sProjKey; | |
update updateCase;*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment