Reputation: 11
I have an "command" entity. In command entity I have a field called "command Text". It contains values like Hello User Please Reply <User Name> <User Age>
. I want to replace the dynamic values which is present with <> to actual values.
I have below code to replace the values, but it is not actually replacing.
I want the output like Hello User Please Reply Mathew 25
. Instead I'm getting the Hello User Please Reply Mathew <User Age> Hello User Please Reply <User Name> 25
.
I tried all the ways I can. But not able to fix it.
retrieveAndReplaceDynamicFieldValues: function (formContext, fieldName, commandText, English, NonEnglish, Language) {
var replacedValues = "";
var completedIterations = 0;
Xrm.WebApi.retrieveMultipleRecords("templatefield", "?$select=entity,fieldschemaname&$filter=name eq '" + fieldName + "'").then(
function success(templatefields) {
// Columns
templatefields.entities.forEach(function (TemplateResult) {
var entityName = TemplateResult["[email protected]"].toLowerCase();
var fieldSchemaName = TemplateResult["fieldschemaname"]; // Text
var RecordGUID = retrieveGuid(formContext, entityName);
Xrm.WebApi.retrieveRecord(entityName, RecordGUID, "?$select=" + fieldSchemaName).then(
function success(result) {
// Get the value of the field
var fieldValue = result[fieldSchemaName];
// Replace the field value in the command text
commandText = commandText.replace(new RegExp(fieldName, 'gi'), fieldValue);
replacedValues = commandText;
// Increment the completedIterations counter
completedIterations++;
// Check if this is the last iteration, then update SMS body
if (completedIterations === templatefields.entities.length) {
updateBody(English, NonEnglish, Language, commandText);
}
},
function error(error) {
console.error("Error retrieving Campaign record: " + error.message);
}
);
});
},
function error(error) {
console.error("Error retrieving SMS template field: " + error.message);
}
);
updateBody(English, English, Language, replacedValues);
},
In update body method I'm setting the replaced values. Always template filed count is 1 because we are having at least one dynamic value.
Upvotes: 0
Views: 129
Reputation: 324
It seems like you're attempting to replace dynamic values in a command text using values from related records. The issue might be related to asynchronous JavaScript execution. Your updateBody function is being called before the values are actually replaced in the commandText.
retrieveAndReplaceDynamicFieldValues: function (formContext, fieldName, commandText, English, NonEnglish, Language) {
var replacedValues = "";
var promises = [];
Xrm.WebApi.retrieveMultipleRecords("templatefield", "?$select=entity,fieldschemaname&$filter=name eq '" + fieldName + "'").then(
function success(templatefields) {
templatefields.entities.forEach(function (TemplateResult) {
var entityName = TemplateResult["[email protected]"].toLowerCase();
var fieldSchemaName = TemplateResult["fieldschemaname"];
var RecordGUID = retrieveGuid(formContext, entityName);
var promise = Xrm.WebApi.retrieveRecord(entityName, RecordGUID, "?$select=" + fieldSchemaName).then(
function success(result) {
var fieldValue = result[fieldSchemaName];
commandText = commandText.replace(new RegExp('<' + fieldName + '>', 'gi'), fieldValue);
},
function error(error) {
console.error("Error retrieving record: " + error.message);
}
);
promises.push(promise);
});
Promise.all(promises).then(function () {
updateBody(English, NonEnglish, Language, commandText);
});
},
function error(error) {
console.error("Error retrieving template fields: " + error.message);
}
);
},
updateBody: function (English, NonEnglish, Language, commandText) {
console.log(commandText);
}
Adjust the updateBody function to handle the replaced values as per your requirement.
Upvotes: 0