Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Using this routine, you can for instance create a new ticket in the specified project based on certain conditions, and can pass information for the additional fields to be filled in for this ticket too.

The following example shows how to create a JSON object, transmit it to an external application, and receive data in response. The example simulates creating a user account on another system and receiving back the user ID. This ID could then be stored in a custom field in Jira creating a reference.

Part 1 - Setting Up

This section of code defines the structs that will ultimately determine the JSON structure. The JSON structure is typically defined by the external systems API.

Code Block
languagejava
themeEclipse
linenumberstrue
//Define structs
struct property {
    string property;
    string value;
}

struct properties {
   property [] properties;
}

struct returnData {
    string status;
    string contactID;
}

Part 2 - Adding Data

This section of code creates the structs and arrays and adds data to them.

Code Block
languagejava
themeEclipse
linenumberstrue
//Create array/struct
property [] propertyArray;
property p;
properties newContact;

//Add data to array/struct
p.property = "firstname";
p.value = "Merimas";
propertyArray += p;

p.property = "lastName";
p.value = "Fairbairn";
propertyArray += p;

p.property = "email";
p.value = "merimas_fairbairn@aol.com";
propertyArray += p;

p.property = "company";
p.value = "Goodchild";
propertyArray += p;

newContact.properties += propertyArray;

Part 3 - Sending the Post

This section of code sets up the request, posts the data to the external system, and gets the response.

Code Block
languagejava
themeEclipse
linenumberstrue
//Create request
HttpRequest request;
HttpHeader header = httpCreateHeader("Content-Type", "application/json");
request.headers += header;

//Post data and get response
returnData result = httpPost("https://api.somewebsite.com/contacts/v1/contact/", request, newContact);

//Return ID of newly created user
return result.contactID;

Complete Script

Code Block
languagejava
themeEclipse
linenumberstrue
collapsetrue
//Define structs
struct property {
    string property;
    string value;
}

struct properties {
   property [] properties;
}

struct returnData {
    string status;
    string contactID;
}

//Create array/struct
property [] propertyArray;
property p;
properties newContact;


//Add data to array/struct
p.property = "firstname";
p.value = "Merimas";
propertyArray += p;

p.property = "lastName";
p.value = "Fairbairn";
propertyArray += p;

p.property = "email";
p.value = "merimas_fairbairn@aol.com";
propertyArray += p;

p.property = "company";
p.value = "Goodchild";
propertyArray += p;

newContact.properties += propertyArray;

//Create request
HttpRequest request;
HttpHeader header = httpCreateHeader("Content-Type", "application/json");
request.headers += header;

//Post data and get response
returnData result = httpPost("https://api.somewebsite.com/contacts/v1/contact/", request, newContact);

//Return ID of newly created user
return result.contactID;

See also

Filter by label (Content by label)
showLabelsfalse
showSpacefalse
cqllabel = "http_support"
labelsjira_project_routine

...