K-field examples

The following sections lists some example calculations for K-fields.

For JavasScript best practices, a reference of JavaScript functions in Medallia Experience Cloud, and for more tips about how to use the editor, see Programming idioms.

Tip: The K-field library in the JavaScript editor also provides some common K-field scripts.

Age

Returns the age when birth date is provided. See K-field calculation optimization tips for more information about date/time calculations.

(function() {
var a = e_companyname_cust_birth_date;
 if (a == null) {
null;
}
 var birth = date(a);
 var today = date(e_creationdate);
 var years = new org.joda.time.Period(birth, today);
 return years.getYears();
})();

Alert resolution time

Calculates the Alert resolution time:

(function() {
 var c = a_Alert_date_created;
 var r = a_Alert_date_closed;
 return daysDifferenceByMidnights(c, r);
})();

Average of questions

Returns the average value of three survey questions:

avg(numeric(q_ov_experience), numeric(q_ov_service), numeric(q_ov_accommodations))

Average of 5 fields, avoiding a NaN (not an integer) error ticket

When all of the values in an average calculation are null (no value), the avg() function returns a NaN error, which Experience Cloud. This function returns a null value instead:

(function() {
    var a = numeric(q_nci_sales_sp_overall);
    var b = numeric(q_nci_sales_fn_overall);
    var c = numeric(q_nci_sales_del_overall);
    var d = numeric(q_nci_sales_ovdeal_ovpurchexp);
    var e = numeric(q_nci_sales_makedeal_purch_deal);
    var output = avg(a, b, c, d, e);
    if ( isNaN(output) ) return null;
    return output;
})();

"Brand code" for a unit

To get the Brand code Unit group Data field of the Brand Unit Group for a unit:

getUnitGroupDatafield(e_unitid, 'Brand', 3, 'brandcode')

Customer full name

Returns the full name of the user by merging title, first name, and last name:

(function() {
  var title = e_companyname_title_txt;
  var fullname = title + “ ” + e_firstname + “ ” + e_lastname;
  return fullname;
})();

"Customer loyalty" based on other fields

This returns an integer indicating company-specific value corresponding to the count of nights the customer stayed with the company:

(function() {
  var v = q_nights_business + q_nights_pleasure;
  if (v >= 50) { return 1; }
  else if (between(v, 20, 49)) { return 2; }
  else if (between(v, 10, 19)) { return 3; }
  else { return 4; }
})();

Email filter

Returns Yes if the email address contains specific domains:

(function() {
  var str=e_email;
  if (str.search(".company1.com") != -1 || str.search(".company2.com") != -1) {
    return 1;
  else{
    return 2;
  }
})();

K-field in Alerts

A company needs an Alert to be trigger when for a both a low-score survey and Social media responses. Rather than creating separate Alerts, create a K-field to reference either the Experience Feedback field (for the survey response) or the Overall Score System field (for the Social Feedback response), and return Yes if the score for either was below a certain threshold:

(function () {
  var smSource = String(a_social_media_source_enum);
  var overall_survey = numeric (q_hotelexp_ov_recommend);
  var overall_social = numeric (a_overall_score_with_social_media_5_buckets);

  // Survey
  if (!smSource) {
    if ( q_hotelexp_ov_recommend == 1 || q_hotelexp_ov_recommend == 2 || q_hotelexp_ov_recommend == 3 ) return 1;
    else return 2;
  }
  // Social
  else {
    if (overall_social == 1 || overall_social == 2 || overall_social == 3) return 1;
    else return 2;
  }
})();
Warning: If the company wants to check the Alert success rate for surveys sent (multiple records), use an R-field instead. The numbers of individual responses are calculated with the help of K-fields, and then the Alert success rate for the entire program is calculated with the R-field.

Profanity filter

A company needs to mask, or censor, existing profane words or phrases from comment fields. This K-field locates profane words in a comment field, and replaces them with asterisks when viewing the K-field. In reports, reference the K-field instead of the actual comment field. There needs to be one K-field profanity filter for each comment field to censor.

Experience Cloud does not provide a list of undesirable words; it is up to the company to define the what they want to censor. Include the words and phrases in the profanity_list array.

// profanity filter
(function() {

    var RegExp = this['RegExp']; // needed to access the RegExp constructor

    // Identify the comment Q-field here
    var comment = q_example_comment;
    if (comment === null) {
        return null;
    }
    // Define a list of words you would like to censor
    var profanity_list = ["pineapples", "guava", "apple"];

    // Create a regular expression to search for the words in the list
    var profanity_exp = new RegExp("(\\b)(" + profanity_list.join("|") + ")(\\b)","ig");

    // Replace the filtered words in the comment with asterisks
    var clean_comment = comment.replace(profanity_exp, function (word) {
        return word.replace(/./g,'*');
    });
    
    return clean_comment;
})();

Notice the regular expression is configured to match with word boundaries — this protects against accidentally filtering parts of non-profane words (if you filter "foo" it will not replace "food" with "***d"), but it does mean the profanity list should account for plurals, misspellings, and common misspellings.

Survey bounced or failed

Checks whether a particular survey has bounced/failed.

(function() {
var s = intFromPossibleEnum(e_status);
if (seqnum(e_survey_method) != 4)
{ 
if (s == 10) 
return 1;
if (s == 11) 
return 11;
else return null;  }
if (seqnum(e_survey_method) == 4)  
return null;
})();

Unit data field

To retrieve the value of a Data field named roomcount:

getUnitDatafield(e_unitid, 'roomcount')

Unit is a member of a unit group

Returns Yes (true) when the unit is in the group identified by Group1:

isMemberOfUnitGroup(e_unitid, Group1)

Compare two dates without time components

To compare two objects — not datetime — use the functions before() and after(), which return true or false.

Tip: These functions return false if the values are equal.
(function () {
	var today = new Date();
	var departureDate = e_plato_departuredate_date;

	if (today.after(departureDate)) {
	return "Departure date is in the past";
	}
}());