Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Correct formatting to Create Project using REST API in Scriptrunner

Robert Wen_Cprime_
Community Champion
April 27, 2021

I'm trying to create a script that creates a project as a post-function in Scriptrunner Cloud.  I'm using the REST API, but I'm getting 400 errors because of some minor change I need to do.  Code snippet is below.  What am I missing?

def result = post("/rest/api/2/project")
.header('Content-Type', 'application/json')
.body([
key: newProjectKey,
name: newProjectName,
projectTypeKey: newProjectTypeKey,
projectTemplateKey: newProjectTemplateKey,
leadAccountId: newLeadAccountId
])
.asObject(Map)
if (result.status == 201) {
return 'Success'
} else {
return "${result.status}: ${result.body}"
}

 What am I missing?  Thanks in advance!

2 answers

0 votes
Max Lim _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 11, 2021

I can confirm the Rest API you used is correct. Perhaps, you want to programmatically retrieve the account id with email like so:

def newLead = get("/rest/api/3/user/search")
.header('Content-Type', 'application/json')
.queryString("query", "xxx@example.com")
.asObject(List)
.body
.first() as Map

def result = post("/rest/api/2/project")
.header('Content-Type', 'application/json')
.body([
key: newProjectKey,
name: newProjectName,
projectTypeKey: newProjectTypeKey,
projectTemplateKey: newProjectTemplateKey,
leadAccountId: newLead.accountId
])
.asObject(Map)

if (result.status == 201) {
return 'Success'
} else {
return "${result.status}: ${result.body}"
}

This community question also discuss finding account id for your own account.

0 votes
Robert Wen_Cprime_
Community Champion
April 27, 2021

I think the main problem is leadAccountId.  I'm not using the correct format.  I think I have to use dot notation to get at accountId inside the field.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events