2

我正在尝试使用 PHP Toolkit 20.0 和 Enterprise SOAP API 将“机会”批量上传到 Salesforce。

我发现这样做的方法是创建一个 Opportunity 对象,然后通过 SOAP API 在 Salesforce 中创建它,然后在响应中,我为该IdOpportunity 存在的每个 1..n OpportunityLineItems 使用并使用它。

这不是很有效,因为它使用 2 个 SOAP API 调用,并且在批量完成时会使用大量资源并且容易超时。(我不想一次性减少发送的数量,因为 API 调用是有限的)

因此,有没有办法在单个 API 调用中同时创建 Opportunity 及其 OpportunityLineItems?

我尝试了以下方法:

$opp = new stdClass();
$opp->Name = 'Opp1';
$opp->StageName = 'Closed Won';
$opp->Account = new stdClass();
$opp->Account->Custom_ID__c = '1234';
$opp->Pricebook2Id = '...'; 
$opp->OpportunityLineItems = array();
$opp->OpportunityLineItems[0] = new stdClass();
$opp->OpportunityLineItems[0]->Description = 'Product name';
$opp->OpportunityLineItems[0]->Quantity = 1;
$opp->OpportunityLineItems[0]->UnitPrice = 10.00;
...
$opp->OpportunityLineItems[n] = new stdClass();
$opp->OpportunityLineItems[n]->Description = 'Product name';
$opp->OpportunityLineItems[n]->Quantity = 1;
$opp->OpportunityLineItems[n]->UnitPrice = 10.00;

但它导致:
INVALID_FIELD: No such column 'OpportunityLineItems' on entity 'Opportunity'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
这是意料之中的,因为 WSDL 文件指出 OpportunityLineItems 是类型tns:QueryResult而不是ens:

4

1 回答 1

4

编辑:完成大修以显示如何同时添加多个 opp。如果您可以以某种方式错开它们的创建(将它们存储在本地数据库中并仅在您有几个/足够的时间过去/用户按下“刷新队列”按钮时才上传)应该很有用。

警告,代码现在看起来更吓人了,你最好先在编辑历史中检查以前的版本。

创建一个 Apex 类来接受带有 2 个参数的传入请求并尝试插入它们并不难。转到 Setup->Develop->Classes->New 并尝试以下操作:

global with sharing class OpportunityLinkedInsert{

    static webservice Opportunity insertSingle(Opportunity opp, OpportunityLineItem[] lines){
        if(opp == null || lines == null){
            throw new IntegrationException('Invalid data');
        }
        Opportunity[] result = insertMultiple(new List<Opportunity>{opp}, new List<List<OpportunityLineItem>>{lines});
        return result[0]; // I imagine you want the Id back :) 
    }

    /*  I think SOAP doesn't handle method overloading well so this method has different name.
        'lines' are list of lists (jagged array if you like) so opps[i] will be inserted and then lines[i] will be linked to it etc.

        You can insert up to 10,000 rows in one go with this function (remember to count items in both arrays).
    */
    static webservice List<Opportunity> insertMultiple(List<Opportunity> opps, List<List<OpportunityLineItem>> lines){
        if(opps == null || lines == null || opps.size() == 0 || opps.size() != lines.size()){
            throw new IntegrationException('Invalid data');
        }
        insert opps;

        // I need to flatten the structure before I insert it.
        List<OpportunityLineItem> linesToInsert = new List<OpportunityLineItem>();

        for(Integer i = 0; i < opps.size(); ++i){
            List<OpportunityLineItem> linesForOne = lines[i];
            if(linesForOne != null && !linesForOne.isEmpty()){
                for(Integer j = 0; j < linesForOne.size(); ++j){
                    linesForOne[j].OpportunityId = opps[i].Id;
                }
                linesToInsert.addAll(linesForOne);
            }
        }
        insert linesToInsert;
        return opps;
    }

    // helper class to throw custom errors
    public class IntegrationException extends Exception{}
}

您还需要一个单元测试课程,然后才能进入您的生产组织。应该做这样的事情(需要在 100% 可用之前填充更多的东西,有关更多信息,请参阅此问题)。

@isTest
public class OpportunityLinkedInsertTest{
    private static List<Opportunity> opps;
    private static List<List<OpportunityLineItem>> items;

    @isTest
    public static void checSingleOppkErrorFlow(){
        try{
            OpportunityLinkedInsert.insertSingle(null, null);
            System.assert(false, 'It should have failed on null values');
        } catch(Exception e){
            System.assertEquals('Invalid data',e.getMessage());
        }
    }

    @isTest
    public static void checkMultiOppErrorFlow(){
        prepareTestData();
        opps.remove(1);

        try{
            OpportunityLinkedInsert.insertMultiple(opps, items);
            System.assert(false, 'It should have failed on list size mismatch');
        } catch(Exception e){
            System.assertEquals('Invalid data',e.getMessage());
        }
    }

    @isTest
    public static void checkSuccessFlow(){
        prepareTestData();
        List<Opportunity> insertResults = OpportunityLinkedInsert.insertMultiple(opps, items);

        List<Opportunity> check = [SELECT Id, Name, 
            (SELECT Id FROM OpportunityLineItems) 
            FROM Opportunity 
            WHERE Id IN :insertResults 
            ORDER BY Name];
        System.assertEquals(items[0].size(), check[0].OpportunityLineItems.size(), 'Opp 1 should have 1 product added to it');
        System.assertEquals(items[1].size(), check[0].OpportunityLineItems.size(), 'Opp 3 should have 1 products');
    }

    // Helper method we can reuse in several tests. Creates 2 Opportunities with different number of line items.
    private static void prepareTestData(){
        opps = new List<Opportunity>{
            new Opportunity(Name = 'Opp 1', StageName = 'Prospecting', CloseDate = System.today() + 10),
            new Opportunity(Name = 'Opp 2', StageName = 'Closed Won', CloseDate = System.today())
        };

        // You might have to fill in more fields here!
        // Products are quite painful to insert with all their standard/custom pricebook dependencies etc...
        items = new List<List<OpportunityLineItem>>{
            new List<OpportunityLineItem>{
                new OpportunityLineItem(Description = 'Opp 1, Product 1', Quantity = 1, UnitPrice = 10)
            },
            new List<OpportunityLineItem>{
                new OpportunityLineItem(Description = 'Opp 2, Product 1', Quantity = 1, UnitPrice = 10),
                new OpportunityLineItem(Description = 'Opp 2, Product 2', Quantity = 1, UnitPrice = 10),
                new OpportunityLineItem(Description = 'Opp 2, Product 3', Quantity = 1, UnitPrice = 10)
            }
        };
    }
}

这与 Apex 代码差不多。

如果任一插入失败,您将返回 SOAP 异常。这在事务、 ACID等方面也更好一些- 如果插入您的行项目将失败,您准备好从 PHP 端清理它吗?如果在 Salesforce 中设置了一些自动电子邮件通知等并且已经发送了怎么办?一次调用 Apex 将确保回滚整个请求,就像在数据库中工作的存储过程一样。

尝试在沙盒中创建这些类,然后在类列表中找到第一个。它将有一个生成 WSDL 文件的链接,您可以使用该文件生成 PHP 类。转到第二个,您将看到一个“运行测试”按钮。在将测试推送到您的生产组织之前,您必须确保测试通过 - 但这对您来说是平台上编程的全新世界:)

于 2012-11-07T15:50:02.117 回答