Number of conditions met

  • 1
  • Question
  • Updated 4 years ago
  • Answered
I have two separate objects that have fields with matching information such as industry and country. I would like to query a table on object 2 with information from object 1. That is fairly easy to do by adding a row action button on object 1 that activates and sets values of conditions of object 2, then pulls up a popup with the queried table. Then I just have to make all of the conditions one long OR statement. (ie Industry OR Country OR ect....) However, it would be great to be able to rank these by strength of match. If I have 10 OR conditions and 9 of them are met, I would like that one to appear at the top of the table rather than the one that only has 1 out of 10 matches. Any ideas on how to do this?
Photo of Nate Gilbertson

Nate Gilbertson

  • 1,194 Points 1k badge 2x thumb

Posted 4 years ago

  • 1
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,704 Points 20k badge 2x thumb
Only way I can think of is create a field to store the number of matches. Something like "QueryMatchCount". Then, in theory using javascript, you could loop through the rows to get the number of matches. On each row you could loop through the conditions testing them with the values of the current row against the criteria. This shouldn't be too hard as the values are being set by you and should be in the condition as the value. Basically compare the value of the declared "Field" against the value in the "Value" field.

Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,704 Points 20k badge 2x thumb
I forgot to assign the value of QueryMatchCount to QueryMatchCount__c. Goes right before "dfd.resolve();"

Also, not sure if it's necessary to requery in order to get the order by on model to use this field.

Is this possible to sort the model without re-querying?
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,704 Points 20k badge 2x thumb
Sorry. Wrote it without a proper editor. Forgot $ = skuid.$ and there a misplaced ).

var myModel = skuid.$M('YOURMODELNAME'),
	    $ = skuid.$,
	    QueryMatchCount,
	    dfd = $.Deferred();

	// query in snippet
	$.when(myModel.updateData())
		.done(function(){

			// loop through rows
			$.each(myModel.getRows,function(r,currentRow){
		
				// loop through conditions
				$.each(myModel.conditions,function(i,condition){

					// if the value set in the condition equals the value of the corresponding on the current row
					if (condition.value == myModel.getFieldValue(currentRow,condition.field)) {
						QueryMatchCount++;
					}
				});
			});
			dfd.resolve(); 
		})
		.fail(function(){
			dfd.reject();
		});

	return dfd.promise();
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,704 Points 20k badge 2x thumb
Sorry. Wrote it without a proper editor. Forgot $ = skuid.$ and there a misplaced ).

var myModel = skuid.$M('YOURMODELNAME'),
	    $ = skuid.$,
	    QueryMatchCount,
	    dfd = $.Deferred();

	// query in snippet
	$.when(myModel.updateData())
		.done(function(){

			// loop through rows
			$.each(myModel.getRows,function(r,currentRow){
		
				// loop through conditions
				$.each(myModel.conditions,function(i,condition){

					// if the value set in the condition equals the value of the corresponding on the current row
					if (condition.value == myModel.getFieldValue(currentRow,condition.field)) {
						QueryMatchCount++;
					}
				});
			});
			dfd.resolve(); 
		})
		.fail(function(){
			dfd.reject();
		});

	return dfd.promise();
Photo of Nate Gilbertson

Nate Gilbertson

  • 1,194 Points 1k badge 2x thumb
Thank you for your response! 

I tried the code and when I pasted it in, I got an error message in the code saying that I needed another ) after myModel.getFieldValue(currentRow,condition.field) so I added one and the error message went away. However, when I tried to run the javascript on the page, I noticed that I got an error message that says, "Uncaught TypeError: Cannot read property 'Deferred' of undefined.

Any ideas of what I did wrong?



Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,704 Points 20k badge 2x thumb
Photo of Nate Gilbertson

Nate Gilbertson

  • 1,194 Points 1k badge 2x thumb
That worked great! Thank you for the fix. I just ran it and it went through the javascript, but it didn't populate QueryMatchCount__c with the value. Did I add QueryMatchCount__c in the right place? I added it up at the top right after QueryMatchCount


var myModel = skuid.$M('AlumniBestMatch'),    $ = skuid.$,
   QueryMatchCount,
   QueryMatchCount__c,
   dfd = $.Deferred();

// query in snippet
$.when(myModel.updateData())
.done(function(){

// loop through rows
$.each(myModel.getRows,function(r,currentRow){

// loop through conditions
$.each(myModel.conditions,function(i,condition){

// if the value set in the condition equals the value of the corresponding on the current row
if (condition.value == myModel.getFieldValue(currentRow,condition.field)) {
QueryMatchCount++;
}
});
});
dfd.resolve(); 
})
.fail(function(){
dfd.reject();
});

return dfd.promise();
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,704 Points 20k badge 2x thumb
var myModel = skuid.$M('YOURMODELNAME'),
    $ = skuid.$,
    QueryMatchCount,
    dfd = $.Deferred();

    // query in snippet
    $.when(myModel.updateData())
    	.done(function(){
    
    		// loop through rows
    		$.each(myModel.getRows,function(r,currentRow){
    	
    			// loop through conditions
    			$.each(myModel.conditions,function(i,condition){
    
    				// if the value set in the condition equals the value of the corresponding on the current row
    				if (condition.value == myModel.getFieldValue(currentRow,condition.field)) {
    					QueryMatchCount++;
    				}
    			});
    			myModel.updateRow(currentRow,{QueryMatchCount__c: QueryMatchCount});
    		});
    		$.when(myModel.save())
        	    .done(function(){
            		$.when(myModel.updateData())
                	    .done(function(){
                	        dfd.resolve();
                	    })
                	    .fail(function(){
                	        dfd.reject();
                	    });
        	    })
        	    .fail(function(){
        	        dfd.reject();
        	    });
    	})
    	.fail(function(){
    		dfd.reject();
    	});
    
    return dfd.promise();
Photo of Nate Gilbertson

Nate Gilbertson

  • 1,194 Points 1k badge 2x thumb
Thank you so much for your hard work and patience with this! When the table loads in the popup the QueryMatchCount column is still blank and even when I look at the records that come up after the query, all of the fields are blank for QueryMatchCount. Am I missing a step in the process?
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,704 Points 20k badge 2x thumb
I can a screen share a little later to run through it. Should work though. Just missing something small somewhere.
Photo of Nate Gilbertson

Nate Gilbertson

  • 1,194 Points 1k badge 2x thumb
Pat, you are a rockstar! Thank you so much for getting this to work!
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
We agree Nate.  Pat is a rock-star...