【问题标题】:Using filter with Customer screen in Acumatica API在 Acumatica API 中使用带有客户屏幕的过滤器
【发布时间】:2015-05-27 02:16:52
【问题描述】:

查看 Acumatica 的 API 示例,我编写了代码以基于单个过滤器从客户屏幕导出一些数据。过滤器应强制客户的电子邮件地址等于特定值(一旦起作用,我还将检查带有加密密码的自定义字段)。然而,出于某种原因,Export 函数返回的似乎是我们数据库中的每条客户记录。谁能告诉我我的过滤器做错了什么,或者其他什么?调试器的代码和屏幕截图如下。

谢谢!

Public Function ValidateUser(ByVal emailAddress As String, ByVal password As String, ByRef customerFirstName As String, ByRef customerLastName As String, ByRef customerCountry As String, ByRef customerPhone As String, ByRef customerCell As String) As String

    Dim customer As AR303000Content = m_context.AR303000GetSchema()
    m_context.AR303000Clear()

    Dim emailFilter As Filter = New Filter()
    emailFilter.Field = customer.GeneralInfoMainContact.Email
    emailFilter.Condition = FilterCondition.Equals
    emailFilter.Value = emailAddress

    Dim searchfilters() As Filter = {emailFilter}
    Dim searchCommands() As Command = {customer.CustomerSummary.CustomerID, customer.CustomerSummary.CustomerName, customer.GeneralInfoMainContact.Phone1, customer.GeneralInfoMainContact.Phone2, customer.GeneralInfoMainAddress.Country}
    Dim searchResult As String()() = m_context.AR303000Export(searchCommands, searchfilters, 0, False, False)

    Dim numRecords = searchResult.Length
    Dim customerID As String = ""
    Dim customerName As String = ""
    If numRecords > 0 Then
        ' we found a user with that email address
        Dim i As Integer = 0
        For i = 1 To numRecords
            customerID = searchResult(i - 1)(0)
            customerName = searchResult(i - 1)(1)
            customerPhone = searchResult(i - 1)(2)
            customerCell = searchResult(i - 1)(3)
            customerCountry = searchResult(i - 1)(4)
        Next
    End If

    Dim spaceInName = customerName.IndexOf(" ")
    If spaceInName >= 0 Then
        customerFirstName = customerName.Substring(0, spaceInName)
        customerLastName = customerName.Substring(spaceInName + 1)
    End If

    Return customerID
End Function

【问题讨论】:

  • 为了您的目的,您需要先使用通用查询,然后再应用过滤器

标签: acumatica


【解决方案1】:

过滤器通常仅适用于主视图(例如,主表 - 在本例中为 BAccount+Customer)。如果您尝试过滤辅助视图中包含的数据,系统将默默地忽略它。电子邮件地址和联系人记录数据包含在联系人表中,因此您无法按这些字段进行过滤。

我认为应该改进这种行为,如果您尝试过滤不允许的内容,我会在内部提出建议,至少抛出异常。它至少会使解决这个问题更容易。

作为替代方案,如果您使用 Submit() 函数加载特定记录,则可以更改架构中的基础 FieldName 以使其基于另一个字段匹配。下面的示例展示了如何通过基于电子邮件地址查找客户来更新客户信用验证设置:

Screen context = new Screen();
context.CookieContainer = new System.Net.CookieContainer();
context.Url = Url;
context.Login(Login, Password);

AR303000Content custSchema = context.AR303000GetSchema();

custSchema.CustomerSummary.CustomerID.FieldName += 
    ("!" + custSchema.CustomerSummary.ServiceCommands.FilterEmail.FieldName);

var commands = new Command[]
{
    new Value 
    {
        Value = "demo@gmail.com", 
        LinkedCommand = custSchema.CustomerSummary.CustomerID 
    },

    new Value 
    {
        Value = "Disabled", 
        LinkedCommand = custSchema.GeneralInfoCreditVerificationRulesCreditVerification.CreditVerification 
    },

    custSchema.Actions.Save,

    custSchema.GeneralInfoFinancialSettings.CustomerClass
};
var customer = context.AR303000Submit(commands)[0];

您还可以使用 Submit() 函数来检索数据。通常您将需要的字段放在 commands 数组中,但如果您特别需要 CustomerID,则需要使用技巧来检索它,因为 CustomerID.FieldName 值已更改以添加电子邮件过滤器。这是示例:

private static string GetCustomerIDByEmail(string eMailAddress)
{
    Screen context = new Screen();
    context.CookieContainer = new System.Net.CookieContainer();
    context.Login("admin", "admin");

    Content custSchema = context.GetSchema();

    custSchema.CustomerSummary.CustomerID.FieldName +=
        ("!" + custSchema.CustomerSummary.ServiceCommands.FilterEmail.FieldName);

    var commands = new Command[]
    {
        new Value 
        {
            Value = eMailAddress,
            LinkedCommand = custSchema.CustomerSummary.CustomerID
        },

        // Manually define the Field by setting ObjectName and FieldName. We can't use custSchema.CustomerSummary.CustomerID field because it was altered to make it search by e-mail.
        new Field
        {
            ObjectName = "BAccount",
            FieldName = "AcctCD"
        }
    };

    var results = context.Submit(commands);

    if (results.Length == 0)
        return "Not found";
    else
        return results[0].CustomerSummary.CustomerID.Value;
}

【讨论】:

  • 谢谢,但这对我不起作用。我不想在这里保存任何数据,我只是想获取具有该电子邮件地址的客户 ID。我认为如果我只是保存一些虚拟值,您的解决方案可能仍然有效,只要有某种方法可以从结果中获取客户 ID。但是当我运行它时,API 认为我正在尝试添加一个新客户并给出有关缺少必填字段的错误。如果我们构建了一个包含客户 ID 和电子邮件地址的自定义屏幕,那么我们是否能够像我尝试的那样通过电子邮件地址过滤它?
  • 您使用 GetCustomerIDByEmail 的第二个示例运行良好,谢谢!我同意如果过滤器在辅助视图上工作或至少给出某种错误会很好,所以也感谢您提出建议。
  • 现在我已经更多地使用 GetCustomerIDByEmail 解决方案,我注意到它似乎总是返回一个结果。如果未找到具有该地址的客户记录,则 results[0].CustomerSummary.CustomerID.Value 是电子邮件地址的前 6 个字符。所以我只是添加了一个条件 results[0].CustomerSummary.CustomerID.Value 应该是数字。从理论上讲,如果某人的电子邮件地址的前 6 个字符为数字,这将导致问题。但我也在做一些其他的验证,所以这对我的目的来说没问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
  • 2021-09-16
  • 2018-03-03
  • 1970-01-01
  • 2018-02-22
  • 1970-01-01
相关资源
最近更新 更多