You need to sign in to do that
Don't have an account?
Katie DeLuna 7
trigger to execute on case status = 'Escalated'
I can't seem to update my trigger so that it only creates a ticket in Jira (webservice connector) when the case status equals "Escalated". Can someone help?
trigger SynchronizeWithJIRAIssue on Case (after insert) {
system.debug('trigger!');
//Identify profile name to be blocked from executing this trigger
String JIRAAgentProfileName = 'JIRA Agent2';
List<Profile> p = [SELECT Id FROM Profile WHERE Name=:JIRAAgentProfileName];
//Check if specified Profile Name exist or not
if(!p.isEmpty())
{
//Check if current user's profile is catergorized in the blocked profile
if(UserInfo.getProfileId()!= String.valueOf(p[0].id))
{
for (Case c : Trigger.new) {
system.debug('inside case ' + c.CaseNumber);
//Define parameters to be used in calling Apex Class
String jiraURL = 'http://xxxxxxx';
String systemId = '2';
String objectType ='Case';
String objectId = c.id;
String projectKey = 'xxx';
String issueType = 'xx';
System.debug('\n\n status is escalated');
//Execute the trigger
JIRAConnectorWebserviceCallout.CreateIssue(jiraURL, systemId ,objectType,objectId,projectKey,issueType);
}
}
}
}
trigger SynchronizeWithJIRAIssue on Case (after insert) {
system.debug('trigger!');
//Identify profile name to be blocked from executing this trigger
String JIRAAgentProfileName = 'JIRA Agent2';
List<Profile> p = [SELECT Id FROM Profile WHERE Name=:JIRAAgentProfileName];
//Check if specified Profile Name exist or not
if(!p.isEmpty())
{
//Check if current user's profile is catergorized in the blocked profile
if(UserInfo.getProfileId()!= String.valueOf(p[0].id))
{
for (Case c : Trigger.new) {
system.debug('inside case ' + c.CaseNumber);
//Define parameters to be used in calling Apex Class
String jiraURL = 'http://xxxxxxx';
String systemId = '2';
String objectType ='Case';
String objectId = c.id;
String projectKey = 'xxx';
String issueType = 'xx';
System.debug('\n\n status is escalated');
//Execute the trigger
JIRAConnectorWebserviceCallout.CreateIssue(jiraURL, systemId ,objectType,objectId,projectKey,issueType);
}
}
}
}
As I said if this way of calling web service may result in breach of governor limits. Sending the cases in a list would be a better solution. But for that the external web service (that is exposed by JIRA) should have a corresponding operation.
Thanks,
Kaustav
All Answers
If that does not do it, I have a few questions:
1. is JIRAConnectorWebserviceCallout doing the web services callout? You cannot make a web services call directly from a trigger.
2. Are any of your debug statements firing? If so, which ones?
3. Are all of the cases hitting this trigger guarenteed to be escalated? I am not seeing anywhere were you evaluate whether there is a escalated flag on a case.
As for the status check = you need to put an if condition.
The code should look something like this
First select all the cases that have status as escalated. Add their ids in one list and then send them over to Jira.
However you first need to make sure that the the external system has a compatible operation that accepts more than one case ids.
Let me know if this helps.
Thanks,
Kaustav
I do have a class created, then have it reach out to the trigger. I originally had the "escalated' validtion in there, but removed it because it's wasn't working. Here is the class/callout:
global class JIRAConnectorWebserviceCallout {
@future (callout=true)
WebService static void createIssue( String jiraURL, String systemId, String objectType, String objectId, String projectKey, String issueType) {
System.debug('creating issue for ' + jiraURL);
//Set your username and password here
String username = 'xxxxx';
String password = 'xxxxxxx';
//Construct HTTP request and response
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
//Construct Authorization and Content header
Blob headerValue = Blob.valueOf(username+':'+password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setHeader('Content-Type','application/json');
req.setHeader('Content-Length','0');
req.setHeader('Project','xxx');
req.setHeader('Summary','xxx');
req.setHeader('Reporter','xxx');
req.setTimeout(30000);
//Construct Endpoint
//String endpoint = 'http://jira.xxxxxxxxx.com/plugins/servlet/customware/connector/issue/2/Case/create.action?id=' + objectId +
// '&Project=LEV&Reporter=xxxxxxxxx&Summary=test&issueType=27';
String endpoint = 'http://jira.optionscity.com/rest/customware/connector/1.0/2/Case/' + objectId + '/issue/create.json';
String data = '{"project":"LEV","issueType":"27"}';
system.debug('url is, ' + endpoint);
system.debug('data is, ' + data);
//Set Method and Endpoint and Body
req.setMethod('POST');
req.setEndpoint(endpoint);
req.setBody(data);
try {
//Send endpoint to JIRA
res = http.send(req);
} catch(System.CalloutException e) {
System.debug(res.toString());
System.debug('Callout error: '+ e);
System.debug(res.toString());
}
}
}
Is this going to affect the trigger suggestion from @kaustav goswami?
Thank you!
trigger SynchronizeWithJIRAIssue on Case (after insert) {
system.debug('trigger!');
//Identify profile name to be blocked from executing this trigger
String JIRAAgentProfileName = 'JIRA Agent2';
List<Profile> p = [SELECT Id FROM Profile WHERE Name=:JIRAAgentProfileName];
//This is new code added Sept 25
List<Case> escCases = new List<Case>();
List<String> caseIdsToSend = new List<String>();
//Check if specified Profile Name exist or not
if(!p.isEmpty())
{
//This is new code added Sept 25
for (Case c : Trigger.new) {
if(c.Status == 'Escalated'){
escCases.add(c);
}
}
//New code added Sept 25. consolidate all case ids to send over to the web service
if(!escCases.isEmpty()){
for(Case c : escCases){
caseIdsToSend.add(c.Id);
}
}
// call webservice
//Check if current user's profile is catergorized in the blocked profile
if(UserInfo.getProfileId()!= String.valueOf(p[0].id))
{
for (Case c : Trigger.new) {
system.debug('inside case ' + c.CaseNumber);
//Define parameters to be used in calling Apex Class
String jiraURL = 'http://jira.xxxxxxx';
String systemId = '2';
String objectType ='Case';
String objectId = c.id;
String projectKey = 'LEV';
String issueType = '27';
System.debug('\n\n status is escalated');
//Execute the trigger
JIRAConnectorWebserviceCallout.CreateIssue(jiraURL, systemId ,objectType,objectId,projectKey,issueType);
}
}
}
}
Please understand that when you iterate over triiger.new all the cases in the trigger's context will be iterated over.
You can do two things -
Either iterate over escCases list like
Or you can put in a check in the loop for the case status and use your original code
But I have mentioned before the design would have been more effective if the external service accepted a list of case ids to create the cases.
Let me know if this helps.
Thanks,
Kaustav
trigger SynchronizeWithJIRAIssue on Case (after insert) {
system.debug('trigger!');
//Identify profile name to be blocked from executing this trigger
String JIRAAgentProfileName = 'JIRA Agent2';
List<Profile> p = [SELECT Id FROM Profile WHERE Name=:JIRAAgentProfileName];
//new code
List<Case> escCases = new List<Case>();
List<String> caseIdsToSend = new List<String>();
//Check if specified Profile Name exist or not
if(!p.isEmpty())
{
//Check if current user's profile is catergorized in the blocked profile
if(UserInfo.getProfileId()!= String.valueOf(p[0].id))
{
for (Case c : Trigger.new) {
//new code to check for escalated
if(c.Status == 'Escalated'){
escCases.add(c);
}
}
// consolidate all case ids to send over to the web service
if(!escCases.isEmpty()){
for(Case c : escCases){
caseIdsToSend.add(c.ID);
}
}
//call webservice
system.debug('inside case ' + c.CaseNumber);
//Define parameters to be used in calling Apex Class
String jiraURL = 'http://jira.vvvvvvvv5';
String systemId = '2';
String objectType ='Case';
String objectId = c.id;
String projectKey = 'LEV';
String issueType = '27';
System.debug('\n\n status is escalated');
//Execute the trigger
JIRAConnectorWebserviceCallout.CreateIssue(jiraURL, systemId ,objectType,objectId,projectKey,issueType);
}
}
}
As I said if this way of calling web service may result in breach of governor limits. Sending the cases in a list would be a better solution. But for that the external web service (that is exposed by JIRA) should have a corresponding operation.
Thanks,
Kaustav
trigger SynchronizeWithJIRAIssue on Case (after insert, after update){
......
......
}
Thanks,
Kaustav
Here is my code:
trigger CreateWithJIRAIssue on Case (after insert) {
system.debug('trigger!');
//Identify profile name to be blocked from executing this trigger
String JIRAAgentProfileName = 'JIRA Agent2';
List<Profile> p = [SELECT Id FROM Profile WHERE Name=:JIRAAgentProfileName];
//new code
List<Case> escCases = new List<Case>();
//Check if specified Profile Name exist or not
if(!p.isEmpty()){
//Check if current user's profile is catergorized in the blocked profile
if(UserInfo.getProfileId()!= String.valueOf(p[0].id)){
for (Case c : Trigger.new){
//new code to check for escalated
// all the cases that have status as 'Escalated' is added to a list called escCases
if(c.Status == 'Escalated'){
escCases.add(c);
}
}
if(escCases != null && escCases.size()>0){
// now iterate over the selected cases and call the web service
for(Case c : escCases){
//call webservice
system.debug('inside case ' + c.CaseNumber);
//Define parameters to be used in calling Apex Class
String jiraURL = 'http://jira';
String systemId = '2';
String objectType ='Case';
String objectId = c.id;
String projectKey = 'LEV';
String issueType = '27';
System.debug('\n\n status is escalated');
//Execute the web service call
//warning - may result in breach of governer limits
//sending cases in a list would be a better solution, but jira should have a corresponding operation
JIRAConnectorWebserviceCallout.CreateIssue(jiraURL, systemId ,objectType,objectId,projectKey,issueType);
}
}
}
}
}
Thank you!
taken from "7 Ways to Integrate Salesforce and Jira (https://www.peeklogic.com/article/7-ways-to-integrate-salesforce-and-jira/)"