Code snippet to render a field if any child record of custom object has a value of true in a custom field?

  • 1
  • Question
  • Updated 2 years ago
  • In Progress
I have object Case.
Case has child object Experience

I want to show Case field "order number" if any experiences attached to the case have the field "show order number" checked as true.

The current render I can find only checks the first row. I need js to check all rows.
Photo of Brandon

Brandon

  • 306 Points 250 badge 2x thumb

Posted 2 years ago

  • 1
Photo of Emily Davis

Emily Davis, Employee

  • 3,502 Points 3k badge 2x thumb
Hey, Brandon,
To do this, first you'll need to include the child relationship for the Experience object in the Case model, if you haven't done so already. This tutorial explains how to do this:
http://help.skuid.com/m/models-conditions-filters/l/102520-include-child-relationships

Your snippet would be called as a custom field renderer on the Order Number field, and would look something like this:

var CHILD_RELATIONSHIP_NAME = 'Experiences__r', // Replace with the name of your child relationship
    CHILD_FIELD_NAME; // Replace with the name of the field on the child relationship you want to reference

var field = arguments[0],
	value = arguments[1],
	$ = skuid.$,
	caseModel = field.model,
	caseRow = field.row,
	experiences = [];

// Get the experiences child relationship
if (caseModel.getFieldValue(caseRow, CHILD_RELATIONSHIP_NAME)) { 
    experiences = caseModel.getFieldValue(caseRow, CHILD_RELATIONSHIP_NAME).records;
}

// Loop through child records; check if the "Show Order Number" field is true for any of them
var showOrderNumber = false;
if (experiences.length > 0) {
    $.each(experiences, function(i, record) {
        if (record.CHILD_FIELD_NAME === true) {
            showOrderNumber = true;
        }
    });
}

if (showOrderNumber === true) {
    // Run the standard field renderer
    skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);
} else {
    // Do nothing
}

Let me know how that works.
Emily
Photo of Brandon

Brandon

  • 306 Points 250 badge 2x thumb
I created the snippet and attached it to the Case Order_Number. I set field renderer to this javascript under "Custom (run a Snippet)"

The page loads, however order number is showing when it shouldn't. No experiences have a true "Order_Number_Show__c" .

var CHILD_RELATIONSHIP_NAME = 'Experiences__r', // Replace with the name of your child relationship
    Order_Number_Show__c; // Replace with the name of the field on the child relationship you want to reference

var field = arguments[0],
	value = arguments[1],
	$ = skuid.$,
	caseModel = field.model,
	caseRow = field.row,
	experiences = [];

// Get the experiences child relationship
if (caseModel.getFieldValue(caseRow, CHILD_RELATIONSHIP_NAME)) { 
    experiences = caseModel.getFieldValue(caseRow, CHILD_RELATIONSHIP_NAME).records;
}

// Loop through child records; check if the "Show Order Number" field is true for any of them
var showOrderNumber = false;
if (experiences.length > 0) {
    $.each(experiences, function(i, record) {
        if (record.Order_Number_Show__c === true) {
            showOrderNumber = true;
        }
    });
}

if (showOrderNumber === true) {
    // Run the standard field renderer
    skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);
} else {
    // Do nothing
}
Photo of Brandon

Brandon

  • 306 Points 250 badge 2x thumb
Is this code only going to showOrderNumber on non-deleted experiences related to the case?
Photo of Emily Davis

Emily Davis, Employee

  • 3,502 Points 3k badge 2x thumb
Brandon,
First, I think I gave you some bad JS. Update the first part of your code with this:
var CHILD_RELATIONSHIP_NAME = 'Experiences__r', // Replace with the name of your child relationship
    CHILD_FIELD_NAME = 'Order_Number_Show__c'; // Replace with the name of the field on the child relationship you want to reference
And replace

if (record.Order_Number_Show__c === true)

with

if (record[CHILD_FIELD_NAME] === true)

In that code, you will want to replace 'Order_Number_Show__c' with the API name of the "Show Order Number" checkbox field you were referring to earlier on your child relationship. Does that make sense?

Thanks!
Emily