How do I create a task in a section of a project using the java Library

I want to create a task inside a section of a project.

Therefore I need a few things:

  • A way to get the id of the section
    • I can’t find it on the UI (projectIds and taskIds for example can be seen in the URL)
    • There is an endpoint to get sections of a project and the java lib example seems to support this see https://developers.asana.com/docs/get-sections-in-a-project
      • Unfortunately my com.asana.Client doesn’t seem to have a sections member see [1] :thinking: (using the latest java release 10.1)
  • A way to assign a task to a section
    • Creating a task in a section as described here developers.asana.com/docs/add-task-to-section has the same problem for me - I don’t seem to have the sections member in the Client

Checking the github source code for the java client - it also doesn’t have a section member see https://github.com/Asana/java-asana/blob/master/src/main/java/com/asana/Client.java
There is a ProjectMemberships member but this only gives you read options as far as I can tell.

[1]

Any help would be very much appreciated,
Best Regards
Ronald Findling

Noone having any idea what I’m doing wrong? This seems to me like this support is missing in the java client atm …

Lol I posted with our asana test user here - sry this is not a bot but a real person asking ^^

@Ross_Grambo, what do you think? I thought the clients were pretty up-to-date?

1 Like

Hello @Ronald_Findling and your test user @Asana2 :slight_smile:

The client libraries don’t get nearly as much love as they deserve. I’m surprised this is the first I’ve heard that sections are missing, and I now see an issue back in March on the github that I must have missed as well. :frowning:

That being said, I went ahead and added it to the Java client. It’ll be on the next deploy which should go out either later today or tomorrow (depends how fast maven is).

If you need a fix before then, you could create a new instance of the resources.Sections class and go from there, or manually send a request.

1 Like

@Ross_Grambo thx for the quick update!

Point 1:
I updated to the lates java client 10.3 and can now see the methods to create and fetch sections over the api -> this solves that part :slight_smile:

Point 2:
I don’t see a way to create a task in a section directly with the java library in contrast to some forum posts showing how to do this with the the rest interface see here or here.
Strangely enough I could not find this in the REST documentation for the task creation here.

So I only see the 2 step option to create the task in a project and then in a second step assign it to a project see here.

Is there a better way or do I have to make these 2 calls?

Best Regards,
Ron

Hey @Ronald_Findling,

Honestly, I wasn’t sure so I tried a few things and it looks like this works:

POST /tasks

{
	"data": {
		"name":"Test",
		"projects": "1234",
		"memberships": [
			{
				"project": "1234",
				"section": "12345"
			}
		]
	}
}

Perhaps I’m crazy, but give that a try and let me know if it works for you as well :slight_smile:

Hey @Ross_Grambo
My second point is purely about the java client support for doing it - not REST only :slight_smile:
Even though it seems the REST interface lacks the documentation for what you just wrote.

Good point! I’ll update the docs to include memberships as a writable field.

In java-asana you can construct json like:

    HashMap<String, String> memberships = new HashMap<String, String>();
    memberships.put("project", "1234");
    memberships.put("section", "12345");

    HashMap[] memArray = new HashMap[1];
    memArray[0] = memberships;

    Task t = client.tasks.createTask()
            .data("name", "java test")
            .data("projects", "1234")
            .data("memberships", memArray)
            .execute();

You could and probably should use the Memberships class instead of a generic hashmap.

Hey @Ross_Grambo thx for the reply.

The pure REST call works for me when doing it with a tool/CURL.

After a bit of trying I also got your java example to work but is a bit of a hack (modifications see [2])

Regarding your comment to make it less hacky - I would not know how to use the Membership class to full full the REST interface with the current java-client-lib - do you have an example for this aswell ^^
I tried:

  • A nearly empty Membership entity (just gid of project and section set)
  • Constructing a full Membership entity (failed as I don’t know how to create a Task with the section data see [1]

[1]

public static class Membership {
        public Project project;
        public Task section;
    }

[2]
The memArray should be a list as the expected JSON is that memberships is a list of object

HashMap<String, String> memberships = new HashMap<String, String>();
memberships.put("project", "1234");
memberships.put("section", "12345");

List<Object> memArray = new ArrayList<>()
memArray.add(memberships)

Task t = client.tasks.createTask()
    .data("name", "java test")
    .data("projects", "1234")
    .data("memberships", memArray)
    .execute();

Ah I forgot that our java models don’t work for writes. So I believe I was incorrect in saying there is a cleaner way… :frowning:

For now I think we’re stuck explicitly declaring an array of hashmaps and using that :confused:

Ah ok - well in that case this is how good we get at the moment.
Thx for the quick support @Ross_Grambo :slight_smile:

1 Like