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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published 6 hours ago in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

27 views 0 2
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