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

edited May 10, 2017 in Questions
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.
Tagged:

Comments

  • Emily DavisEmily Davis ✭✭✭✭
    edited April 28, 2017
    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
  • edited May 10, 2017
    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" .image
    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  }
  • edited May 2, 2017
    Is this code only going to showOrderNumber on non-deleted experiences related to the case?
  • Emily DavisEmily Davis ✭✭✭✭
    edited May 10, 2017
    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
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!