crm:创建唯一的发票

时间:2013-06-11 16:48:27

标签: c# dynamics-crm-2011

你能帮我解决crm 2011 我需要另一个系统的进口发票。 (很简单)

private void CreateInvoice(Invoice _invoice)
{
    IOrganizationService _service = GetCRMService();
    Entity entity = new Entity("invoice");

    entity[e_Invoice.InvoiceNumber] =  _invoice.Id.ToString();
    entity[e_Invoice.CustomerId] = new EntityReference("account", new Guid("6209A6AD-43B6-E211-A99D-005056A51C55"));

    _service.Create(entity);
}

我在 PreRetriveInvoice 上打电话。

所以在第二次通话时,方法会创建另一个项目(例如 INV-01157-F4C5F5

对我来说,这是不可接受的。

如何查看发票号码?如果我在系统中有这个号码,我会跳过记录。

1 个答案:

答案 0 :(得分:1)

您可以向OrganizationService创建查询,要求搜索具有给定编号的发票,如果结果不为空,则跳过创建发票。

您的代码可能如下所示:

private void CreateInvoice(Invoice _invoice)
{
    IOrganizationService _service = GetCRMService();

    // Getting all invoices with given number 
    var filter = new FilterExpression();
    filter.AddCondition(e_Invoice.InvoiceNumber, ConditionOperator.Equal, _invoice.Id.ToString());

    var query = new QueryExpression("invoice")
    {
        ColumnSet = new ColumnSet(true),
        Criteria = filter,
        Distinct = true
    };

    // Executing query
    var invoices = (EntityCollection)_service.RetrieveMultiple(query);

    if (invoices.Entities.Count == 0)
    {
        // Creating new invoice
        Entity entity = new Entity("invoice");

        entity[e_Invoice.InvoiceNumber] =  _invoice.Id.ToString();
        entity[e_Invoice.CustomerId] = new EntityReference("account", new Guid("6209A6AD-43B6-E211-A99D-005056A51C55"));

        _service.Create(entity);
    }
}