hi I have some javascript code which is used to take contract pieces of information and items and create an invoice with those very items.
Though I am not quite sure whether this approach is the best for creating a new record from one entity into another.
My code:
var currentYear = new Date().getFullYear(); var currentStart = new Date(currentYear, 0, 1); //currentStart.setHours(0,0,0,0); var currentEnd = new Date(currentYear, 11, 31); //currentEnd.setHours(0,0,0,0); function start() { var ID = parent.Xrm.Page.data.entity.getId().substring(1, 37); var fetchxml = ` `; var encodedFetchXML = encodeURIComponent(fetchxml); var queryPath = "/api/data/v8.2/contractdetails?fetchXml=" encodedFetchXML; var requestPath = parent.Xrm.Page.context.getClientUrl() queryPath; var req = new XMLHttpRequest(); req.open("GET", requestPath, true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); req.onreadystatechange = function () { if (this.readyState === 4) { this.onreadystatechange = null; if (this.status === 200) { var returned = JSON.parse(this.responseText); var results = returned.value; getContractInfo(results); } else { alert(this.statusText); } } }; req.send(); } function test(data, newInvoiceID) { var data = data; var ID = newInvoiceID; for (var i = 0; i < data.length; i ) { var existingProduct = data[i]; if(Date.parse(existingProduct.activeon) < Date.parse(currentStart)) { if(Date.parse(existingProduct.expireson) > Date.parse(currentEnd)) { var lineitem = {}; lineitem["productid@odata.bind"] = "/products(" existingProduct._productid_value ")"; lineitem["uomid@odata.bind"] = "/uoms(" existingProduct._uomid_value ")"; lineitem["new_contractactiveon"] = currentStart; lineitem["new_contractexpireson"] = currentEnd; lineitem["new_ortfirma@odata.bind"] = "/accounts(" existingProduct._new_ortfirma_value ")"; lineitem.quantity = existingProduct.initialquantity; lineitem["invoiceid@odata.bind"] = "/invoices(" ID ")"; lineitem["manualdiscountamount"] = existingProduct.discount; test3(lineitem, ID); } if(Date.parse(existingProduct.expireson) < Date.parse(currentEnd)) { var lineitem = {}; lineitem["productid@odata.bind"] = "/products(" existingProduct._productid_value ")"; lineitem["uomid@odata.bind"] = "/uoms(" existingProduct._uomid_value ")"; lineitem["new_contractactiveon"] = currentStart; lineitem["new_contractexpireson"] = existingProduct.expireson; lineitem["new_ortfirma@odata.bind"] = "/accounts(" existingProduct._new_ortfirma_value ")"; lineitem.quantity = existingProduct.initialquantity; lineitem["invoiceid@odata.bind"] = "/invoices(" ID ")"; test3(lineitem, ID); } } else if(Date.parse(existingProduct.activeon) > Date.parse(currentStart)) { if(Date.parse(existingProduct.expireson) > Date.parse(currentEnd)) { var lineitem = {}; lineitem["productid@odata.bind"] = "/products(" existingProduct._productid_value ")"; lineitem["uomid@odata.bind"] = "/uoms(" existingProduct._uomid_value ")"; lineitem["new_contractactiveon"] = existingProduct.activeon; lineitem["new_contractexpireson"] = currentEnd; lineitem["new_ortfirma@odata.bind"] = "/accounts(" existingProduct._new_ortfirma_value ")"; lineitem.quantity = existingProduct.initialquantity; lineitem["invoiceid@odata.bind"] = "/invoices(" ID ")"; lineitem["manualdiscountamount"] = existingProduct.discount; test3(lineitem, ID); } if(Date.parse(existingProduct.expireson) < Date.parse(currentEnd)) { var lineitem = {}; lineitem["productid@odata.bind"] = "/products(" existingProduct._productid_value ")"; lineitem["uomid@odata.bind"] = "/uoms(" existingProduct._uomid_value ")"; lineitem["new_contractactiveon"] = existingProduct.activeon; lineitem["new_contractexpireson"] = existingProduct.expireson; lineitem["new_ortfirma@odata.bind"] = "/accounts(" existingProduct._new_ortfirma_value ")"; lineitem.quantity = existingProduct.initialquantity; lineitem["invoiceid@odata.bind"] = "/invoices(" ID ")"; test3(lineitem, ID); } } } } function test3(data, invoiceID) { var req = new XMLHttpRequest(); req.open("POST", parent.Xrm.Page.context.getClientUrl() "/api/data/v8.2/invoicedetails", false); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.onreadystatechange = function() { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 204) { var uri = this.getResponseHeader("OData-EntityId"); var regExp = /\(([^)] )\)/; var matches = regExp.exec(uri); var newEntityId = matches[1]; } else { parent.Xrm.Utility.alertDialog(this.statusText); } } }; req.send(JSON.stringify(data)); setTimeout(50000, Xrm.Utility.openEntityForm("invoice", invoiceID).then( function (success) { console.log(success); }, function (error) { console.log(error); })); } //info from contract that is going to be used in the invoice function getContractInfo(data) { var ID = parent.Xrm.Page.data.entity.getId().substring(1, 37); var req = new XMLHttpRequest(); req.open("GET", parent.Xrm.Page.context.getClientUrl() "/api/data/v8.2/contracts(" ID ")?$select=activeon,_customerid_value,expireson,_serviceaddress_value,title&$expand=contract_line_items($select=activeon,expireson,initialquantity,price,title,new_thisstart, new_thisend)", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var result = JSON.parse(this.response); var activeon = result["new_thisstart"]; var _customerid_value = result["_customerid_value"]; var expireson = result["new_thisend"]; var title = result["title"]; createInvoice(_customerid_value, title, data, activeon, expireson, ID); } else { parent.Xrm.Utility.alertDialog(this.statusText); } } }; req.send(); } //create the invoice function createInvoice(customer, title, data, activeon, expireson, contractID) { var entity = {}; entity["customerid_account@odata.bind"] = "/accounts(" customer ")"; entity["pricelevelid@odata.bind"] = "/pricelevels(be2084c3-1e50-eb11-bc99-00155db20709)"; entity.name = title; entity["transactioncurrencyid@odata.bind"] = "/transactioncurrencies(59f3fa86-385a-ea11-bc6a-00155db20709)"; entity["new_VertragId@odata.bind"] = "/contracts(" contractID ")"; entity.new_contractactiveon = activeon; entity.new_contractexpireson = expireson; var req = new XMLHttpRequest(); req.open("POST", parent.Xrm.Page.context.getClientUrl() "/api/data/v8.2/invoices", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 204) { var uri = this.getResponseHeader("OData-EntityId"); var regExp = /\(([^)] )\)/; var matches = regExp.exec(uri); var newEntityId = matches[1]; var invoiceID = this.getResponseHeader("OData-EntityId").substring(67,103); test(data, invoiceID); } else { parent.Xrm.Utility.alertDialog(this.statusText); } } }; req.send(JSON.stringify(entity)); }
I believe this part is at fault here:
setTimeout(50000, Xrm.Utility.openEntityForm("invoice", invoiceID).then(
function (success) {
console.log(success);
},
function (error) {
console.log(error);
}));
I'd like to open my freshly created invoice record after all of its line items are created. As of now line item creation is incomplete and yet it jumps to the record
I would appreciate your feedback and solutions as I am not too well-versed in coding. Overall improvements to my code would be very welcome as well :)