Add Custom Field Setting insert_after

When using the Python API to insert a custom field into multiple projects, I am having unreliable results with the behavior of “insert_after” and “insert_before” options. Here’s my algorithm:

For each project in the team:
get the custom field settings
find the relevant “custom_field” “gid” to insert after
get the custom field setting gid
insert_after : custom field setting gid

This does stop the new custom field from being inserted at the end, but it does not insert the custom field into the correct spot. Sometimes it inserts it 2 after and other times it inserts it 4 fields after the “insert_after” custom field setting.

import asana

MY_PERSONAL_ACCESS_TOKEN = ':)'

ADD_CUSTOM_FIELD_GID = ':)'
TEAM_GID= ':)'
PROJECTS_EXCLUDED = [':)', ':)',':)',':)',':)']
INSERT_AFTER = ':)'

def customFieldSettingGID(client, project_gid, custom_field_gid):
	# Return the custom field setting gid to insert before

	# Path to send API request to
	path = "/projects/%s/custom_field_settings" % project_gid

	retval = None
	custom_fields = client.get(path,{})
	for field in custom_fields:
		if field['custom_field']['gid'] == custom_field_gid:
			retval = field['gid']
			break
		elif field['custom_field']['gid'] == ADD_CUSTOM_FIELD_GID:
			return 0
	return retval

def main():

	client = asana.Client.access_token(MY_PERSONAL_ACCESS_TOKEN)

	team_projects = client.projects.find_all({'team': TEAM_GID})

	for project in team_projects:
		if project['gid'] not in PROJECTS_EXCLUDED:
			try:
				cfs_insert_after = customFieldSettingGID(client,project['gid'],INSERT_AFTER)
				if cfs_insert_after is None:
					print("Insert After not found in: %s" % project['name'])
				elif cfs_insert_after == 0:
					print("Custom Field Already Exists in: %s" % project['name'])
				else:
					client.projects.add_custom_field_setting(project['gid'], {'custom_field':ADD_CUSTOM_FIELD_GID, \
						"insert_after": cfs_insert_after, "is_important": False})
					print("Added to: " + project['name'])

			except asana.error.ForbiddenError:
				print("Custom Field already exists in: " + project['name'])

if __name__ == "__main__":
	main()

I am facing the same issue with insert_before. Same approach, querying the project’s custom field settings, using that gid as “insert_before”, yet the custom field gets inserted at the very end…

Hi everyone,

I tried to reproduce this via the API in Postman with the Add a custom field to a project endpoint and was able to see that the behavior with insert_after seems inconsistent. At times it would insert the custom field in the right place and at other times it does not. This seems to me like a bug. I’ll flag this internally.

1 Like