You need to sign in to do that
Don't have an account?
Uday raj
Creation of Issue into jira from Salesforce along with an attachment
When ever a case has ben created it should directly create an issue in Jira(wrote a Trigger on case object whenever a case is identified it will fire by applying some condition) now its turn to attach a file or attachment into Jira from Salesforce, myself strucked ova here.
public SalesforcetoJira(){}
//Change values in this class according to you JIRA/Salesforce coordinates
public String baseUrl = ''; // Base URL of your JIRA instance
public String username = ''; // JIRA username
public String password = ''; // JIRA password // Constructs Basic Http Authentication header from provided credentials
public String authHeader(){ Blob headerValue = Blob.valueOf(username+':'+password); return 'Basic ' + EncodingUtil.base64Encode(headerValue);
}
public SalesforcetoJira(JiraUrl,String objectType,String projectKey,String issueType,string summary,string description)
{
HttpRequest req = new HttpRequest();
Http http = new Http();
req.setMethod('GET');
//Set HTTPRequest header properties
req.setHeader('Accept', 'application/json');
req.setHeader('Content-Type','application/json');
Blob headerValue = Blob.valueOf(username+':'+password);
req.setHeader('Authorization','Basic '+ EncodingUtil.base64Encode(headerValue));
req.setEndpoint(baseUrl);
String CreateIssueJsonJIra= '{"fields": {"project":{"key": " "},"summary":" ","description":" " ,"issuetype":{"name": "Bug"}}} ';
String JSONData = JSON.serializePretty(CreateIssueJsonJIra); r
eq.setBody(CreateIssueJsonJIra);
system.debug('after response....'+CreateIssueJsonJIra);
try{
//Execute web service call here
HTTPResponse res = http.send(req);
String ResponseJsonString = res.getBody();
map<string,string> attachment= (map<string,string>)JSON.deserializeUntyped(ResponseJsonStringJIra);
system.debug('after response...'+attachment);
system.debug('after response...'+attachment.get('id'));
System.debug('ResponseJsonStringJIra'+ResponseJsonStringJIra);
JSONParser parser = JSON.createParser(ResponseJsonStringJIra);
}
}
catch(System.CalloutException e)
{
System.debug('Callout error: '+ e);
}
}
I appreciate any help.
public SalesforcetoJira(){}
//Change values in this class according to you JIRA/Salesforce coordinates
public String baseUrl = ''; // Base URL of your JIRA instance
public String username = ''; // JIRA username
public String password = ''; // JIRA password // Constructs Basic Http Authentication header from provided credentials
public String authHeader(){ Blob headerValue = Blob.valueOf(username+':'+password); return 'Basic ' + EncodingUtil.base64Encode(headerValue);
}
public SalesforcetoJira(JiraUrl,String objectType,String projectKey,String issueType,string summary,string description)
{
HttpRequest req = new HttpRequest();
Http http = new Http();
req.setMethod('GET');
//Set HTTPRequest header properties
req.setHeader('Accept', 'application/json');
req.setHeader('Content-Type','application/json');
Blob headerValue = Blob.valueOf(username+':'+password);
req.setHeader('Authorization','Basic '+ EncodingUtil.base64Encode(headerValue));
req.setEndpoint(baseUrl);
String CreateIssueJsonJIra= '{"fields": {"project":{"key": " "},"summary":" ","description":" " ,"issuetype":{"name": "Bug"}}} ';
String JSONData = JSON.serializePretty(CreateIssueJsonJIra); r
eq.setBody(CreateIssueJsonJIra);
system.debug('after response....'+CreateIssueJsonJIra);
try{
//Execute web service call here
HTTPResponse res = http.send(req);
String ResponseJsonString = res.getBody();
map<string,string> attachment= (map<string,string>)JSON.deserializeUntyped(ResponseJsonStringJIra);
system.debug('after response...'+attachment);
system.debug('after response...'+attachment.get('id'));
System.debug('ResponseJsonStringJIra'+ResponseJsonStringJIra);
JSONParser parser = JSON.createParser(ResponseJsonStringJIra);
}
}
catch(System.CalloutException e)
{
System.debug('Callout error: '+ e);
}
}
I appreciate any help.
My requirement is to create a Issue with an attchment on a case object using Rest Api.
I am able to create a Issue into jira by passing my credentials and unable to move forward in attaching a file into jira.
Please help me.
Thanks
Uday
Pcon, I want to add a file on case object and pass it on to jira.
Here is my code:
global class CaseAttachment {
@future(callout=true)
public static void uploadFile(Blob file_body, String file_name,string Jiraid){
String boundary = ' ';
String header = '--'+boundary+'\r\nContent-Disposition: form-data; name="file"; filename="'+file_name+'"\r\nContent-Type: application/octet-stream';
String footer = '\r\n--'+boundary+'--';
String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
// String file_name= '';
String endpoint = 'http://jira url/rest/api/2/issue/{JiraId}/attachments-addAttachment';
while(headerEncoded.endsWith('='))
{
header+=' ';
headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
}
String bodyEncoded = EncodingUtil.base64Encode(file_body);
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
Blob bodyBlob = null;
String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
if(last4Bytes.endsWith('='))
{
Blob decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
HttpRequest tmp = new HttpRequest();
tmp.setBodyAsBlob(decoded4Bytes);
String last4BytesFooter = tmp.getBody()+footer;
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded.substring(0,bodyEncoded.length()-4)+EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
}
else
{
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
}
HttpRequest requ = new HttpRequest();
requ.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
requ.setMethod('POST');
requ.setEndpoint(endpoint);
requ.setBodyAsBlob(bodyBlob);
requ.setCompressed(true);
requ.setTimeout(120000);
Http http1 = new Http();
HTTPResponse resp = http1.send(requ);
}
}
NOTE: When including code please use the "Add a code sample" button (icon <>) to increase readability and make referencing code easier.
Here is the Response :
|USER_DEBUG|[75]|DEBUG|Response System.HttpResponse[Status=Not Found, StatusCode=404]
[1] https://confluence.atlassian.com/display/JIRAKB/How+to+attach+an+attachment+in+a+JIRA+issue+using+REST+API
This XML file does not appear to have any style information associated with it. The document tree is shown below".
<status>
<status-code>404</status-code>
<message>
null for uri: https://xxx.atlassian.net/rest/api/latest/issue/key/attachment-addAttachments
</message>
</status>
Can i have the referal code to get the attachments from JIRA to saleforce ,eg: when a attachment is created in JIRA then the attachment should be created in salesforce under case object.
can you Please help out.
Thanks ,
Ram
Thanks for the attachment solutions. Its really good.
Could you please let me know how can i send JIRA comment status back to Salesforce. I would like to see if JIRA comment can be updated in Salesforce. Your help is really appreciated. Thanks
[1] https://developer.atlassian.com/jiradev/jira-apis/webhooks/
[2] http://www.wadewegner.com/2013/03/creating-anonymous-rest-apis-with-salesforce-com/
[3] https://trailhead.salesforce.com/apex_integration_services/apex_integration_webservices
Could you please let me know . how can i fetch the attachment from JIRA and Save it to Salesforce under Case Record? Your help is really appreciated.
- You can use Webhooks in Jira to alert Salesforce that there is a new attachment, then download and insert it. I have some caveats earlier in this thread about how to do taht
- You can stand up a secondary system that polls Jira for new attachments and then inserts them directly into Salesforce
- You have a scheduled Apex job that polls Jira for new attachments and inserts them
If I were doing this for an enterprise org, I would look at doing this with a secondary system. The limitations around runtime and such would make it much easier to do. You could do a hybrid of 1 & 2 and use the webhook to notify the secondary system and then have that push into Salesforce.[1] http://bobbuzzard.blogspot.com/2011/07/creating-attachments-in-apex-code.html
I am good in Apex. But not clear which JIRA REST API we need to call to grab the attachment from JIRA. I have attachment Id in Webhook.
Hi Pcon,
Your above stuff was very helpfull. Now I need to get Attachment Blob from JIRA Instance to SF Instance, I got attachment ID
from JIRA Webhook and when I request for attachmnet with attachment Id, reponse back to SF as :
ResPonse: {"id":10505,"self":"https://convertio.atlassian.net/rest/api/2/attachment/10505","filename":"04e4277617e779bdac7c27a2aaab455d_png-small-medium-large-blue-cloud-clipart-png_600-358.png","author":{"self":"https://convertio.atlassian.net/rest/api/2/user?username=asinghal","key":"asinghal","accountId":"557058:eae85e9b-dbb3-4f16-99cd-142549243580","name":"asinghal","avatarUrls":{"16x16":"https://avatar-cdn.atlassian.com/012b7836666487f4e6d10372273e2d00?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F012b7836666487f4e6d10372273e2d00%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue","24x24":"https://avatar-cdn.atlassian.com/012b7836666487f4e6d10372273e2d00?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F012b7836666487f4e6d10372273e2d00%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue","32x32":"https://avatar-cdn.atlassian.com/012b7836666487f4e6d10372273e2d00?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F012b7836666487f4e6d10372273e2d00%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue","48x48":"https://avatar-cdn.atlassian.com/012b7836666487f4e6d10372273e2d00?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F012b7836666487f4e6d10372273e2d00%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue"},"displayName":"Atul Singhal","active":true},"created":"2017-05-16T14:55:19.606+0530","size":61164,"mimeType":"image/png","properties":{},"content":"https://convertio.atlassian.net/secure/attachment/10505/04e4277617e779bdac7c27a2aaab455d_png-small-medium-large-blue-cloud-clipart-png_600-358.png","thumbnail":"https://convertio.atlassian.net/secure/thumbnail/10505/04e4277617e779bdac7c27a2aaab455d_png-small-medium-large-blue-cloud-clipart-png_600-358.png"}
In ResPonse Content have url for this attachment but link request need JIRA login.
Code:
public class JIRA_ATTACHMENT_PULL {
public static void jiraPullAtt(String strIdAtt,String StrCaseId,String StrJIRA_Key)
{
UtilClass objUtilClass=new UtilClass();
UtilClass.ConnectionWrp obj=objUtilClass.getConnectionDetail();
Http h2 = new Http();
HttpRequest req2 = new HttpRequest();
String auth_header2 = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(obj.strusername + ':' + obj.strpassword));
req2.setHeader('Content-Type','application/json');
req2.setHeader('accept','application/json');
req2.setHeader('Authorization',auth_header2);
req2.setMethod('GET');
req2.getBodyAsBlob();
string sEndPoint2='https://convertio.atlassian.net/rest/api/2/attachment/'+strIdAtt;
system.debug('----- sEndPoint: '+ sEndPoint2);
req2.setEndpoint(sEndPoint2);
req2.setTimeout(120000);
HttpResponse res2 = h2.send(req2);
system.debug('---- req: ' + req2);
system.debug('---- ResPonse: ' + res2.getBody());
JSONParser parser = JSON.createParser(res2.getBody());
string JIRASFDCAttachment='';
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'content')) {
// Get the value.
parser.nextToken();
JIRASFDCAttachment = parser.getText();
}
}
JSONParser parser2 = JSON.createParser(res2.getBody());
string JIRASFDCAttFileName='';
while (parser2.nextToken() != null) {
if ((parser2.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser2.getText() == 'filename')) {
// Get the value.
parser2.nextToken();
JIRASFDCAttFileName= parser2.getText();
}
}
JIRA_Incoming_Attchment__c ObjJIA= new JIRA_Incoming_Attchment__c();
ObjJIA.name=StrJIRA_Key+' | '+JIRASFDCAttFileName;
ObjJIA.Case__c= StrCaseId;
ObjJIA.JIRA_Attachment_URL__c=JIRASFDCAttachment;
Insert ObjJIA;
system.debug('ObjJIA'+ObjJIA);
System.debug('JIRASFDCAttachment: '+JIRASFDCAttachment);
System.debug('JIRASFDCAttFileName: '+JIRASFDCAttFileName);
}
}
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
You will of course have to set your own parentId to get this to work. There are obviously many more fields being returned in both the Issue and the Attachment, but I've just put the minimum in here so I could get the data needed to write it out to the object.
Hi pcon,
Thanks for your prompt reply. Its really appreciated.
In above code when we request and getting a response that content only URL which is a secure URL not a public so when we click on this to open that attachment that asks for JIRA login.
07:35:24:141 USER_DEBUG [44]|DEBUG|iJira_Issue:[fields=Jira_Fields:[attachment=(Jira_Attachment:[content=https://convertio.atlassian.net/secure/attachment/11301/images.jpg, filename=images.jpg], Jira_Attachment:[content=https://convertio.atlassian.net/secure/attachment/11300/JIRA_logo.svg.png, filename=JIRA_logo.svg.png], Jira_Attachment:[content=https://convertio.atlassian.net/secure/attachment/11302/KEY.txt, filename=KEY.txt], Jira_Attachment:[content=https://convertio.atlassian.net/secure/attachment/11219/Salesforce_Superhero_bmqitn.png, filename=Salesforce_Superhero_bmqitn.png], Jira_Attachment:[content=https://convertio.atlassian.net/secure/attachment/11218/Salesforce_Superhero_bmqitn.png, filename=Salesforce_Superhero_bmqitn.png])], id=11011, key=PGCRUSCW-14]
As I wish to do save the attachment in SF Instance so we can open again that attachment without Login to JIRA.
Is there any way to get BLOB value of attachments Instant of URL.
@pcon really I need your help on this, Your help is really appreciated.
The endpoint you have set is the attachment not the Jira. The code provided deserializes the returning JSON into and object that can be used. Line 38 won't work because there is no Blob value, it's JSON text data. Additionally, you should remove the accept and content-type headers. You're not sending anything, so Content-Type isn't used and appart from the initial request for the full Jira data, you're not going to be accepting back JSON, you'll be accepting back whatever the attachment type is. If you want to include the accept header, you will need to see if Jira returns that data in it's attachment list, add it to the Jira_Attachment object and then add that to the headers for each attachment.
Hi Pcon,
First of all, Thank you very much for all your help. I really appreciate all your effort and giving your valuable time...
I need one more suggestion on getting JIRA Attachment BLOB value in SF instance.
As I followed your suggestions and prepare below code for this purpose but the instant of Blob value res1.getBodyAsBlob() // getting null value here
@Pcon please help me on this because of this unable to get the scenario of getting JIRA attachment in SF Attachment.
We need to see where it's trying to redirect you to
@Pcon,
Yes, You are right that was redirecting on JIRA login page, So added authentication header in request Now response code receive:
StatusCode: 200 and Status OK but response body is ' ' and ResponseAsblob is equaled to null.
@pcon please help me on this in postman when I did same authentication and request, receive response as body but for request from salesforce receiving null, Unable to understand why it is its working f9 in postman testing but here response receive status code is 200 and status is OK but body in null.
You're missing the space after Basic. Oddly enough, with a broken Authorization header, atlassian's API returns a 200. That's why in my example above, I reused variables whenever possible. There is no reason to create new ones, they just cause confusion.
I think that if you fix that, you'll see that you are still getting a 302. If you were to use cURL to get that URL with your Authorization header, I bet that you would see that it is a 302 and a redirect to a different location. That location is probably a hosted server that is not under the same atlassian domain.
Becuase of this, you'd have to do something like
I would also recommend that you change your password immediately since the Basic hash that you have in this forum post could be used to access your Jira account even without knowing the username or password.
Hi Pcon,
I really appreciate all your effort and giving your valuable time.
You are amazing!!
I did the same as you suggest me and able to get download an attachment through a link received in Location Header which has token value.
But for next to save the attachment in salesforce need BLOB value so as you did in above code i did same but getting.
System.CalloutException:
Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://media-api.atlassian.io/file/115ff7a4-af1d-45d2-a0c9-b1abccf58d15/binary?token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI5YjI2YjVjMi1hM2U5LTRlMGMtYjRlYy0yNGM0MTU2ZjQzMTkiLCJhY2Nlc3MiOnsidXJuOmZpbGVzdG9yZTpmaWxlOjExNWZmN2E0LWFmMWQtNDVkMi1hMGM5LWIxYWJjY2Y1OGQxNSI6WyJyZWFkIl19LCJleHAiOjE0OTczNjk3ODgsIm5iZiI6MTQ5NzM2OTEyOH0.jq0EyEeBIXCfq9qYNwQYskvleJhAM80bOsehw0WlXJo&client=9b26b5c2-a3e9-4e0c-b4ec-24c4156f4319&name=images.jpg
please help me on this so I can get blob value after requesting
EndPoint[https://media-api.atlassian.io/file/115ff7a4-af1d-45d2-a0c9-b1abccf58d15/binary?token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI5YjI2YjVjMi1hM2U5LTRlMGMtYjRlYy0yNGM0MTU2ZjQzMTkiLCJhY2Nlc3MiOnsidXJuOmZpbGVzdG9yZTpmaWxlOjExNWZmN2E0LWFmMWQtNDVkMi1hMGM5LWIxYWJjY2Y1OGQxNSI6WyJyZWFkIl19LCJleHAiOjE0OTczNjk3ODgsIm5iZiI6MTQ5NzM2OTEyOH0.jq0EyEeBIXCfq9qYNwQYskvleJhAM80bOsehw0WlXJo&client=9b26b5c2-a3e9-4e0c-b4ec-24c4156f4319&name=images.jpg]
Please, its really needed for my pending scenario saving JIRA attachment in salesforce attachment.
[1] https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_remote_site_settings.htm
Thanks! Pcon you are superb really so much happy finally.
I really appreciate all your effort and for giving your valuable time. please share your blogger link so I can connect to you for future.
Amazing Job! Thanks once again @Pcon
@ Pcon thanks for your help really I appreciate your effort,
Now, I am facing issue for sending attachment from salesforce to JIRA as JIRA may have attachment size 10MB and In Salesforce easily possible for 15MB so when I tried for sending an attachment more then 3MB facing [Apex Heap Size too large] error ,
Please help me on this how I can sent attachment more than 3 MB from salesforce to JIRA.
Code:
Its working fine for till 2MB file but if attachment size more then 2.5 MB facing that issue [Apex Heap Size is too large].
@Pcon once again I hope for your help on this.
Hi Pcon,
So can you help me on this, Like if attachment size is too large and facing that issue so, can send a comment msg to JIRA So that in JIRA get confirmation attachment not Uploaded in JIRA.
Because I tried for that if I get this error I will send a comment but error does not get handled by try catch.
The code is taken from blog: 7 Ways to Integrate Salesforce and Jira (https://www.peeklogic.com/article/7-ways-to-integrate-salesforce-and-jira/)