performing HTTP Post from scriptrunner

I created a custom field called 'E-mail Address' . After transititioning an issue to a certain state I want to send an HTTP Post request to a certain url if an email is listed in that issue.

Currently I am checking the empty email condition with:

ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Contact E-mail") != ""

 

Does anybody have an Idea how I can send the value of the email value to an url: www.exampleurl.com with argument email=issueemail

 

Thanks!

1 answer

0 vote
Tarun Sapra Community Champion Oct 04, 2017

Here's example of working code -

https://community.atlassian.com/t5/Product-Apps-questions/help-with-REST-using-groovy/qaq-p/17649

 

To check if field is null or present you can do that with "if" statement in groovy

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def email = customFieldManager.getCustomFieldObjectByName('Email');
// this is the check
if(issue.getCustomFieldValue(email)) {
//code here for REST POST call
} else {
//do nothing
}

Hi Tarun,

I combined the suggestion you posted with a default REST Post groovy example. I am using the following script, but the importing of the classes/dependencies seem to fail.

 

import com.atlassian.jira.ComponentAccessor;
import groovyx.net.http.HTTPBuilder;
import static groovyx.net.http.ContentType.URLENC;

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def email = customFieldManager.getCustomFieldObjectByName('Email Adress');

// this is the check
if(issue.getCustomFieldValue(email))
{
    def http = new HTTPBuilder( 'http://www.example.com/Scripts/test.php' );
    def postBody = [username: 'bob']; // will be url-encoded
 
    http.post( path: '/', body: postBody, requestContentType: URLENC )
    {
        resp -> println "POST Success: ${resp.statusLine}";
        assert resp.statusLine.statusCode == 201;
    }
}
else
{
    //do nothing
}

The debug/output/error console lists the following errors after running the code:

Script2.groovy: 1: unable to resolve class com.atlassian.jira.ComponentAccessor
@ line 1, column 1.
   import com.atlassian.jira.ComponentAccessor;
   ^

Script2.groovy: 2: unable to resolve class groovyx.net.http.HTTPBuilder
@ line 2, column 1.
   import groovyx.net.http.HTTPBuilder;
   ^

Script2.groovy: 3: unable to resolve class groovyx.net.http.ContentType
@ line 3, column 1.
   import static groovyx.net.http.ContentType.URLENC;
   ^

3 errors



settings.JPG

 Do you have any idea what is going on here?

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

849 views 2 9
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you