1

Through javascript, I was hoping to check what groups a user belongs to and then use that information to show / hide something. I can't use ACLs because I'd like the average internal user to be able to enter the information on intake but not be able to edit it in future.

Using the API, I can get an object that has a

    CRM.api3('Contact', 'get', {
  "return": "group",
  "id": 68525 // arbitrary number will need current user
}).done(function(result) {
  // do something
    console.log(result);

});

which returns this

Object { is_error: 0, version: 3, count: 1, id: 68525, values: Object }

The group info is in the values: Objects, how can I reference it?

  • Did you try console.log(result.values); ? – Demerit Oct 4 '17 at 19:13
  • Similar result: Object { 68525: Object } In the console I can see inside that nested object, it holds the groups info, I just have no idea how to get to it. – RayWright Oct 4 '17 at 19:18
  • You can also iterate with a loop if you don't know the id, e.g. for (var i in result.values) { // do something with result.values[i] } Can also search the internet for general javascript howtos. – Demerit Oct 4 '17 at 20:08
  • Yes, I did search the internet, but was struggling for the right search terms, I thought perhaps someone here had done something similar and could offer advice. I figured it out for myself. – RayWright Oct 5 '17 at 12:34
1

To get the 'groups' result from the returned 'result' - I had to access the nested data. I referenced this great explanation here: https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json

I still struggled for a bit and realized that's because the groups result i was looking for was nested down two levels.

result Object { is_error: 0, version: 3, count: 1, id: 68525, values: Object }

   values Object { 68525: Object }

       68525 Object { contact_id: "68525", groups: "Administrators", id: "68525" }

On top of that to reference that last object by a number i had to enclose on quotes.

I still have work to do towards my ultimate functionality but my proof of concept holds true.

Here's what I ultimately used to get the contents of 'groups':

console.log(result['values']['68525']['groups']);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.