Important Update: Archer Community Scheduled Maintenance on November 23–24 - New Community Launching Soon! Learn More..
2024-06-24 09:46 PM - edited 2024-06-24 09:56 PM
Hi all, I'm trying to make a custom object to set a user/group field to the current user when they click it and am not having much luck. I've cannibalised some other code I found in this thread: Solved: Custom Object to Get Current User name into a Text field - Page 2 - Archer Community - 448033 (archerirm.community)
But I cannot get it working on just a button click. It doesn't change the value of the field at all, but does add a weird 'Go' where the dropdown icon normally is.
When I try save the record after clicking it I just get an unexpected error.
Have put my code below, any assistance would be appreciated!
<script type="text/javascript">
var setUser = {
fldId: '23085',
usersName: parent.parent.ArcherApp.globals.displayName,
usersId: parent.parent.ArcherApp.globals.userId};
console.log(setUser);
function assignUser() {
console.log("In assignUser function")
var RPFieldRoot = ArcherTech.UI.ClientContentManager.GetInstance().getFieldById(setUser.fldId), UsrArray = [];
var RPFieldRootId = RPFieldRoot.clientId;
UsrArray.push({
name: setUser.usersName,
value: setUser.usersId + ':1'
});
var serialized = Sys.Serialization.JavaScriptSerializer.serialize(UsrArray);
$('div[id*="'+ RPFieldRootId +'_"] div:first-child').text(setUser.usersName);
$('input[id*="'+ RPFieldRootId +'_"]').val(serialized);
$('#SelectedUsers'+setUser.fldId).val(setUser.usersId);
}
</script>
<!-- The button is placed inside of a table so that we can centre it on the page -->
<table style="margin-left: auto; margin-right: auto;">
<tbody>
<tr>
<td style="text-align: center;">
<a title="Assign To Me" id="assignToMe" href="javascript:assignUser();">Assign To Me</a>
</td>
</tr>
</tbody>
</table>
2024-06-25 01:07 AM
I have used the same Custom Object and it's working as expected, just changed few things - window.onload() for overriding button's onclick functionality and the fieldID, rest everything was same. Try using the one from here archer-custom-objects-forum/custom-object-to-get-current-user-name-into-a-text-field
2024-06-25 09:56 AM
@JordanG is there another custom object in the app that's adding the 'go' the field?
For your hyperlink, try this instead:
<a title="Assign To Me" id="assignToMe" href="#" onclick="assignUser();">Assign To Me</a>
Advisory Consultant
2024-06-25 09:10 PM
Thanks for the reply David and squarabh. With your suggestion David unfortunately it still errors out, although this time instead of an unexpected error it said I didn't have permission to view the page the moment I pressed the button.
To confirm, the only other custom objects on that page are the chevron tracker and the Open In New Window. Took both those custom objects off, no change.
I've tried changing my 'button' from an <a> tag to a <button> tag, also no different. I've also tried to implement everything within a Sys.Application.add_load function, which is how the code I cannibalised works, but that also throws an unexpected error. I've quadruple checked and the field ID is definitely correct.
One thing I did only just notice is that the field was set to private (but I did have edit permissions for it), so thought I might've figured it out, but setting it to public has also not changed the behaviour.
This is my current attempt with no difference in behaviour, but closer to the working code from the post I mentioned:
<script type="text/javascript">
var setUser = {
fldId: '23085',
usersName: parent.parent.ArcherApp.globals.displayName,
usersId: parent.parent.ArcherApp.globals.userId};
console.log(setUser);
Sys.Application.add_load(function() {
console.log("Loaded")
$('button[id="assignToMe"]').removeAttr('onclick').unbind('click');
$('button[id="assignToMe"]').prop("onclick", null);
$('button[id="assignToMe"]').click(function() {
console.log("I tried to run")
assignUser();
});
function assignUser() {
console.log("In assignUser function")
var RPFieldRoot = ArcherTech.UI.ClientContentManager.GetInstance().getFieldById(setUser.fldId), UsrArray = [];
var RPFieldRootId = RPFieldRoot.clientId;
UsrArray.push({
name: setUser.usersName,
value: setUser.usersId + ':1'
});
var serialized = Sys.Serialization.JavaScriptSerializer.serialize(UsrArray);
$('div[id*="'+ RPFieldRootId +'_"] div:first-child').text(setUser.usersName);
$('input[id*="'+ RPFieldRootId +'_"]').val(serialized);
$('#SelectedUsers'+setUser.fldId).val(setUser.usersId);
}
})
</script>
<!-- Style formats how the button looks -->
<style>
.custom-assign-style{
background: #229bed;
background: linear-gradient(#229bed, #0084dd);
border-radius: 11px;
padding: 10px 20px;
color: #ffffff;
font: normal bold 15px/1 "Open Sans", sans-serif;
text-align: center;
display: inline-block;
text-decoration: none;
}
</style>
<!-- The button is placed inside of a table so that we can centre it on the page -->
<table style="margin-left: auto; margin-right: auto;">
<tbody>
<tr>
<td style="text-align: center;">
<button title="Assign To Me" class="custom-assign-style" id="assignToMe" href="#">Assign To Me</button>
</td>
</tr>
</tbody>
</table>