[SOLVED] URGENT | Wix Code Javascript Question | PLEASE HELP

HUGE THANK YOU to @stcroppe and @yisrael-wix for your help with this! I decided against a companyId and used your method to create a hook for a projID instead. I honestly would not have come anywhere near this without your assistance so my gratitude is deep and wide!!! Here’s what the final data.js file looks like:

//Proj ID Hook (VA Plans)
export function projects_afterQuery(item, context) {
 let companyName = item.company;
 
// Generate the two character prefix from a multiple word company name
 let regex1 = /^(\w)\w*\s*(\w)?(.*)/; 
 let prefixChars = companyName.replace(regex1, "$1$2");

// Make sure we have a two character prefix AND that the company name has at least 
// two characters for us to use
 if (prefixChars.length !== 2 && companyName.length > 2) {
    prefixChars = companyName.substring(0, 2);
}

let dd = ('0' + item.datePurchased.getDate()).slice(-2);
let mm = ('0' + (item.datePurchased.getMonth() + 1)).slice(-2);
let yy = item.datePurchased.getFullYear().toString().substr(2, 2);
let comID = prefixChars.toUpperCase() + mm + dd + yy;
 
item.projID = item.planType + " | " + comID
    ;
 
 return item;
}

Hello! Could someone provide an example that would show me how to create a #companyId using the first letter of the first two words a user provides in a company name input field followed by the 6-digit date selected in a date input field? For instance, if someone typed Wix Code Forum in the #companyName field, and the date selected in the #datePurchased field was 03/26/2019, their #companyId would be WC032619. Thanks in advance!!!

Hey Juanita,

How are you? The following code should give you what you’re looking for:

let companyName = "Taylor Enterprises";
let dd = ('0' + date.getDate()).slice(-2);
let mm = ('0' + (date.getMonth() + 1)).slice(-2);
let yy = date.getFullYear().toString().substr(2, 2);
let comID = companyName.substring(0, 2).toUpperCase() + mm + dd + yy;

@juanita-taylor Yisrael’s solution is consistent with the solution I provided in Facebook. The key condition to it working is that you would need to make sure that the date input element, that you are using, is a datePicker and NOT a text input element.


If you use a text input element the processing to get the date elements that you need will be different.

I will offer a minor adjustment to Yisrael’s solution as it doesn’t answer your question regarding selecting the first character of each work in the company name. The solution provided will take the first two letters “Ta” and result in the prefix TA.

To get the two characters from the first letter of each of the first two words in a string you could use a regular expression like this:

let regex1 = /^(\w)\w*\s*(\w)?(.*)/;
let prefixChars = $w('#companyName').value.replace(regex1, "$1$2").toUpperCase();
console.log(prefixChars);

So if $w(‘#companyName’).value is set to “Taylor Enterprises” the prefix will be “TE”. The regular expression replace() function used in this way will only ever deliver a prefix of one or two characters depending upon the number of words in the company name. So if the company name is only one word, e.g. “Taylors” then the prefix delivered is “T”.

If you MUST have two characters then you can blend the approaches like this:

let companyName = "Taylor Enterprises";

// Generate the two character prefix from a multiple word company name
let regex1 = /^(\w)\w*\s*(\w)?(.*)/; 
let prefixChars = companyName.replace(regex1, "$1$2");

// Make sure we have a two character prefix AND that the company name has at least 
// two characters for us to use
if (prefixChars.length !== 2 && companyName.length > 2) {
    prefixChars = companyName.substring(0, 2);
}

let dd = ('0' + date.getDate()).slice(-2);
let mm = ('0' + (date.getMonth() + 1)).slice(-2);
let yy = date.getFullYear().toString().substr(2, 2);
let comID = prefixChars.toUpperCase() + mm + dd + yy;

Hope this helps

Wow, @yisrael-wix and @stcroppe ! Thanks SO much for your help! I never would’ve gotten this far without you! Unfortunately, my code only worked partially. It produced the date, but not the prefix. Could you take a look at the code below to let me know what I’ve done wrong? Also, I’d like to update the client id’s of my current clients. Should I use a hook to do that? If so, what adjustments do I need to make to the existing after query hook that I already have setup on the existing data.js file?
Again - thanks so much for your help!

let regex1 = /^(\w)\w*\s*(\w)?(.*)/;
 let prefixChars = $w('#companyName').value.replace(regex1, "$1$2").toUpperCase();
  console.log(prefixChars);
 
let date = $w('#dateCreated').value; // Must be a date picker
let day = String(date.getDate());
let month = String(date.getMonth() + 1); // Months start at 0
let year = String(date.getFullYear()).substring(2); // we use getFullYear because getYear is deprecated
// Create the companyID
let companyId = `${prefixChars}${day}${month}${year}`;
console.log(companyId);
// Set the element
$w('#companyId').text = companyId; // Use value if companyId is textInput

Is the page you have this on public? What is the URL so we could have a look at the page behaviour? What does the console.log(prefixChars) statement show?

Off hand the issue is likely to be with the value being returned from the $w(‘#companyName’) element. So the seeing the code in context would be a big help (either the whole page code or the page URL as mentioned above.

CHeers

@stcroppe The code is on my Signup page which is setup in accordance with Code Queen Nayelli’s tutorial on Member signups (the one that allows for searching for duplicate usernames). My website address is www.writeitrightsite.com. Here’s the full page code:

 
import wixUsers from 'wix-users';

import wixData from 'wix-data';

import wixLocation from 'wix-location';

 

$w.onReady(function () {
 
 let user = wixUsers.currentUser;

 let userId = user.id; // "r5cme-6fem-485j-djre-4844c49" 

 let isLoggedIn = user.loggedIn; // true


  user.getEmail()

    .then( (email) => {

 let userEmail = email;

    $w("#emailText").value = userEmail;

 let referralCode = userId.split("-")[4];
 
    $w("#referralCode").text = referralCode;

 let clientType = clientType;
 
    $w("#profileClientType").value = $w("#clientType").value;

 let regex1 = /^(\w)\w*\s*(\w)?(.*)/;
 let prefixChars = $w('#companyName').value.replace(regex1, "$1$2").toUpperCase();
  console.log(prefixChars);
 
let date = $w('#dateCreated').value; // Must be a date picker
let day = String(date.getDate());
let month = String(date.getMonth() + 1); // Months start at 0
let year = String(date.getFullYear()).substring(2); // we use getFullYear because getYear is deprecated
// Create the companyID
let companyId = `${prefixChars}${day}${month}${year}`;
console.log(companyId);
// Set the element
$w('#companyId').text = companyId; // Use value if companyId is textInput

  } );

$w("#createProfileButton").onClick( () => {

 let toInsert = {

 "firstName": $w("#firstName").value,

 "lastName": $w("#lastName").value,

 "companyName": $w("#companyName").value,

 "emailAddress": $w("#emailText").value,

 "websiteAddress": $w("#websiteAddress").value,

 "phone": $w("#phone").value,

 "username": $w("#username").value,

 "referredBy": $w("#referredBy").value,

 "clientType": $w("#profileClientType").value,

 "companyId": $w("#companyId").text,

 "referralCode": $w("#referralCode").text
  };

  wixData.insert("profile", toInsert)

  .then( () => {

    wixLocation.to("/account/profile-page");

  } )
});

});

export function clientType_onchange(event) {
 
  console.log($w("#clientType").value);

 if ($w("#clientType").value === "Virtual Assistant Client") {
       $w("#boxCompanyDetails").show();
       $w("#profileClientType").value = $w("#clientType").value;             
       } 
 if ($w("#clientType").value === "Standard Client") {
         $w("#boxCompanyDetails").hide();
         $w("#profileClientType").value = $w("#clientType").value;
 
}}

export function buttonSaveCompany_onclick(event) {
  $w("#boxCompanyDetails").hide();
 
}

export function button1_mouseIn(event) {
  $w("#boxClientNameScreenTip").show(); 
}
export function button1_mouseOut(event) {
  $w("#boxClientNameScreenTip").hide(); 
}

export function buttonCloseTip_onclick(event) {
  $w("#boxClientNameScreenTip").hide();
}