Post

3 followers Follow
0
Avatar

How to create Expenses using REST API and createAndRetrieve

My objective is to create one Expense Sheet per project and add Expense items per month from an external system.

PART 1: CREATING A NEW EXPENSE SHEET AND SET DEFAULT PROJECT
curl -H "Authorization: Session f45185ff-c5cf-46f7-a40c-0f6ce9d9b958_23596289"https://api2.clarizen.com/v2.0/services/data/createAndRetrieve -d "{entity:{Id:'/ExpenseSheet',Description:'New Expense Sheet Created by Clarizen API',Date:'2016-06-30',project:{externalid:'/Project/68ca3ebilvkxchy11qddm8mid4243'}},fields:['CreatedOn','ExternalId']}"

However, I get below error
{"errorCode":"General","message":"Can not convert value '{\r\n \"externalid\":\"/Project/68ca3ebilvkxchy11qddm8mid4243\"\r\n}' to type 'Entity' (Field name: 'Value')","referenceId":"1U3LupwnLVgGH8jAFMiVmd"}

Worth to mention is that a separate call on the object itself works:
curl -H "Authorization: Session f45185ff-c5cf-46f7-a40c-0f6ce9d9b958_23596289"https://api2.clarizen.com/v2.0/services/data/objects/ExpenseSheet/1m455c1pq0dw4fkr0pzubyx4t44 -d "{project:'/Project/68ca3ebilvkxchy11qddm8mid4243'}"

PART 2: ADDING AN EXPENSE UNDER AN EXPENSE SHEET
curl -H "Authorization: Session f45185ff-c5cf-46f7-a40c-0f6ce9d9b958_23596289"https://api2.clarizen.com/v2.0/services/data/createAndRetrieve -d "{entity:{Id:'/Expense',Description:'New Expense Created by Clarizen API',DateIncurred:'2016-06-30',LocalAmount:{currency:'JPY',value:1234567.0},ExpenseSheet:{externalid:'/ExpenseSheet/1m455c1pq0dw4fkr0pzubyx4t44'}},fields:['CreatedOn','ExternalID']}"

This time as well, I get an error
{"errorCode":"General","message":"Can not convert value '{\r\n \"externalid\":\"/ExpenseSheet/1m455c1pq0dw4fkr0pzubyx4t44\"\r\n}' to type 'Entity' (Field name: 'Value')","referenceId":"1hm4KeLo5f3vsBTvafVIfr"}

If I don't set Expense Sheet when creating the Expense item, a new Expense Sheet will automatically created as Parent. And, it is not possible to update the field 'ExpenseSheet' after the Expense is created.

Any suggestions about how I can achieve below objective is highly thankful.
1. Create a new Expense Sheet and set project name (called Default Project)
2. Add new Expenses to the created Expense Sheet

Mattias Hallberg Answered

Please sign in to leave a comment.

4 comments

2
Avatar

For the Part 1, you are setting the project reference as an object and it has to be a plain string (exactly what you set in the separate call you mention). That is, this is wrong:

{

...

Project: {externalid: '/Project/68ca3ebilvkxchy11qddm8mid4243'},

...

}

The expected format is:

{

...

Project: '/Project/68ca3ebilvkxchy11qddm8mid4243',

...

}

Fernando Borrego Polo 2 votes
Comment actions Permalink
2
Avatar

For the second part, the problem is exactly the same, you need to set the expense sheet field as:

 

{

...

ExpenseSheet; '/ExpenseSheet/1m455c1pq0dw4fkr0pzubyx4t44',

...

}

Fernando Borrego Polo 2 votes
Comment actions Permalink
0
Avatar

the "id" in the JSON response body, is it just the "ExternalID" of the entity with type information added?

Bo Gao 0 votes
Comment actions Permalink