Important Update: Some Community URL Redirects are Under Maintenance. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

Listing users from a Record Permissions field with a Custom Object

ThomasHeath3
Contributor II

We're trying to create a Custom Object in 6.11 that compares the users in two Record Permissions field but we're having trouble getting started. 

At a basic level, does anyone have a suggestion for script that would simply list the names and/or IDs of the people named in a Record Permissions field so that we can view the structure.

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions

@ThomasHeath3, div's don't have a value attribute line input elements have.

Just try and see what you get

Sys.Application.add_load(function() {
     console.log($('div[id*="f" + fieldId + "c_shf"]').text());
}};

 

 Advisory Consultant

View solution in original post

8 REPLIES 8

DavidPetty
Archer Employee
Archer Employee

@ThomasHeath3 , this should get you started

Just update the two variables (lines 2 and 3) with the record permission fields.  It will do the check when the user tries to Save or Save and Close.

<script type="text/javascript">
     var primaryUsersFieldName = 'User (Groups)';
     var secondaryUsersFieldName = 'Users/Groups Comparison';

     Sys.Application.add_load(function() {
          // Hijack Save Button
          $('#master_btnApply').clone().attr('id', 'master_customBtnApply').insertBefore('#master_btnApply');
          $('#master_btnApply').hide();
          $('#master_customBtnApply').unbind('click').prop("onclick", null).click(function(){ CheckUsers('apply');return false;});

          // Hijack Save and Close Button
          $('#master_btnSave').clone().attr('id', 'master_customBtnSave').insertBefore('#master_btnApply');
          $('#master_btnSave').hide();
          $('#master_customBtnSave').unbind('click').prop("onclick", null).click(function(){ CheckUsers('save');return false;});

          primaryUsersFieldId = lookupFieldId(primaryUsersFieldName);
          secondaryUsersFieldId = lookupFieldId(secondaryUsersFieldName);
     });

     function CheckUsers(action) {
          var selectedPrimaryUserValueObj = $.parseJSON($('input[id*="f' + primaryUsersFieldId + 'c_shf"]').val());
          var selectedSecondaryUserValueObj = $.parseJSON($('input[id*="f' + secondaryUsersFieldId + 'c_shf"]').val());
          var dupUsers = '';
          
          if(selectedPrimaryUserValueObj && selectedSecondaryUserValueObj) {
               $.each(selectedPrimaryUserValueObj, function(pitem, pkey){
                    $.each(selectedSecondaryUserValueObj, function(sitem, skey){
                         if (pkey.value === skey.value){
                              dupUsers += '<li>' + skey.name + '</li>';
                         }
                    });
               });
          }

          if (dupUsers) {
               var msg = "The following user(s) have already been selected in <b>" + primaryUsersFieldName + "</b>:<ul>" + dupUsers + "</ul>Please remove the above selected user(s) found in <b>" + secondaryUsersFieldName + "</b> order to continue.";
               var title = 'Warning';
               WarningAlert(msg,title);
          } else {
               SaveApply(action) 
          }
     }

     function lookupFieldId(fldName){
          var goFindId = null;

          try { goFindId = $('.FieldLabel:findField("' + fldName + ':") > span')[0].id;} catch (err) {}
          try {if (!goFindId) goFindId = $('.SectionLabel:findField("' + fldName + ':")')[0].id;} catch (err) {}
          try {if (!goFindId) goFindId = $('.SubSectionLabel:findField("' + fldName + ':")')[0].id;} catch (err) {}

          return goFindId ? $LM._layoutItems[goFindId.replace( /^\D+/g, '')].fieldId : 0;
     }

     function SaveApply(action) {
          if (action == 'save') {
               $("#master_btnSave").click();
          } else if (action == 'apply') {
               $("#master_btnApply").click();
          }
     }

     $.expr[':'].findField = function(a, i, m) {
          return $(a).text().replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_').match("^" + m[3].replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_') + "$");
     };
</script>

 

 Advisory Consultant

Thank you, David...this is very helpful.

Would it make a difference if the Record Permissions fields aren't being manually updated by a user but automatically populated by related records?

 

Anytime

Good question.  I'm assuming that the data is just plain HTML at that point.  You'd have to inspect the HTML to see how it's structured and go from there.

 Advisory Consultant

Hi David,

We're trying out the following code with some takeaways from your response, to try and print out the contents of the primary users field (to see what we can use from the code for our use case). We keep running into hanging loading screens or  selectedPrimaryUserValueObj being undefined. Any ideas?

 

Sys.Application.add_load(function() {
var selectedPrimaryUserValueObj = $.parseJSON($('div[id*="f" + fieldId + "c_shf"]').val());
console.log(selectedPrimaryUserValueObj);
if(selectedPrimaryUserValueObj && typeof selectedPrimaryUserValueObj !== undefined) {
$.each(selectedPrimaryUserValueObj, function(pitem, pkey){
console.log("KEY: " + pkey + ", ITEM: " + pitem);
});
}});

@ThomasHeath3, div's don't have a value attribute line input elements have.

Just try and see what you get

Sys.Application.add_load(function() {
     console.log($('div[id*="f" + fieldId + "c_shf"]').text());
}};

 

 Advisory Consultant

One problem we ran into is that we can HIDE the duplicate user in the Secondary field but the notifications and record generation that references that field is still seeing and acting on those hidden users. Do you have any thoughts on how to REMOVE and not HIDE those duplicates based on the script below? Thank you!


Sys.Application.add_load(function () {
var primaryDiv = document.getElementById("master_DefaultContent_rts_s6032_f" + primaryFieldId + "c");
var secondaryDiv = document.getElementById("master_DefaultContent_rts_s6032_f" + secondaryFieldId + "c");

var primaryListItems = primaryDiv.getElementsByTagName("ul")[0].getElementsByTagName("li");
var secondaryListItems = secondaryDiv.getElementsByTagName("ul")[0].getElementsByTagName("li");

let count = 0;
let secondariesToRemove = [];
let index = 0;

for (let primaryListItem of primaryListItems) {
let primaryAnchor = primaryListItem.getElementsByTagName("div")[0].getElementsByTagName("a")[0];

if (primaryAnchor === undefined) {
break;
}

let primaryHref = primaryAnchor.getAttribute("href");

for (let secondaryListItem of secondaryListItems) {
let secondaryAnchor = secondaryListItem.getElementsByTagName("div")[0].getElementsByTagName("a")[0];

if (secondaryAnchor === undefined) {
break;
}

let secondaryHref = secondaryAnchor.getAttribute("href");

count++;

if (primaryHref === secondaryHref) {
secondariesToRemove[index] = secondaryListItem;
index++;
}
}
}

for (let secondary of secondariesToRemove) {
secondary.innerHTML = '';
}
});

As-long-as the field isn't inheriting users you'd have to get the id of the selected users, remove the duplicate and repopulate the field using this custom object, Re: Custom Object for Record Permission set value - Archer Community - 408587 (archerirm.community)

 Advisory Consultant

Hi David - we are getting closer but still struggling with this a bit. The input field with the RPFieldRoot id has its value successfully updated to be the same user list, minus the duplicates, but the user field itself still has the duplicates in it on page load. Thank you!

Here's our script if anything pops out:

 

function getUsers() {
var primaryDiv = document.getElementById("master_DefaultContent_rts_s6032_f22161c");
var secondaryDiv = document.getElementById("master_DefaultContent_rts_s6032_f22160c");

if (primaryDiv === undefined || primaryDiv === null || secondaryDiv === undefined || secondaryDiv === null) {
console.log("Some div was null");
console.log("Primary div: " + primaryDiv);
console.log("Secondary div: " + secondaryDiv);
return null;
}

var primaryListItems = primaryDiv.getElementsByTagName("ul")[0].getElementsByTagName("li");
var secondaryListItems = secondaryDiv.getElementsByTagName("ul")[0].getElementsByTagName("li");

let count = 0;
let secondariesToRemove = [];
let index = 0;

for (let primaryListItem of primaryListItems) {
if (primaryListItem === undefined || primaryListItem === null) {
console.log("Primary list item was null or undefined");
continue;
}

let primaryAnchor = primaryListItem.getElementsByTagName("div")[0].getElementsByTagName("a")[0];

if (primaryAnchor === undefined) {
console.log("Primary anchor was null or undefined");
break;
}

let primaryHref = primaryAnchor.getAttribute("href");

for (let secondaryListItem of secondaryListItems) {
let secondaryAnchor = secondaryListItem.getElementsByTagName("div")[0].getElementsByTagName("a")[0];

if (secondaryAnchor === undefined) {
console.log("Secondary anchor was null or undefined");
break;
}

let secondaryHref = secondaryAnchor.getAttribute("href");

count++;

if (primaryHref === secondaryHref) {
console.log("HREF before substring: " + secondaryHref);
let tmp = secondaryHref.substring(78);
tmp = tmp.substring(0, 5);
secondariesToRemove[index] = tmp;
index++;
console.log("Href after substring: " + tmp);
}
}
}

return secondariesToRemove;
}

function setUsers(userFldId, secondariesToRemove) {
console.log("In setUser...");

var RPFieldRoot = ArcherTech.UI.ClientContentManager.GetInstance().getFieldById(userFldId), UsrArray = [];
var RPFieldRootId = RPFieldRoot.clientId;

console.log("RPFieldRootId: " + RPFieldRootId);

let existingUsersText = $('input[id*="' + RPFieldRootId + '_"]').val();
let addedUserIds = [];
const existingUsers = JSON.parse(existingUsersText);
console.log(existingUsers);

for (let user of existingUsers) {
console.log(user);
let foundMatch = false;
for (let secondaryToRemove of secondariesToRemove) {
if (user.value === secondaryToRemove+":1") {
console.log("Found match: " + user.value + " " + user.name);
foundMatch = true;
break;
}
}
if (!foundMatch) {
UsrArray.push(user);
addedUserIds.push(user.value.substring(0, 5));
}
}



var serialized = Sys.Serialization.JavaScriptSerializer.serialize(UsrArray);
$('div[id*="' + RPFieldRootId + '_"] div:first-child').text('');
$('input[id*="' + RPFieldRootId + '_"]').val(serialized);
$('#SelectedUsers22361').val(addedUserIds);
}

Sys.Application.add_load(function () {
let secondariesToRemove = getUsers();
if (secondariesToRemove !== null) {
setUsers(22361, secondariesToRemove);
} else {
console.log("Found no secondaries to remove");
}

alert('script loaded');
});