Question

Creating a new processing button that sends emails

  • 1 February 2024
  • 7 replies
  • 91 views

Userlevel 4
Badge

Hi,

I am creating a new processing screen that will be used to send invoice reminders to customers, but I am not sure how to set up the send email button to do this correctly.

Below is an example of how I was trying to set it up. This code doesn’t publish because of some errors, so I know it’s completely wrong, but I am not sure how I can correct it.

public PXAction<DunningLettersDAC> sendEmail;

[PXUIField(DisplayName = "Send Email", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]

protected IEnumerable SendEmail(PXAdapter adapter)
{
// Loop through all selected rows to process
foreach (DunningLettersDAC currentRecord in Base.ProcessingScreen.Cache.Updated)
{
var emailSettings = new SendMailSettings
{
Subject = "Your Email Subject",
Body = "Your Email Body",
AppendFiles = true
};

sendEmail.PressButton(adapter, emailSettings);
}

return adapter.Get();
}

Here is a list of what i am expecting this button to do, so let me know if there are some things on this list that on this list I won’t be able to achieve:

  1. Send email button should pull information from an existing email template
  2. To determine the email to send an email to, it should grab the Customer ID from the row currently being processed and pull the email from the customer card
  3. If possible, it should attach the relevant Customer Statement to the email, which I guess would be handled via the Email Template if it can be grabbed via the code for this new button.

What is the best way to trigger an email from from a custom button in a processing screen?

Let me know if you need any other information about what I am trying to do.

 

Kind regards,

Andrew


7 replies

Hi @AndrewA,

Could you please try with below code
 

public PXAction<DunningLettersDAC> sendEmail;

[PXButton(CommitChanges = true, Category = CommonActionCategories.DisplayNames.PrintingAndEmailing, DisplayOnMainToolbar = false)]
[PXUIField(DisplayName = "Send Email", MapEnableRights = PXCacheRights.Select)]
protected IEnumerable SendEmail(
PXAdapter adapter,
[PXString]
string notificationCD = null) => Notification(adapter, notificationCD ?? "EMAIL");

[PXUIField(DisplayName = "Notifications", Visible = false)]
[PXButton(ImageKey = PX.Web.UI.Sprite.Main.DataEntryF)]

protected virtual IEnumerable Notification(PXAdapter adapter, [PXString] string notificationCD)
{
var records = adapter.Get<YourDAC>().ToArray();

PXLongOperation.StartOperation(this, () =>
{
var recEntry = Lazy.By(() => PXGraph.CreateInstance<YourGraph>());

foreach (YourDAC dac in records)
{
try
{
recEntry.Value.Clear();

using (var ts = new PXTransactionScope())
{
var parameters = new Dictionary<string, string>
{
[nameof(DAC) + "." + nameof(Field)] = value,
[nameof(DAC) + "." + nameof(Field)] = value,
};

recEntry.Value.Document.Current = dac;
recEntry.Value.Activity.SendNotification(ARNotificationSource.Customer, notificationCD, order.BranchID, parameters);
recEntry.Value.Document.Update(dac); // to apply field assignments made to an record

recEntry.Value.Save.Press();
ts.Complete();
}
}
catch (Exception exception)
{
PXProcessing<DAC>.SetError(exception);
}
}
});
PXLongOperation.WaitCompletion(Base.UID);

return records;
}

Regards,
Sagar Gadge

Userlevel 4
Badge

Hi @KumarDighe ,

Thanks for your sending through that code. I filled in the places I believe I needed to change, and I have received some errors below when trying to publish these changes. I have also included the code I updated below the errors so you can see if I have updated something incorrectly that is causing an issue.

Do you know how to resolve some of these issues?

Also, in your code, I can see you are using a PXTransactionScope. Could you please explain what bit of code is doing as I was not sure how I needed to set it up.

Let me know if you need any other information.

Kind regards,

Andrew

 

\App_Code\Caches\DunningLettersGraph.cs(44): error CS1061: 'IEnumerable<DunningLettersDAC>' does not contain a definition for 'ToArray' and no accessible extension method 'ToArray' accepting a first argument of type 'IEnumerable<DunningLettersDAC>' could be found (are you missing a using directive or an assembly reference?)

\App_Code\Caches\DunningLettersGraph.cs(48): error CS0305: Using the generic type 'Lazy<T>' requires 1 type arguments

\App_Code\Caches\DunningLettersGraph.cs(50): error CS1579: foreach statement cannot operate on variables of type '?' because '?' does not contain a public instance definition for 'GetEnumerator'

\App_Code\Caches\DunningLettersGraph.cs(58): error CS0246: The type or namespace name 'Dictionary<,>' could not be found (are you missing a using directive or an assembly reference?)

\App_Code\Caches\DunningLettersGraph.cs(60): error CS0103: The name 'value' does not exist in the current context

\App_Code\Caches\DunningLettersGraph.cs(61): error CS0103: The name 'value' does not exist in the current context

\App_Code\Caches\DunningLettersGraph.cs(65): error CS0103: The name 'order' does not exist in the current context

\App_Code\Caches\DunningLettersGraph.cs(78): error CS0103: The name 'Base' does not exist in the current context

\App_Code\Caches\DunningLettersGraph.cs(44): error CS1061: 'IEnumerable<DunningLettersDAC>' does not contain a definition for 'ToArray' and no accessible extension method 'ToArray' accepting a first argument of type 'IEnumerable<DunningLettersDAC>' could be found (are you missing a using directive or an assembly reference?)

 

[PXButton(CommitChanges = true, DisplayOnMainToolbar = false)]
[PXUIField(DisplayName = "Send Email", MapEnableRights = PXCacheRights.Select)]
protected IEnumerable SendEmail(
PXAdapter adapter,
[PXString]
string notificationCD = null) => Notification(adapter, notificationCD ?? "EMAIL");

[PXUIField(DisplayName = "Notifications", Visible = false)]
[PXButton(ImageKey = PX.Web.UI.Sprite.Main.DataEntryF)]

protected virtual IEnumerable Notification(PXAdapter adapter, [PXString] string notificationCD)
{
var records = adapter.Get<DunningLettersDAC>().ToArray();

PXLongOperation.StartOperation(this, () =>
{
var recEntry = Lazy.By(() => PXGraph.CreateInstance<DunningLettersGraph>());

foreach (DunningLettersDAC dac in records)
{
try
{
recEntry.Value.Clear();

using (var ts = new PXTransactionScope())
{
var parameters = new Dictionary<string, string>
{
[nameof(dac) + "." + nameof(dac.CustomerID)] = value,
[nameof(dac) + "." + nameof(dac.CustomerID_description)] = value,
};

recEntry.Value.Document.Current = dac;
recEntry.Value.Activity.SendNotification(ARNotificationSource.Customer, notificationCD, order.BranchID, parameters);
recEntry.Value.Document.Update(dac); // to apply field assignments made to an record

recEntry.Value.Save.Press();
ts.Complete();
}
}
catch (Exception exception)
{
PXProcessing<DunningLettersDAC>.SetError(exception);
}
}
});
PXLongOperation.WaitCompletion(Base.UID);

return records;
}

 

Userlevel 4
Badge +1

@AndrewA Acumatica has a dedicated processing page for sending out Dunning Letter notifications, is there a problem with that? Why would you develop a new, separate page doing pretty much the same thing?

 

 

Userlevel 4
Badge

Hi @Samvel Petrosov ,

 

The client is currently on a licensing level that doesn’t provide this feature, and they are not wanting to upgrade licenses just for a single feature. Therefore, we are being tasked with recreating this for them so they can use this process in their current system.

 

Kind regards,

Andrew

 

Userlevel 4
Badge +1

Hi @KumarDighe ,

Thanks for your sending through that code. I filled in the places I believe I needed to change, and I have received some errors below when trying to publish these changes. I have also included the code I updated below the errors so you can see if I have updated something incorrectly that is causing an issue.

Do you know how to resolve some of these issues?

Also, in your code, I can see you are using a PXTransactionScope. Could you please explain what bit of code is doing as I was not sure how I needed to set it up.

Let me know if you need any other information.

Kind regards,

Andrew

 

\App_Code\Caches\DunningLettersGraph.cs(44): error CS1061: 'IEnumerable<DunningLettersDAC>' does not contain a definition for 'ToArray' and no accessible extension method 'ToArray' accepting a first argument of type 'IEnumerable<DunningLettersDAC>' could be found (are you missing a using directive or an assembly reference?)

\App_Code\Caches\DunningLettersGraph.cs(48): error CS0305: Using the generic type 'Lazy<T>' requires 1 type arguments

\App_Code\Caches\DunningLettersGraph.cs(50): error CS1579: foreach statement cannot operate on variables of type '?' because '?' does not contain a public instance definition for 'GetEnumerator'

\App_Code\Caches\DunningLettersGraph.cs(58): error CS0246: The type or namespace name 'Dictionary<,>' could not be found (are you missing a using directive or an assembly reference?)

\App_Code\Caches\DunningLettersGraph.cs(60): error CS0103: The name 'value' does not exist in the current context

\App_Code\Caches\DunningLettersGraph.cs(61): error CS0103: The name 'value' does not exist in the current context

\App_Code\Caches\DunningLettersGraph.cs(65): error CS0103: The name 'order' does not exist in the current context

\App_Code\Caches\DunningLettersGraph.cs(78): error CS0103: The name 'Base' does not exist in the current context

\App_Code\Caches\DunningLettersGraph.cs(44): error CS1061: 'IEnumerable<DunningLettersDAC>' does not contain a definition for 'ToArray' and no accessible extension method 'ToArray' accepting a first argument of type 'IEnumerable<DunningLettersDAC>' could be found (are you missing a using directive or an assembly reference?)

 

[PXButton(CommitChanges = true, DisplayOnMainToolbar = false)]
[PXUIField(DisplayName = "Send Email", MapEnableRights = PXCacheRights.Select)]
protected IEnumerable SendEmail(
PXAdapter adapter,
[PXString]
string notificationCD = null) => Notification(adapter, notificationCD ?? "EMAIL");

[PXUIField(DisplayName = "Notifications", Visible = false)]
[PXButton(ImageKey = PX.Web.UI.Sprite.Main.DataEntryF)]

protected virtual IEnumerable Notification(PXAdapter adapter, [PXString] string notificationCD)
{
var records = adapter.Get<DunningLettersDAC>().ToArray();

PXLongOperation.StartOperation(this, () =>
{
var recEntry = Lazy.By(() => PXGraph.CreateInstance<DunningLettersGraph>());

foreach (DunningLettersDAC dac in records)
{
try
{
recEntry.Value.Clear();

using (var ts = new PXTransactionScope())
{
var parameters = new Dictionary<string, string>
{
[nameof(dac) + "." + nameof(dac.CustomerID)] = value,
[nameof(dac) + "." + nameof(dac.CustomerID_description)] = value,
};

recEntry.Value.Document.Current = dac;
recEntry.Value.Activity.SendNotification(ARNotificationSource.Customer, notificationCD, order.BranchID, parameters);
recEntry.Value.Document.Update(dac); // to apply field assignments made to an record

recEntry.Value.Save.Press();
ts.Complete();
}
}
catch (Exception exception)
{
PXProcessing<DunningLettersDAC>.SetError(exception);
}
}
});
PXLongOperation.WaitCompletion(Base.UID);

return records;
}

 

Based on the error messages, you are missing a number of Using statements. Can you send the .CS file?

Userlevel 4
Badge

Hi @Samvel Petrosov ,

I am using the Customisation screen in the system so I don’t have a proper file. Here is the entire code from that screen.

Let me know if you need anything else.

Kind regards,
Andrew
 

using System;
using System.Collections;
using PX.Data;
using PX.Objects;
using PX.Objects.AR;
using PX.Web.UI;

namespace DunningLettersScreen
{
public class DunningLettersGraph : PXGraph<DunningLettersGraph>
{

public PXProcessingJoin<ARInvoice,
InnerJoin<Customer, On<ARInvoice.customerID, Equal<Customer.bAccountID>>>,
Where<ARInvoice.customerID, IsNotNull>,
OrderBy<Asc<ARInvoice.customerID>>>
Invoices;

public PXSave<MasterTable> Save;
public PXCancel<MasterTable> Cancel;

public PXProcessing<DunningLettersDAC, Where<DunningLettersDAC.selected, Equal<True>>> Items;


public PXFilter<MasterTable> MasterView;
public PXFilter<DunningLettersDAC> DetailsView;

public PXSelect<DunningLettersDAC> DunningLettersRecords;

[PXButton(CommitChanges = true, DisplayOnMainToolbar = false)]
[PXUIField(DisplayName = "Send Email", MapEnableRights = PXCacheRights.Select)]
protected IEnumerable SendEmail(
PXAdapter adapter,
[PXString]
string notificationCD = null) => Notification(adapter, notificationCD ?? "EMAIL");

[PXUIField(DisplayName = "Notifications", Visible = false)]
[PXButton(ImageKey = PX.Web.UI.Sprite.Main.DataEntryF)]

protected virtual IEnumerable Notification(PXAdapter adapter, [PXString] string notificationCD)
{
var records = adapter.Get<DunningLettersDAC>().ToArray();

PXLongOperation.StartOperation(this, () =>
{
var recEntry = Lazy.By(() => PXGraph.CreateInstance<DunningLettersGraph>());

foreach (DunningLettersDAC dac in records)
{
try
{
recEntry.Value.Clear();

using (var ts = new PXTransactionScope())
{
var parameters = new Dictionary<string, string>
{
[nameof(dac) + "." + nameof(dac.CustomerID)] = value,
[nameof(dac) + "." + nameof(dac.CustomerID_description)] = value,
};

recEntry.Value.Document.Current = dac;
recEntry.Value.Activity.SendNotification(ARNotificationSource.Customer, notificationCD, order.BranchID, parameters);
recEntry.Value.Document.Update(dac); // to apply field assignments made to a record

recEntry.Value.Save.Press();
ts.Complete();
}
}
catch (Exception exception)
{
PXProcessing<DunningLettersDAC>.SetError(exception);
}
}
});
PXLongOperation.WaitCompletion(Base.UID);

return records;
}


[Serializable]
public class MasterTable : IBqlTable
{

}

[Serializable]
public class DetailsTable: IBqlTable
{

}

}
}

 

Userlevel 7
Badge

Hi @AndrewA were you able to find a solution? Thank you!

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved