Dynamically adding row to table and selected entity related attributes to table dynamically using html in mscrm
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<meta charset="utf-8">
<title>Sample page</title>
<script src="../../ClientGlobalContext.js.aspx" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="http://managed.apy.com:5555/apyuthorities/WebResources/mtcsv_/Scripts/Jquery.1.8.2.min.js"></script>
<script src="http://managed.apy.com:5555/apyuthorities/WebResources/apy_Sdknew.js" type="text/javascript"></script>
<!--<script src="Scripts/SDK.MetaData.js" type="text/javascript"></script>-->
<script src="http://managed.apy.com:5555/apy/WebResources/apy_XrmSvcToolkit.js" type="text/javascript"></script>
<script type="text/javascript">
var results = null;
var message = null;
var alertFlag = null;
var attributecount = null;
var entityname = null;
var attributes = [];
var flag = true;
var onloads = true;
var addRow = true;
$(document).ready(function () {
retrieveTableData();
onloads = true;
buildTableData();
//onload = false;
});
function buildTableData() {
var tbody = "<div class='Record-table'><table id='tblData' cellpadding='0' cellspacing='0'><colgroup><col style='width:39px' /><col style='width:300px' /><col style='width:200px' /><col style='width:300px' /><col style='width:250px' /><col /></colgroup><tbody id='tbody'>";
if (flag) {
tbody += "<tr id='rowOne' class='rowdiv1'><td>Entity</td><td>Field</td><td>Direction</td><td>Table</td><td>Field</td><td><input type=button id=addRowButton onclick='addRowtoTable(this)' value='+' ><input type=button id=saveButton onclick='saveTable(this)' value='Save' ></td></tr>";
flag = false;
}
if (addRow) {
tbody += "<tr>";
tbody += "<td><select onchange='onSelectedEntity()' id='selectedEntity'><option value='invoice'>Invoice</option> <option value='invoicedetail'>Invoice Product</option></select></td>";
if (onloads) {
onSelectedEntity();
onloads = false;
}
tbody += "<td><select onchange='onSelectedAttribute()' id='selectedAttribute'><option>--Select--</option> </select></td>";
addRow = false;
}
tbody += "<td><input type='text' id='direction'></td>";
tbody += "<td><input type='text' id='entity'></td>";
tbody += "<td><input type='text' id='filed'></td>";
tbody += "</tr>";
tbody += "</tbody></table></div>"
$('#tabledata').append(tbody);
}
var count = 0;
var rowCount = 0;
function addRowtoTable(e) {
//var entNam = $(this)[0].entityName;
//if (entNam != null && entNam != undefined) {
// entityName = entNam;
//} else {
// entityName = "invoice";
//}
entityName = "invoice";
$("#addRowButton").prop('disabled', true);
$("#addRowButton").hide();
//setTimeout(function () { alert("Hello"); }, 5000);
count = $("tr").click(function () { });
rowCount = count.length;
addRow = true;
var tbody = "";
if (rowCount > 0) {
if (addRow) {
tbody += "<tr id='tr" + rowCount + "'>"
tbody += "<td><select onchange='onSelectedEntity0(this)' id='td" + rowCount + "'><option value='invoice'>Invoice</option> <option value='invoicedetail'>Invoice Product</option></select></td>";
addColumn(entityName);
addRow = false;
}
tbody += "<td><select onchange='onSelectedAttribute()' id='trr" + rowCount + "'><option>--Select--</option> </select></td>";
}
tbody += "<td><input type='text' id='direction" + rowCount + "'></td>";
tbody += "<td><input type='text' id='entity" + rowCount + "'></td>";
tbody += "<td><input type='text' id='filed" + rowCount + "'></td>";
tbody += "</tr>";
tbody += "</tbody></table></div>"
$('#tabledata').append(tbody);
$("#addRowButton").prop('disabled', false);
$("#addRowButton").show();
}
function addColumn(entityName) {
//var entNam = $(this)[0].entityName;
//if (entNam != null && entNam != undefined) {
// entityName = entNam;
//} else {
// entityName = "invoice";
//}
if (entityName != null && entityName != undefined && entityName != "") {
SDK.Metadata.RetrieveEntity(SDK.Metadata.EntityFilters.Attributes, entityName, null, false, function (entityMetadata) { successRetrieveEntityData(entityName, entityMetadata); }, errorRetrieveEntity);
}
}
function successRetrieveEntityData(logicalName, entityMetadata) {
attributecount = null;
attributes = [];
attributecount = entityMetadata.Attributes.length;
entityMetadata.Attributes.sort(function (a, b) {
if (a.LogicalName < b.LogicalName)
{ return -1 }
if (a.LogicalName > b.LogicalName)
{ return 1 }
return 0;
});
var attribute = null;
//attribute += "<td><select id='trr" + rowCount + "'>";
//attribute += "<option value='select'>--Select--</option>";
for (var i = 0; i < attributecount; i++) {
attributes.push(entityMetadata.Attributes[i].LogicalName);
attribute += "<option value='td1" + attributes[i] + "'>" + attributes[i] + "</option>";
}
// attribute += "</select></td>";
$('#trr' + rowCount).append(attribute);
// setTimeout(function () { alert("Hello"); }, 5000);
}
var tdId = null;
function onSelectedEntity0(e) {
if ($(e)[0] != undefined && $(e)[0] != null) {
if ($(e)[0].value != undefined && $(e)[0].value != null) {
var selectEnt = $(e)[0].value;
tdId = $(e)[0].id.split("td")[1];
SDK.Metadata.RetrieveEntity(SDK.Metadata.EntityFilters.Attributes, selectEnt, null, false, function (entityMetadata) { successRetrieveSelectedEntity(selectEnt, entityMetadata); }, errorRetrieveEntity);
}
}
}
function successRetrieveSelectedEntity(logicalName, entityMetadata) {
$("#trr" + tdId).empty();
attributecount = null;
attributes = [];
attributecount = entityMetadata.Attributes.length;
entityMetadata.Attributes.sort(function (a, b) {
if (a.LogicalName < b.LogicalName)
{ return -1 }
if (a.LogicalName > b.LogicalName)
{ return 1 }
return 0;
});
var att = "";
att += "<option value='select'>--Select--</option>";
for (var i = 0; i < attributecount; i++) {
attributes.push(entityMetadata.Attributes[i].LogicalName);
att += "<option value='" + attributes[i] + "'>" + attributes[i] + "</option>";
}
if (tdId != null && tdId != undefined) {
$('#trr' + tdId).append(att);
// setTimeout(function () { alert("Hello"); }, 5000);
}
}
function onSelectedEntity() {
$("#selectedAttribute").empty();
if (onloads) {
entityName = "invoice";
onloads = false;
} else {
var a = $("#selectedEntity option:selected")[0].value;
if (a != null && a != undefined) {
entityName = a;
} else {
entityName = "invoice";
}
}
if (entityName != null && entityName != undefined && entityName != "") {
SDK.Metadata.RetrieveEntity(SDK.Metadata.EntityFilters.Attributes, entityName, null, false, function (entityMetadata) { successRetrieveEntity(entityName, entityMetadata); }, errorRetrieveEntity);
}
}
function successRetrieveEntity(logicalName, entityMetadata) {
attributecount = null;
attributes = [];
attributecount = entityMetadata.Attributes.length;
entityMetadata.Attributes.sort(function (a, b) {
if (a.LogicalName < b.LogicalName)
{ return -1 }
if (a.LogicalName > b.LogicalName)
{ return 1 }
return 0;
});
var att = "";
if (!onloads) {
att += "<option value='select'>--Select--</option>";
}
for (var i = 0; i < attributecount; i++) {
attributes.push(entityMetadata.Attributes[i].LogicalName);
att += "<option value='" + attributes[i] + "'>" + attributes[i] + " </option>";
}
// setTimeout(function () { alert("Hello"); }, 5000);
$('#selectedAttribute').append(att);
}
function errorRetrieveEntity(error) {
alert("Error!");
}
function saveTable() {
debugger;
var tblRows = "";
$("#tabledata tr").each(function () {
var cells = $(this).find("td");
tblRows += "<tr>";
if (cells.length > 0) {
$(cells).each(function () {
var cellData = "";
if ($(this).find("select").length > 0) {
cellData = $(this).find("select").val();
}
else if ($(this).find("input").length > 0) {
cellData = $(this).find("input").val();
}
else {
cellData = $(this).html();
}
tblRows += "\t<td>" + cellData + "</td>\n";
});
}
else {
cells = $(this).find("th");
if (cells.length > 0) {
$(cells).each(function () {
xml += "\t<td>" + $(this).html() + "</td>\n";
});
}
xml += "</tr>\n";
}
tblRows += "</tr>";
});
}
function saveTablegfghgvjgh() {
debugger;
// $("#saveButton").click(function () {
var xml = "";
$("#tabledata tr").each(function () {
var cells = $(this).find("td");
xml += "<schedule name='" + cells.eq(0).text() + "'>\n";
if (cells.length > 0) {
$(cells).each(function () {
var cellData = "";
if ($(this).find("select").length > 0) {
cellData = $(this).find("select").val();
}
else if ($(this).find("input").length > 0) {
cellData = $(this).find("input").val();
}
else {
cellData = $(this).html();
}
xml += "\t<data>" + cellData + "</data>\n";
});
xml += "</schedule>\n";
}
else {
cells = $(this).find("th");
if (cells.length > 0) {
$(cells).each(function () {
xml += "\t<data>" + $(this).html() + "</data>\n";
});
}
xml += "</schedule>\n";
}
//var cells = $("td", this);
//if (cells.length > 0) {
// xml += "<schedule name='" + cells.eq(0).text() + "'>\n";
// for (var i = 1; i < cells.length; ++i) {
// debugger;
// xml += "\t<data>" + cells.eq(i).text() + "</data>\n";
// }
// xml += "</schedule>\n";
//}
});
var entity = {};
entity.apy_xmldata = xml;
// window.alert(xml);
//XrmSvcToolkit.createRecord({
// entityName: "apy_cwmapping",
// entity: entity,
// async: false,
// successCallback: function (result) {
// alert("success");
// var newEntityId = result.AccountId;
// },
// errorCallback: function (error) {
// alert(error.message);
// }
//});
XrmSvcToolkit.updateRecord({
entityName: "apy_cwmapping",
id: "71BBA68E-393E-E811-B47A-005056934FB9",
entity: entity,
async: false,
successCallback: function () {
alert("Updated"); //Success - No Return Data - Do Something
},
errorCallback: function (error) {
alert(error.message);
},
});
//});
}
function retrieveTableData() {
debugger;
XrmSvcToolkit.retrieve({
entityName: "apy_cwmapping",
id: "71BBA68E-393E-E811-B47A-005056934FB9",
select: ["apy_xmldata"],
//expand: ['user_accounts'],
async: false,
successCallback: function (result) {
debugger;
alert("retrieve success");
var apy_cwmapping = result.apy_cwmapping;
},
errorCallback: function (error) {
alert(error.message);
}
});
}
</script>
<div class="wrapper">
<div id="tabledata" class="table-data">
<div style="background-color: #f3f3f3;">
<table cellpadding="0" cellspacing="0" id="tblSPFolderConfig" style="table-layout: fixed; width: 100%;">
<colgroup>
<col style="width: 300px;">
<col style="width: 200px;">
<col style="width: 300px;">
</colgroup>
<thead>
<tr>
<th>CRM</th>
<th></th>
<th>Connect Wise</th>
</tr>
</thead>
</table>
</div>
</div>
</body>
</html>
<head></head>
<body>
<meta charset="utf-8">
<title>Sample page</title>
<script src="../../ClientGlobalContext.js.aspx" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="http://managed.apy.com:5555/apyuthorities/WebResources/mtcsv_/Scripts/Jquery.1.8.2.min.js"></script>
<script src="http://managed.apy.com:5555/apyuthorities/WebResources/apy_Sdknew.js" type="text/javascript"></script>
<!--<script src="Scripts/SDK.MetaData.js" type="text/javascript"></script>-->
<script src="http://managed.apy.com:5555/apy/WebResources/apy_XrmSvcToolkit.js" type="text/javascript"></script>
<script type="text/javascript">
var results = null;
var message = null;
var alertFlag = null;
var attributecount = null;
var entityname = null;
var attributes = [];
var flag = true;
var onloads = true;
var addRow = true;
$(document).ready(function () {
retrieveTableData();
onloads = true;
buildTableData();
//onload = false;
});
function buildTableData() {
var tbody = "<div class='Record-table'><table id='tblData' cellpadding='0' cellspacing='0'><colgroup><col style='width:39px' /><col style='width:300px' /><col style='width:200px' /><col style='width:300px' /><col style='width:250px' /><col /></colgroup><tbody id='tbody'>";
if (flag) {
tbody += "<tr id='rowOne' class='rowdiv1'><td>Entity</td><td>Field</td><td>Direction</td><td>Table</td><td>Field</td><td><input type=button id=addRowButton onclick='addRowtoTable(this)' value='+' ><input type=button id=saveButton onclick='saveTable(this)' value='Save' ></td></tr>";
flag = false;
}
if (addRow) {
tbody += "<tr>";
tbody += "<td><select onchange='onSelectedEntity()' id='selectedEntity'><option value='invoice'>Invoice</option> <option value='invoicedetail'>Invoice Product</option></select></td>";
if (onloads) {
onSelectedEntity();
onloads = false;
}
tbody += "<td><select onchange='onSelectedAttribute()' id='selectedAttribute'><option>--Select--</option> </select></td>";
addRow = false;
}
tbody += "<td><input type='text' id='direction'></td>";
tbody += "<td><input type='text' id='entity'></td>";
tbody += "<td><input type='text' id='filed'></td>";
tbody += "</tr>";
tbody += "</tbody></table></div>"
$('#tabledata').append(tbody);
}
var count = 0;
var rowCount = 0;
function addRowtoTable(e) {
//var entNam = $(this)[0].entityName;
//if (entNam != null && entNam != undefined) {
// entityName = entNam;
//} else {
// entityName = "invoice";
//}
entityName = "invoice";
$("#addRowButton").prop('disabled', true);
$("#addRowButton").hide();
//setTimeout(function () { alert("Hello"); }, 5000);
count = $("tr").click(function () { });
rowCount = count.length;
addRow = true;
var tbody = "";
if (rowCount > 0) {
if (addRow) {
tbody += "<tr id='tr" + rowCount + "'>"
tbody += "<td><select onchange='onSelectedEntity0(this)' id='td" + rowCount + "'><option value='invoice'>Invoice</option> <option value='invoicedetail'>Invoice Product</option></select></td>";
addColumn(entityName);
addRow = false;
}
tbody += "<td><select onchange='onSelectedAttribute()' id='trr" + rowCount + "'><option>--Select--</option> </select></td>";
}
tbody += "<td><input type='text' id='direction" + rowCount + "'></td>";
tbody += "<td><input type='text' id='entity" + rowCount + "'></td>";
tbody += "<td><input type='text' id='filed" + rowCount + "'></td>";
tbody += "</tr>";
tbody += "</tbody></table></div>"
$('#tabledata').append(tbody);
$("#addRowButton").prop('disabled', false);
$("#addRowButton").show();
}
function addColumn(entityName) {
//var entNam = $(this)[0].entityName;
//if (entNam != null && entNam != undefined) {
// entityName = entNam;
//} else {
// entityName = "invoice";
//}
if (entityName != null && entityName != undefined && entityName != "") {
SDK.Metadata.RetrieveEntity(SDK.Metadata.EntityFilters.Attributes, entityName, null, false, function (entityMetadata) { successRetrieveEntityData(entityName, entityMetadata); }, errorRetrieveEntity);
}
}
function successRetrieveEntityData(logicalName, entityMetadata) {
attributecount = null;
attributes = [];
attributecount = entityMetadata.Attributes.length;
entityMetadata.Attributes.sort(function (a, b) {
if (a.LogicalName < b.LogicalName)
{ return -1 }
if (a.LogicalName > b.LogicalName)
{ return 1 }
return 0;
});
var attribute = null;
//attribute += "<td><select id='trr" + rowCount + "'>";
//attribute += "<option value='select'>--Select--</option>";
for (var i = 0; i < attributecount; i++) {
attributes.push(entityMetadata.Attributes[i].LogicalName);
attribute += "<option value='td1" + attributes[i] + "'>" + attributes[i] + "</option>";
}
// attribute += "</select></td>";
$('#trr' + rowCount).append(attribute);
// setTimeout(function () { alert("Hello"); }, 5000);
}
var tdId = null;
function onSelectedEntity0(e) {
if ($(e)[0] != undefined && $(e)[0] != null) {
if ($(e)[0].value != undefined && $(e)[0].value != null) {
var selectEnt = $(e)[0].value;
tdId = $(e)[0].id.split("td")[1];
SDK.Metadata.RetrieveEntity(SDK.Metadata.EntityFilters.Attributes, selectEnt, null, false, function (entityMetadata) { successRetrieveSelectedEntity(selectEnt, entityMetadata); }, errorRetrieveEntity);
}
}
}
function successRetrieveSelectedEntity(logicalName, entityMetadata) {
$("#trr" + tdId).empty();
attributecount = null;
attributes = [];
attributecount = entityMetadata.Attributes.length;
entityMetadata.Attributes.sort(function (a, b) {
if (a.LogicalName < b.LogicalName)
{ return -1 }
if (a.LogicalName > b.LogicalName)
{ return 1 }
return 0;
});
var att = "";
att += "<option value='select'>--Select--</option>";
for (var i = 0; i < attributecount; i++) {
attributes.push(entityMetadata.Attributes[i].LogicalName);
att += "<option value='" + attributes[i] + "'>" + attributes[i] + "</option>";
}
if (tdId != null && tdId != undefined) {
$('#trr' + tdId).append(att);
// setTimeout(function () { alert("Hello"); }, 5000);
}
}
function onSelectedEntity() {
$("#selectedAttribute").empty();
if (onloads) {
entityName = "invoice";
onloads = false;
} else {
var a = $("#selectedEntity option:selected")[0].value;
if (a != null && a != undefined) {
entityName = a;
} else {
entityName = "invoice";
}
}
if (entityName != null && entityName != undefined && entityName != "") {
SDK.Metadata.RetrieveEntity(SDK.Metadata.EntityFilters.Attributes, entityName, null, false, function (entityMetadata) { successRetrieveEntity(entityName, entityMetadata); }, errorRetrieveEntity);
}
}
function successRetrieveEntity(logicalName, entityMetadata) {
attributecount = null;
attributes = [];
attributecount = entityMetadata.Attributes.length;
entityMetadata.Attributes.sort(function (a, b) {
if (a.LogicalName < b.LogicalName)
{ return -1 }
if (a.LogicalName > b.LogicalName)
{ return 1 }
return 0;
});
var att = "";
if (!onloads) {
att += "<option value='select'>--Select--</option>";
}
for (var i = 0; i < attributecount; i++) {
attributes.push(entityMetadata.Attributes[i].LogicalName);
att += "<option value='" + attributes[i] + "'>" + attributes[i] + " </option>";
}
// setTimeout(function () { alert("Hello"); }, 5000);
$('#selectedAttribute').append(att);
}
function errorRetrieveEntity(error) {
alert("Error!");
}
function saveTable() {
debugger;
var tblRows = "";
$("#tabledata tr").each(function () {
var cells = $(this).find("td");
tblRows += "<tr>";
if (cells.length > 0) {
$(cells).each(function () {
var cellData = "";
if ($(this).find("select").length > 0) {
cellData = $(this).find("select").val();
}
else if ($(this).find("input").length > 0) {
cellData = $(this).find("input").val();
}
else {
cellData = $(this).html();
}
tblRows += "\t<td>" + cellData + "</td>\n";
});
}
else {
cells = $(this).find("th");
if (cells.length > 0) {
$(cells).each(function () {
xml += "\t<td>" + $(this).html() + "</td>\n";
});
}
xml += "</tr>\n";
}
tblRows += "</tr>";
});
}
function saveTablegfghgvjgh() {
debugger;
// $("#saveButton").click(function () {
var xml = "";
$("#tabledata tr").each(function () {
var cells = $(this).find("td");
xml += "<schedule name='" + cells.eq(0).text() + "'>\n";
if (cells.length > 0) {
$(cells).each(function () {
var cellData = "";
if ($(this).find("select").length > 0) {
cellData = $(this).find("select").val();
}
else if ($(this).find("input").length > 0) {
cellData = $(this).find("input").val();
}
else {
cellData = $(this).html();
}
xml += "\t<data>" + cellData + "</data>\n";
});
xml += "</schedule>\n";
}
else {
cells = $(this).find("th");
if (cells.length > 0) {
$(cells).each(function () {
xml += "\t<data>" + $(this).html() + "</data>\n";
});
}
xml += "</schedule>\n";
}
//var cells = $("td", this);
//if (cells.length > 0) {
// xml += "<schedule name='" + cells.eq(0).text() + "'>\n";
// for (var i = 1; i < cells.length; ++i) {
// debugger;
// xml += "\t<data>" + cells.eq(i).text() + "</data>\n";
// }
// xml += "</schedule>\n";
//}
});
var entity = {};
entity.apy_xmldata = xml;
// window.alert(xml);
//XrmSvcToolkit.createRecord({
// entityName: "apy_cwmapping",
// entity: entity,
// async: false,
// successCallback: function (result) {
// alert("success");
// var newEntityId = result.AccountId;
// },
// errorCallback: function (error) {
// alert(error.message);
// }
//});
XrmSvcToolkit.updateRecord({
entityName: "apy_cwmapping",
id: "71BBA68E-393E-E811-B47A-005056934FB9",
entity: entity,
async: false,
successCallback: function () {
alert("Updated"); //Success - No Return Data - Do Something
},
errorCallback: function (error) {
alert(error.message);
},
});
//});
}
function retrieveTableData() {
debugger;
XrmSvcToolkit.retrieve({
entityName: "apy_cwmapping",
id: "71BBA68E-393E-E811-B47A-005056934FB9",
select: ["apy_xmldata"],
//expand: ['user_accounts'],
async: false,
successCallback: function (result) {
debugger;
alert("retrieve success");
var apy_cwmapping = result.apy_cwmapping;
},
errorCallback: function (error) {
alert(error.message);
}
});
}
</script>
<div class="wrapper">
<div id="tabledata" class="table-data">
<div style="background-color: #f3f3f3;">
<table cellpadding="0" cellspacing="0" id="tblSPFolderConfig" style="table-layout: fixed; width: 100%;">
<colgroup>
<col style="width: 300px;">
<col style="width: 200px;">
<col style="width: 300px;">
</colgroup>
<thead>
<tr>
<th>CRM</th>
<th></th>
<th>Connect Wise</th>
</tr>
</thead>
</table>
</div>
</div>
</body>
</html>
Comments
Post a Comment