Can't retrieve loginName / AccountName of person field
Posted: 28 Feb 2020
Hello !
I have an issue form, with a "Demandeur" single person field, and of course the AssignedTo field.
I need to do 3 comparisons / actions with the current user :
1 _ If the "Demandeur" field is empty, I fill it with the current user.
_ Else (if the "Demandeur" field isn't empty) then ...
2 _ if the "Demandeur" field is different from the current user, I hide a table of the form (it's a table with fields for the Demandeur).
3 _ If the AssignedTo field is empty, then do nothing.
_ Else (if the AssignedTo field isn't empty) then ...
4.1 _ if the AssignedTo field is different from the current user, I hide a table of the form (it's a table with fields for the AssignedTo person).
There is the code that does it :
The firts comparison works well and I succeed to fill the "Demandeur" field with the current user.
2 issues detected by several tests :
_ For the "Demandeur" field , it's like this field was impossible to read: "Unable to get property '0' of undefined or null reference"
>> How can I retrieve the login name of that field ??
_ For the AssignedTo field, it's like this field was full when empty : When I am on item where the AssignedTo field is empty, the "if" react like this field was full (I see the console.log("6");), and I got the error : "Unable to get property '0' of undefined or null reference"
>> How can I do to detect if my AssignedTo field is empty or not ??
If I can't do that, my list of issues will never work as expected, and it's a highly expected list for my company.
Thank you very much in advanace for your help !
I have an issue form, with a "Demandeur" single person field, and of course the AssignedTo field.
I need to do 3 comparisons / actions with the current user :
1 _ If the "Demandeur" field is empty, I fill it with the current user.
_ Else (if the "Demandeur" field isn't empty) then ...
2 _ if the "Demandeur" field is different from the current user, I hide a table of the form (it's a table with fields for the Demandeur).
3 _ If the AssignedTo field is empty, then do nothing.
_ Else (if the AssignedTo field isn't empty) then ...
4.1 _ if the AssignedTo field is different from the current user, I hide a table of the form (it's a table with fields for the AssignedTo person).
There is the code that does it :
Code: Select all
SP.SOD.executeOrDelayUntilScriptLoaded((function () {
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
ctx.load(web);
var user = web.get_currentUser();
user.retrieve();
ctx.executeQueryAsync(
function () {
console.log("0");
var currentuser = user.get_loginName();
console.log("CurrentUser = " + currentuser + " .");
// si demandeur est vide, alors remplir avec current user
if (fd.field('Demandeur').value() == '') {
console.log("1");
fd.field('Demandeur').value(user.get_loginName());
}
else {
console.log("2");
var demandeur = fd.field('Demandeur').value().dictionaryEntries.AccountName;
console.log("Demandeur = " + demandeurTXT + " .");
//si demandeur n'est pas egal à currentuser alors masquer les boutonsUser
if (demandeur != currentuser) {
console.log("3");
$('.boutonsUser').hide();
}
}
console.log("4");
if (fd.field('AssignedTo').value == '') {
console.log("5");
}
else {
console.log("6");
var assignedTo = fd.field('AssignedTo').value().dictionaryEntries[0].AccountName;
console.log("AssignedTo = " + assignedTo + " .");
//si currentuser == assigné à, alors masquer le boutonPEC
if (assignedTo == currentuser) {
console.log("7");
$('.boutonPEC').hide();
}
}
}
);
}), "SP.js");
2 issues detected by several tests :
_ For the "Demandeur" field , it's like this field was impossible to read: "Unable to get property '0' of undefined or null reference"
>> How can I retrieve the login name of that field ??
_ For the AssignedTo field, it's like this field was full when empty : When I am on item where the AssignedTo field is empty, the "if" react like this field was full (I see the console.log("6");), and I got the error : "Unable to get property '0' of undefined or null reference"
>> How can I do to detect if my AssignedTo field is empty or not ??
If I can't do that, my list of issues will never work as expected, and it's a highly expected list for my company.
Thank you very much in advanace for your help !