cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
glparker
Community Champion

What's the deal with changing role_id's

So, I started looking at using API to add and remove enrollments in 2019.    At that time, I had mades calls to 
  /api/v1/accounts/86643/roles
And found that for us, 
   "id": 1208,
   "role": "TeacherEnrollment",
   "label": "Teacher",

That project got put on hold, but I'm picking it back up.    

Today, when I get a teachers enrollment, I don't get back role_id 1208, rather, I'm not getting back for all teachers role_id 7743

If I call the account roles api, I also get back TeacherEnrollment of 7743, and I don't see role_id 1208 in this list
  /api/v1/accounts/86643/roles

If I get the details of role 7743, I get back all the permissions I would expect for this role_id
   /api/v1/accounts/86643/roles/7743

HOWEVER,  I can still use the API to get back the details of the old role_id 1208
   /api/v1/accounts/86643/roles/1208

  With the ability to query/add roles by Name, I HAVE to use role_id, right?   So, I should expect that I could 'hard code' role 1208 in my code and it would always work to add Teachers to courses.   But my lived experience is that somewhere between 2019 and today, Instructure created a new TeacherEnrollment with a new role_id.

Should I hard code 7743 today and hope it doesn't change?   Or do I need to make an expensive call to /roles to lookup whatever the current role_id for TeacherEnrollment is before every call to modify an enrollment?   What's the best practice here?  

 

 

 

 

Labels (1)
0 Kudos
5 Replies
glparker
Community Champion

Additional information.  

 

Instructure lets me create/POST a new enrollment using the old 1208 role_id, and it adds them as a teacher.   However, when I look at the enrollment record, Instructure has renumbered it to the 'current' 7743

 
"role": "TeacherEnrollment",
"role_id": 7743,
 
The problem I'm having is if I add the person with 1208, delete them, then try to readd them with 1208, I get an internal server error.     POST 1208, DELETE, POST 1208 -> Internal Server Error

If I do the same, POST 7743, Delete, POST 7743, it works fine.
 
So, really, this all boils down to making sure I'm using the correct role_id.   So my main concern is will it change again, and was the original change documented and I missed it?   
MattHanes
Community Champion

I just set it to TeacherEnrollment. I don't specify a role. There are only two required fields to add an enrollment:

curl https://<canvas>/api/v1/courses/:course_id/enrollments \
  -X POST \
  -F 'enrollment[user_id]=2' \
  -F 'enrollment[type]=TeacherEnrollment'
glparker
Community Champion

Will setting it to type TeacherEnrollment always work if you have custom roles that are based on TeacherEnrollment?   

glparker
Community Champion

Oh!   I had missed that enrollment[type] is actually a required element.   That's interesting, I've not been using it.  

 

MattHanes
Community Champion


@glparker wrote:

Will setting it to type TeacherEnrollment always work if you have custom roles that are based on TeacherEnrollment?   


I'm not sure, we haven't really had any use cases for using a custom role for courses. We only use the account custom roles for people like principals or academic coaches to determine what they can see as an administrator.