Is it possible to fetch unique dedicated Business Code for my clients registration?

I have a simple registration for my clients to register themselves according to there business category and subcategory/categories. I would like to know is it possible to give unique business code fetched from another dataset matching category and subcategory (Every time when a new business registers)? Kindly help asap : )

You will have to elaborate more about your wished functionality.
Not everything is clear in your setup.

  1. Are all of the Business-Categories already predefined and included in a Business-Collection, or do the Business-Collection grow-up with every NEW Business (entered by a new USER) automaticaly?

  2. What exactly would be the “Business-Sub-Categories”?

  3. The same question for “Business-Code” - - > It’s already predefined somewhere? Every of the Business has already a predefined CODE in a Collection, or do you generate always a NEW-RANDOM one for every new entered Business, which is not inluded in your Business-Collection, yet?

If you are searching for a good and exact answer, you should also elaborate more of your project and also give some more details, or the best would be a simple EXAMPLE of the whole project flow, from A to Z, described step by step!

Hi, @russian-dima Thanks for your quick response. I have uploaded some of snapshots to understand my scenario.

import wixData from ‘wix-data’;

$w . onReady ( function () {
uniqueDropDown1 ();
});
function uniqueDropDown1 (){
wixData . query ( “collection” )
. limit ( 1000 )
. find ()
. then ( results => {
const uniqueTitles = getUniqueTitles ( results . items );
$w ( “#dropdown1” ). options = buildOptions ( uniqueTitles );
});
function getUniqueTitles ( items ) {
const titlesOnly = items . map ( item => item . userType );
return [… new Set ( titlesOnly )];
}
function buildOptions ( uniqueList ) {
return uniqueList . map ( curr => {
return { label : curr , value : curr };
});
}
}

export function dropdown1_change_1 ( event , $w ) {
uniqueDropDown2 ();
$w ( “#dropdown2” ). enable ();
}

function uniqueDropDown2 (){
wixData . query ( “collection” )
. contains ( “userType” , $w ( “#dropdown1” ). value )
. limit ( 1000 )
. find ()
. then ( results => {
const uniqueTitles = getUniqueTitles ( results . items );
$w ( “#dropdown2” ). options = buildOptions ( uniqueTitles );
});
function getUniqueTitles ( items ) {
const titlesOnly = items . map ( item => item . category );
return [… new Set ( titlesOnly )];
}
function buildOptions ( uniqueList ) {
return uniqueList . map ( curr => {
return { label : curr , value : curr };
});
}
}

export function dropdown2_change_1 ( event , $w ) {
uniqueDropDown3 ();
$w ( “#dropdown3” ). enable ();
}

function uniqueDropDown3 (){
wixData . query ( “collection” )
. contains ( “category” , $w ( “#dropdown2” ). value )
. limit ( 1000 )
. find ()
. then ( results => {
const uniqueTitles = getUniqueTitles ( results . items );
$w ( “#dropdown3” ). options = buildOptions ( uniqueTitles );
});
function getUniqueTitles ( items ) {
const titlesOnly = items . map ( item => item . subCategory );
return [… new Set ( titlesOnly )];
}
function buildOptions ( uniqueList ) {
return uniqueList . map ( curr => {
return { label : curr , value : curr };
});
}
}

export function dropdown3_change ( event , $w ) {
uniqueDropDown4 ();
$w ( “#dropdown4” ). enable ();
}

function uniqueDropDown4 (){
wixData . query ( “BusinessRegister1” )
. contains ( “subCategory” , $w ( “#dropdown3” ). value )
. limit ( 1000 )
. find ()
. then ( results => {
const uniqueTitles = getUniqueTitles ( results . items );
$w ( “#dropdown4” ). options = buildOptions ( uniqueTitles );
});
function getUniqueTitles ( items ) {
const titlesOnly = items . map ( item => item . businessCode );
return [… new Set ( titlesOnly )];
}
function buildOptions ( uniqueList ) {
return uniqueList . map ( curr => {
return { label : curr , value : curr };
});
}
}


I just need to get a new unique code (fetched from dataset “BusinessRegister1”) but I am not getting how to eliminate already used code or disable it which already given to a registered user. Kindly help : (

If i would be you, i would probably do something like this…


I would add a additional data-field into my db, which would detect all already used codes, when doing a DB-Query. → I hope i did not misunderstand your wished functionality.

Of course everything can be generated by code, that means…

-everytime when you a new registration, you also write corresponding values into your DB (update, save or insert).

@russian-dima Sir, Kindly elaborate on this. How to DB-Query already used code by additional data-field? Kindly update my code on this. It will be very helpful if you could update.

@onlinesanawad
For me it is a little bit difficult understand all the flow of your project, because i do not see all of your DBs and single steps. It would turn into a SERVICE if i would write code for you.

But what you can do yourself? You can use → CONSOLE to investigate your own code.
Take a look onto this expanded example of your code…

import wixData from 'wix-data';
 //--------------- User-Interface -------------------
const COLLECTION1 = "collection";
const COLLECTION2 = "BusinessRegister1";
const LIMIT = 1000;

$w.onReady(function() {uniqueDropDown1();
    $w('#dropdown1').onchange((event)=>{console.log(event.target.id+"-clicked!");
        create_uniqueDropDown2();
        $w("#dropdown2").enable(); console.log("DropDow-2 enabled!");
    });
    //-----------------
    $w('#dropdown2').onchange((event)=>{console.log(event.target.id+"-clicked!");
        create_uniqueDropDown3();
        $w("#dropdown3").enable(); console.log("DropDow-3 enabled!");
    });
    //-----------------
    $w('#dropdown3').onchange((event)=>{console.log(event.target.id+"-clicked!");
        create_uniqueDropDown4();
        $w("#dropdown4").enable(); console.log("DropDow-4 enabled!");
    });
});

//---------------------- [ FUNCTIONS ] ----------------------------------------------------
function uniqueDropDown1 (){ console.log("Generating Unique-DropDown-1 running...");
    wixData.query(COLLECTION1)
    .limit(LIMIT)
    .find()
    .then(results=> {console.log("RESULTS: ", results.items);
        const uniqueTitles = getUniqueTitles(results.items);
        $w("#dropdown1").options = buildOptions(uniqueTitles);
    });

    function getUniqueTitles(items) {
        const titlesOnly = items.map(item => item.userType);
        return [...new Set(titlesOnly)];
    }
    function buildOptions(uniqueList) {
        return uniqueList.map(curr => {
            return {label:curr, value:curr};
        });
    }
}
 
function create_uniqueDropDown2() {
    wixData.query(COLLECTION1)
    .contains("userType", $w("#dropdown1").value)
    .limit(LIMIT)
    .find()
    .then(results=> {console.log("RESULTS: ", results.items);
        const uniqueTitles = getUniqueTitles(results.items);
        $w("#dropdown2").options = buildOptions(uniqueTitles);
    });
    function getUniqueTitles(items) {
        const titlesOnly = items.map(item => item.category);
        return [...new Set(titlesOnly)];
    }
    function buildOptions(uniqueList) {
        return uniqueList.map(curr => {
            return {label:curr, value:curr};
        });
    }
}
     
function create_uniqueDropDown3() {
    wixData.query(COLLECTION1)
    .contains("category", $w("#dropdown2").value)
    .limit(LIMIT)
    .find()
    .then(results=> {console.log("RESULTS: ", results.items);
        const uniqueTitles = getUniqueTitles(results.items);
        $w("#dropdown3").options = buildOptions(uniqueTitles);
    });
    function getUniqueTitles(items) {
        const titlesOnly = items.map(item => item.subCategory);
     return [...new Set(titlesOnly)];
    }
    function buildOptions(uniqueList) {
        return uniqueList.map(curr => {
            return {label:curr, value:curr};
        });
    }
}
 
function create_uniqueDropDown4() {
    wixData.query(COLLECTION2)
    .contains("subCategory", $w("#dropdown3").value)
    .limit(LIMIT)
    .find()
    .then(results => {console.log("RESULTS: ", results.items);
        const uniqueTitles = getUniqueTitles(results.items);
        $w("#dropdown4").options = buildOptions(uniqueTitles);
    });
    function getUniqueTitles(items) {
        const titlesOnly = items.map(item => item.businessCode);
        return [...new Set(titlesOnly)];
    }
    function buildOptions(uniqueList) {
        return uniqueList.map(curr => {
            return {label:curr, value:curr};
        });
    }
}

You should always work with CONSOLE, especialy if you have issues!
You just showing the code for your unique generated dropdowns, which will fill your dropdowns automaticaly from the corresponded DATABASES/COLLECTIONS.

I see no if-else-queries inside your code and also no saving-processes. All the given code right now, is just for the filling of your dropdowns.

1 Like