Server side script string comparison

Matrix Version:5.5.1.3

<script runat="server">
var id = 1234;

var getName = "%globals_get_text%"; // "Lun"
var name= "%" + "globals_asset_name:" + id + "%"; // "Lunch"

print(name.indexOf(getName)); // Errr: I am getting -1

</script>

Hi, I am checking if a short string is included in another long string with above code, but I am getting wrong result from the code. getName(Lun) should be included in name(Lunch) but it says it is not.

I tried with normal javascript and it works well. Should I need to do any other thing to complete this comparison?

Thanks in advance.

It’s because you are mixing global keyword evaluation with SSJS, they don’t get evaluated at the same time.

Couldn’t you just do a simple keyword check for this one? Something like

%globals_asset_name:1234^contains:{globals_get_text}%

Have a read of https://matrix.squiz.net/manuals/concepts/chapters/server-side-javascript#processing-order for more info on the processing order of SSJS and keywords.

Whole code is like below

    var result = '%result_list%'; // '["1112","1113"],["1112","1114"],'

    var getName = "%globals_get_text%"; 
    var result_list = result.split(',');
    
    var list = [];
    for(var i = 0; i < result_list.length; i++){
        var id = result_list[i].replace(/[^0-9]/g, '');

        if(id && list.indexOf(id) === -1) {
            list.push(id);
        }
    }

   for(var i = 0; i < list.length; i++){
           var id = list[i];

           var getName = "%globals_get_text%"; // "Lun"
           var name= "%" + "globals_asset_name:" + id + "%"; // "Lunch"

           print(name.indexOf(getName)); // Errr: I am getting -1
   }

This code is written inside of search asset.

Seems like I cannot make this by simple keywords?

What are you actually trying to achieve from a high level?

what I am trying to build is
In a project, multiple students are participated. And there are like 10 projects and about 40 students are participated to each projects. I used search asset to find out all students who participated the project.

   var result = '%result_list%'; // '["1112","1113"],["1112","1114"],'
   var result_list = result.split(',');
   var list = [];
    for(var i = 0; i < result_list.length; i++){
        var id = result_list[i].replace(/[^0-9]/g, '');

        if(id && list.indexOf(id) === -1) {
            list.push(id);
        }
    }

Students are standard page asset, which is related asset(metadata) to project. Search Page asset searches all the projects and returns all student list from project’s metadata field.

   for(var i = 0; i < list.length; i++){
           var id = list[i];

           var getName = "%globals_get_text%"; // "Lun"
           var name= "%" + "globals_asset_name:" + id + "%"; // "Lunch"

           print(name.indexOf(getName)); // Errr: I am getting -1
   }

Above part, we have list of asset id of students. We should be able to filter by name from the list.

var getName = “%globals_get_text%”; <-- name user put on the filter
var name= “%” + “globals_asset_name:” + id + “%”; <-- We get the asset name using keyword

If name on the filter is included to any name in the list, print something

This is my purpose of those codes.

Also I tried

var link = '<a href="' + '%' + 'globals_asset_url:' + id + '%' + '" rel="noopener noreferrer">' + name + '</a>';
print('%' + 'globals_asset_name:' + id + '^contains:{globals_get_text}' + ':'+ link + ':%');

but no luck yet

Try doing something like this instead where you bring the “if” logic into the keyword instead of JS:

print("%" + "globals_asset_name:" + id + "^contains:{globals_get_text}:yes:no%"); 

But you could also try and print the actual asset’s name in the search results, so that it’s part of the array that %result_list% produces, that way you can do it all in JS.