js file for fullName

Hi i have created a number of dropdown’s and text boxes for personal information. On the page code the fields have been assigned to either null or undefined depending upon user flow / choice

My current js has me by the balls at the moment. Any changes i make means that i cannot open a collection as this shows internet error which isn’t true. I have saved from a previous version and experimented to prove this.

I think this is because i have saved a previous version that now isn’t fit for purpose.

What i’m trying to achieve is based on the following but has these field keys
nickName, prefix, subPrefix, addOwnPrefix, firstName, middleName, lastName, suffix, addYourownSuffix

on the page code for any fields that will be empty i have assigned null or undefined,

so this is what i have, do i need to create a new js file to stop the collection being corrupted.

I took this code from a forum post and am unsure if the items should also concertina, a correct code example would be great

export function Member_afterQuery(item, context) {
item.fullName = item.nickName + “” + item.subPrefix + “” + item.firstName + “” + item.middleName + “” + item.lastName + “” + item.suffix;
let fullName = ‘’;
if (item.nickName !== undefined) {
fullName = item.nickName.trim() + ’ ’ + item.subPrefix;
} else {
fullName = item.subPrefix;
}
if (item.subPrefix !== undefined) {
fullName = item.subPrefix.trim() + ’ ’ + item.firstName.trim();
} else {
fullName = item.firstName;
}
if (item.middleName !== undefined) {
fullName = fullName + ’ ’ + item.middleName.trim() + ’ ’ + item.lastName.trim();
} else {
fullName = fullName + ’ ’ + item.lastName;
}
if (item.suffix !== null ) {
fullName = fullName + ’ ’ + item.suffix;
}
item.fullName = fullName;
return item;
}
Thanks
Adam

so can for each field key can i write code to define each key as either, null, undefined or ‘value present’ to create this fullName string

and if so was that the right code that i added
Thanks
Adam

I’m not sure what exactly you’re trying to do (didn’t have time to go analyze the code), but if all you want is to sequence all the defined fields with a single space between each of them, then you can do something like:

export function Member_afterQuery(item, context) {
    let data = [item.nickName, item.subPrefix, item.firstName, item.middleName , item.lastName, item.suffix];
    data = data.filter(e => e !== undefined);
    data = data.map(e => e.trim());
    item.fullName= data.join(' ');
    return item;
}
1 Like

@jonatandor35 ok thanks, i’ll have to give it a go tomorrow. This is such different code from what i had added previously and although not quite right i thought i was on the right track.

ultimately J.D. i thought that any fields that were null or undefined would be trimmed if i constructed the code above properly
Regards
Adam

@adcatch02 you know that the method trim() means ‘Remove white space from the beginning and the end of a string’ right? So undefined cannot be trimmed (I used trim() in the code above because I thought you were trying to remove white spaces).

I do now :slight_smile: thanks so i think i’ll be able to have a play around tomorrow and use a similar code but without trim for each field key

ok so i’m still at a loose end with this. Just want to put a fullName string together from a js file and leave out any fields that do not have a value

so as background example i have two cascading dropdown’s in page code and if user reaches the second dropdown i have assigned the first one to have a value of null

this also applies to other fields based on above logic. well i hope its logical :slight_smile:

I have added this code and can’t understand why middleName and lastName fields are not being picked up and displayed

export function MemberCandles_afterQuery(item, context) {
item.fullDobdod = item.dob + " " + item.dod;
let fullName = “”;
if (item.prefix) {
}
if (item.subPrefix) {
}
if (item.addOwnPrefix) {
}
if (item.firstName){
}
if (item.middleName){
}
if (item.lastName){
}
fullName = item.prefix + " " + item.subPrefix + " " + item.addOwnPrefix + " " + item.firstName, + " " + item.middleName, + " " + item.middleName, + " " + item.lastName;
return item;
}

It’s the commas. There should be none.

1 Like

I think thats worked thanks so much