0

Here I have created my project on the standard .NET library to GET/POST invoices. But as I want to email the invoice to which it's being created on that name. Here is my sample code below to create invoice.

        public async Task<ActionResult> Create(string Name, string LineDescription, string LineQuantity, string LineUnitAmount, string LineAccountCode)
        {
            var xeroToken = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
            var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();

            XeroConfiguration XeroConfig = new XeroConfiguration
            {
                ClientId = ConfigurationManager.AppSettings["XeroClientId"],
                ClientSecret = ConfigurationManager.AppSettings["XeroClientSecret"],
                CallbackUri = new Uri(ConfigurationManager.AppSettings["XeroCallbackUri"]),
                Scope = ConfigurationManager.AppSettings["XeroScope"],
                State = ConfigurationManager.AppSettings["XeroState"]
            };

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig, httpClientFactory);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);
                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();
            //string xeroTenantId = xeroToken.Tenants[1].TenantId.ToString();

            var contact = new Contact();
            contact.Name = Name;

            var line = new LineItem()
            {
                Description = LineDescription,
                Quantity = decimal.Parse(LineQuantity),
                UnitAmount = decimal.Parse(LineUnitAmount),
                AccountCode = LineAccountCode
            };

            var lines = new List<LineItem>() { line };
            //var lines = new List<LineItem>();
            //for (int j = 0;j < 5;j++)
            //{
            //    lines.Add(line);
            //}
            
            var invoice = new Invoice()
            {
                Type = Invoice.TypeEnum.ACCREC,
                Contact = contact,
                Date = DateTime.Today,
                DueDate = DateTime.Today.AddDays(30),
                LineItems = lines
            };

            var invoiceList = new List<Invoice>();
            invoiceList.Add(invoice);

            var invoices = new Invoices();
            invoices._Invoices = invoiceList;

            var AccountingApi = new AccountingApi();
            var response = await AccountingApi.CreateInvoicesAsync(accessToken, xeroTenantId, invoices);
            RequestEmpty _request = new RequestEmpty();
            //trying this method to send email to specified invoice....
            //var test = await AccountingApi.EmailInvoiceAsync(accessToken, xeroTenantId, Guid.NewGuid(), null);
            var updatedUTC = response._Invoices[0].UpdatedDateUTC;

            return RedirectToAction("Index", "InvoiceSync");
        }

Now as I learned that Xero allows sending email to that specified invoice, here is a link which I learned.
https://developer.xero.com/documentation/api/invoices#email

But as try to find method in the .NET standard library for Xero I stumble upon this method.

var test = await AccountingApi.EmailInvoiceAsync(accessToken, xeroTenantId, Guid.NewGuid(), null);    

How can I use this method to send email to a specified invoice ..?
It throws me an error regarding Cannot assign void to an implicitly-typed variable.

There is another method also in this library.

var test2 = await AccountingApi.EmailInvoiceAsyncWithHttpInfo(accessToken, xeroTenantId, Guid.NewGuid(), null);

As Guid.NewGuid() i have used is for only testing will add created GUID when I understand how these two methods operate.

Update 1:
Here is the method second method i used.

await AccountingApi.EmailInvoiceAsyncWithHttpInfo(accessToken, xeroTenantId, Guid.NewGuid(), null)

enter image description here

Update 2:
Here is the code i used.

        public async Task EmailInvoiceTest(string accessToken,string xeroTenantId,Guid invoiceID, RequestEmpty requestEmpty)
        {
            var AccountingApi = new AccountingApi();
            await AccountingApi.EmailInvoiceAsync(accessToken, xeroTenantId, invoiceID, requestEmpty).ConfigureAwait(false);
        }

enter image description here

  • I assume that both methods have return value of type void. Did you try to remove `var test = `/`var test2 = `? – rufer7 Aug 19 '20 at 11:28
  • @rufer7 as i have updated above its asking for requestEmpty .. ? –  Aug 19 '20 at 11:36

1 Answers1

0

The return type of method EmailInvoiceAsync seems to be Task with return type void. If you await the task, there is no return type which could be assigned to a variable. Remove the variable assignment and pass a valid argument for parameter of type RequestEmpty to solve the problem.

RequestEmpty requestEmpty = new RequestEmpty();
await AccountingApi.EmailInvoiceAsync(accessToken, xeroTenantId, Guid.NewGuid(), requestEmpty);

For an example test see here

IMPORTANT: According to the documentation (see section Emailing an invoice) the invoice must be of Type ACCREC and must have a valid status for sending (SUMBITTED, AUTHORISED or PAID).

rufer7
  • 3,369
  • 3
  • 22
  • 28