Download PDF
Download page Find and Replace E-mail Domain Strings.
Find and Replace E-mail Domain Strings
Custom Action Name
AG_Generic_FindReplace
Objects affected
Any object with an email field that is editable
Description
Use this custom action to modify the domain of email fields of selected records. Use from any object with an editable email field.
Use Cases
Modify a group of selected records' email domains if they were to change companies or email services.
Steps
/** * @author CRMCulture * @version 1.02 * @description Generic find replace for all text, textarea, phone, and email fields. Loops through each row locating the specified value and replaces it with the new value. Works in a ActionGrid with or without edit mode enabled. * * @id AG_Generic_FindReplace ActionGrid custom action id that is added to the name field. * @param kendoEntry Conga Grid helper for Kendo UI modal entry forms. * @param kendoPopup ActionGrid helper for Kendo UI model message windows. * @param sfdc ActionGrid helper for Salesforce REST API's*/crmc.require(["KendoEntry", "KendoPopup", "sfdc"], function(kendoEntry, kendoPopup, sfdc){ crmc.addCustomAction({ "itemID": "AG_Generic_FindReplace", getLabel: function(context) { return "Find/Replace"; }, isAvailable: function(context) { return true; }, click: function(context) { var fieldMeta = context.actionGrid.fieldMeta; var rows = context.selectedRows; var recordIds = []; var onfailure = function(error) { try{ return "<li> Status Code: " + error.statusCode + ", Message: " + error.message + "</li>"; } catch (err){ alert("Error: " + err); } }; var onsuccess = function(results){ var updateResults = ["<style>div.overflow{overflow: scroll; max-height: 600px;}</style><div><div class='overflow'><ul>"]; $.each(results, function(i, row) { if (row.errors) { updateResults.push(onfailure(row.errors)); return; } recordIds.push(row.id); }); if (results.length == recordIds.length) { var TEXT_MESSAGE = kendo.format("{0} Record" + (recordIds.length === 1 ? "" : "s") + " updated, would you like to Conga Grid the result" + (recordIds.length === 1 ? "" : "s") + "?", recordIds.length); var TEXT_BATCH_ADD_RESULTS = "Update Results"; var buttons = [{ label: "Yes", click: function() { window.open(kendo.format("/apex/CRMC_PP__crmc_grid?object={0}&Ids={1}", context.objectDescribe.name, recordIds.join())); } }, { label: "No", click: function() { context.actionGrid.refresh(); } }]; kendoPopup.popupWithButtons(TEXT_BATCH_ADD_RESULTS, TEXT_MESSAGE, buttons); context.actionGrid.refresh(); } else{ updateResults.push("</ul></div></div>"); kendoPopup.popupWithButtons("Batch Update Errors", updateResults.join(""), ErrorButtons(results), {width: 700}); } } function ErrorButtons (results){ return [ { label: "Send To Console", click: function() { $.each(results, function(i, row) { console.log(row.errors); }); } }, { label: "Ok" } ]; } function ButtonOk (){ return [ { label: "Ok" } ]; } function GetColumns(){ var columns = []; for (var i = 0; i < fieldMeta.length; i++) { if(IsType(fieldMeta[i].type) && fieldMeta[i].name.indexOf(".") === -1){ columns.push({active: "true", defaultValue: "false", label: fieldMeta[i].displayName, value: fieldMeta[i].name}); } }; return columns; }; function IsType(type){ switch (type) { case "STRING": return true; case "TEXTAREA": return true; case "PHONE": return true; case "EMAIL": return true; default: return false; } } function BatchUpdate(sObject, resultFunction) { if (!(sObject instanceof Array)) sObject = [sObject]; var limit = 200; var i; var resultsList = []; var numBatches = sObject.length / 200; var numCompletedBatches = 0; var handlerFunction = function(results) { resultsList = resultsList.concat(results); numCompletedBatches++; if (numCompletedBatches >= numBatches) { if (typeof(resultFunction) === "function") { resultFunction(resultsList); } } }; for (i = 0; i < sObject.length; i += limit) { sforce.connection.update(sObject.slice(i, i + limit), { onSuccess: handlerFunction, onFailure: handlerFunction, }); } } function Replace(values){ var updates = []; for (var i = 0; i < rows.length; i++){ //Create a record to hold update. var record = new sforce.SObject(context.objectDescribe.name); //Check if the user wants to replace empty value with new value. if(rows[i][values.apiValue] === undefined && values.oldValue === ""){ record.Id = rows[i].Id; record[values.apiValue] = values.newValue; updates.push(record); } else if(rows[i][values.apiValue] != undefined && values.oldValue != ""){ var string = rows[i][values.apiValue].replace(values.oldValue, values.newValue); //Check if there was a change. if(string != rows[i][values.apiValue]){ record.Id = rows[i].Id; record[values.apiValue] = string; updates.push(record); } } } if(updates.length > 0){ if(updates.length > 200){ BatchUpdate(updates, onsuccess); } else { sforce.connection.update(updates, { onSuccess: onsuccess, onFailure: onfailure }); } } } function EditableReplace(values){ for (var i = 0; i < rows.length; i++) { var ColumnIndex = function(){ for (var columnIndex = 0; columnIndex < context.kendoGrid.columns.length; columnIndex++) { if(context.kendoGrid.columns[columnIndex].field === values.apiValue) return columnIndex; } } var field = context.kendoGrid.columns[i]; var cell = $(kendo.format("{2} tr[data-uid={0}] td:not(.k-group-cell,.k-hierarchy-cell):eq({1})", rows[i].uid, ColumnIndex(), context.actionGrid.getElementId("grid"))); //Check if the user wants to replace empty value with new value. if(rows[i][values.apiValue] === undefined && values.oldValue === ""){ context.kendoGrid.editCell(cell); rows[i].set(values.apiValue, values.newValue); context.kendoGrid.editCell(cell); } else if(rows[i][values.apiValue] != undefined && values.oldValue != ""){ var string = rows[i][values.apiValue].replace(values.oldValue, values.newValue); //Check if there was a change. if(string != rows[i][values.apiValue]){ context.kendoGrid.editCell(cell); rows[i].set(values.apiValue, string); context.kendoGrid.editCell(cell); } } }; } var fields = GetColumns(); if(fields.length > 0){ kendoEntry.entry("Replace With...", [ {name: "Columns", label: 'Column', type: "picklist", values: fields, required: true}, {name: "OldValue", label: 'Old Value', type: "text", required: true}, {name: "NewValue", label: 'New Value', type: "text", required: true} ], {width: 350}, null, function(selectedValues) { //Does the editable property exist? Is ActionGrid in edit mode? var values = {apiValue: selectedValues["Columns"].value, oldValue: selectedValues["OldValue"].value, newValue: selectedValues["NewValue"].value}; context.actionGrid.settings.hasOwnProperty("editable") && context.actionGrid.settings.editable ? EditableReplace(values) : Replace(values); } ); } else { kendoPopup.popupWithButtons("No Supported Fields", "There are no supported fields in the view.", ButtonOk(), {width: 250}); } } });});