web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

create Invoice from order using javascript

(0) ShareShare
ReportReport
Posted on by 100

Hi anyone please help me .

i past my javascript here.

function ORDERtoINVOICE() {

Xrm.Page.data.entity.save();

var Order_Name = Xrm.Page.data.entity.attributes.get("name").getValue();
//to get lookup value
var Order_Currency = new Array();
Order_Currency = Xrm.Page.data.entity.attributes.get("transactioncurrencyid").getValue();
var name1 = Order_Currency[0].name;
var guid1 = Order_Currency[0].id;
var enttype1 = Order_Currency[0].entityType;

//to get lookup value
var Order_Pricelist = new Array();
Order_Pricelist = Xrm.Page.data.entity.attributes.get("pricelevelid").getValue();
var name2 = Order_Pricelist[0].name;
var guid2 = Order_Pricelist[0].id;
var enttype2 = Order_Pricelist[0].entityType;

//to get lookup value
var Order_Customer = new Array();
Order_Customer = Xrm.Page.data.entity.attributes.get("customerid").getValue();
var name3 = Order_Customer[0].name;
var guid3 = Order_Customer[0].id;
var enttype3 = Order_Customer[0].entityType;
//Xrm.Utility.openEntityForm("invoice", "C9F7C8E8-324F-4AE4-9927-14EFCBA20D08");

var parameters = {};
//passing order_name as parameter
parameters["name"] = Order_Name;
//parameters["transactioncurrencyid"] = name1;

// parameters["pricelevelid"] = guid2;
// parameters["pricelevelidname"] = name2 ;
// parameters["pricelevelidtype"] = enttype2 ;

//passing order as aparameter
parameters["customerid"] = guid3;
parameters["customeridname"] = name3 ;
parameters["customeridtype"] = enttype3 ;

Xrm.Utility.openEntityForm("invoice", null, parameters); 


alert("Invoice is created");
Xrm.Page.data.entity.save();
alert("done");
}

this is my code.

till now its working fine.

but when the form is save its generate invoice id.

please help me ?

please correct my code ...

thank you

*This post is locked for comments

I have the same question (0)
  • santhosh crm Profile Picture
    100 on at

    it doesn't  create new invoice.

  • Suggested answer
    PS Profile Picture
    23,577 on at

    Hi Santhosh,

    Why do you need a javascript to create invoice from order? you can achieve it by real time workflow.

    Try real time workflow, it would be much easier

  • Suggested answer
    Royal King Profile Picture
    27,686 on at

    Try with below script it should work


    function ConvertSalesOrderToInvoiceRequest(orderId) {
    var requestMain = ""
    requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
    requestMain += " <s:Body>";
    requestMain += " <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    requestMain += " <request i:type=\"b:ConvertSalesOrderToInvoiceRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
    requestMain += " <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
    requestMain += " <a:KeyValuePairOfstringanyType>";
    requestMain += " <c:key>SalesOrderId</c:key>";
    requestMain += " <c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">"+orderId+"</c:value>";
    requestMain += " </a:KeyValuePairOfstringanyType>";
    requestMain += " <a:KeyValuePairOfstringanyType>";
    requestMain += " <c:key>ColumnSet</c:key>";
    requestMain += " <c:value i:type=\"a:ColumnSet\">";
    requestMain += " <a:AllColumns>false</a:AllColumns>";
    requestMain += " <a:Columns xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">";
    requestMain += " <d:string>invoiceid</d:string>";
    requestMain += " <d:string>totalamount</d:string>";
    requestMain += " </a:Columns>";
    requestMain += " </c:value>";
    requestMain += " </a:KeyValuePairOfstringanyType>";
    requestMain += " </a:Parameters>";
    requestMain += " <a:RequestId i:nil=\"true\" />";
    requestMain += " <a:RequestName>ConvertSalesOrderToInvoice</a:RequestName>";
    requestMain += " </request>";
    requestMain += " </Execute>";
    requestMain += " </s:Body>";
    requestMain += "</s:Envelope>";
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl(), true)
    // Responses will return XML. It isn't possible to return JSON.
    req.setRequestHeader("Accept", "application/xml, text/xml, */*");
    req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
    var successCallback = null;
    var errorCallback = null;
    req.onreadystatechange = function () { ConvertSalesOrderToInvoiceResponse(req, successCallback, errorCallback); };
    req.send(requestMain);
    }
    function ConvertSalesOrderToInvoiceResponse(req, successCallback, errorCallback) {
    if (req.readyState == 4) {
    if (req.status == 200) {
    if (successCallback != null)
    { successCallback(); }
    }
    else {
    errorCallback(_getError(req.responseXML));
    }
    }
    }
    function _getError(faultXml) {
    var errorMessage = "Unknown Error (Unable to parse the fault)";
    if (typeof faultXml == "object") {
    try {
    var bodyNode = faultXml.firstChild.firstChild;
    //Retrieve the fault node
    for (var i = 0; i < bodyNode.childNodes.length; i++) {
    var node = bodyNode.childNodes[i];
    //NOTE: This comparison does not handle the case where the XML namespace changes
    if ("s:Fault" == node.nodeName) {
    for (var j = 0; j < node.childNodes.length; j++) {
    var faultStringNode = node.childNodes[j];
    if ("faultstring" == faultStringNode.nodeName) {
    errorMessage = faultStringNode.text;
    break;
    }
    }
    break;
    }
    }
    }
    catch (e) { };
    }
    return new Error(errorMessage);
    }

  • Suggested answer
    Mithilesh Kumar Profile Picture
    10,047 on at

    Hi Santhosh,

    There is OOB Create Invoice button on Order Form.

    Thanks

  • santhosh crm Profile Picture
    100 on at

    Thanks ..

  • santhosh crm Profile Picture
    100 on at

    Thank you chitra ...

  • santhosh crm Profile Picture
    100 on at

    Hi  ..

    I know oob button ..

    Right now am learning ..

    That's am trying to  create  custom button ..

    Thank you Kumar ...

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans