当前位置: 动力学知识库 > 问答 > 编程问答 >

php - sales force save dependent objects in one call

问题描述:

I am using salesforce partner WSDL with soap api to upload data on salesforce and store it on SF object

I have two dependent objects who have one lookup field for child object

When I am storing parent object I want to store the dependent child object data.

How can I achieve this using SOAP api.

Thanks in advance..

网友答案:

You can do this in the SOAP by using an external ID field on the parent object type, this will let you send the parent & child objects in a single call, and related them automatically via the external Id field values.

here's a sample in Java that creates an account and a related contact in one go. the contact is related to the account via the extId__c field.

public static void main(String[] args) throws Exception {

    // login to salesforce.
    PartnerConnection pc = Connector.newConnection(args[0], args[1]);

    // The new Account record we're going to create.
    SObject acc = new SObject();
    acc.setType("Account");
    acc.setField("Name", "My New Account");
    acc.setField("extId__c", UUID.randomUUID().toString());

    // The new Contact record we're going to create.
    SObject con = new SObject();
    con.setType("Contact");
    con.setField("FirstName", "Simon");
    con.setField("LastName", "Fell");
    // This Account object we build with the relationship to the account above based
    // on the extId__c field, and then we set it on the contact record
    // this is the standard FK lookup using ExternalIds feature.
    SObject parentAcc = new SObject();
    parentAcc.setType("Account");
    parentAcc.setField("extId__c", acc.getField("extId__c"));
    con.setField("Account", parentAcc);


    // Now we can insert both records at once
    SaveResult [] sr = pc.create(new SObject [] { acc, con} );
    printSaveResult("Account result", sr[0]);
    printSaveResult("Contact result", sr[1]);
}

private static void printSaveResult(String label, SaveResult sr) {
    if (sr.isSuccess())
        System.out.println(label + " success recordId is " + sr.getId());
    else 
        System.out.println(label + " failed, reason is " + sr.getErrors()[0].getMessage());
}

When i run this, it prints

Account result success recordId is 0013000001DFMRxAAP
Contact result success recordId is 0033000001aEgskAAC

And when i log into the web app, i can see my new account record, and the child contact record in its related list.

网友答案:

You can't achieve this using the SOAP API, because with that API you'll have to create the master object first so you can get the IDs to set the child objects to. If you want to create all the objects in one transaction then you'll have to expose an Apex method as a SOAP web service and call that instead.

分享给朋友:
您可能感兴趣的文章:
随机阅读: