Newer Version Available
OwnerChangeOptions
Represents actions that can be performed when a record’s owner is changed. Available with these options in API version 35.0 and later.
Fields
OwnerChangeOption Fields
Usage
When changing the owners of multiple accounts, all accounts must have the same old owner and the same new owner. To change ownership of accounts with different owners, use separate API requests.
Sample Code—Java
This sample creates an account, a note, an opportunity, and task for the account. It sets the owner change options so that the note, opportunity, and task are transferred to the new owner along with the account.
1public void ownerChangeOptionsHeaderSample() {
2
3 // Create account. Accounts don't transfer activities, notes, or attachments by default
4
5
6 Account account = new Account();
7 account.setName("Account");
8 com.sforce.soap.enterprise.SaveResult[] sr = connection.create(new com.sforce.soap.enterprise.sobject.SObject[] { account } );
9 String accountId = null;
10
11 if(sr[0].isSuccess()) {
12 System.out.println("Successfully saved the account");
13 accountId = sr[0].getId();
14
15 // Create a note, a task, and an opportunity for the account
16
17 Note note = new Note();
18 note.setTitle("Note Title");
19 note.setBody("Note Body");
20 note.setParentId(accountId);
21
22 Task task = new Task();
23 task.setWhatId(accountId);
24
25 Opportunity opportunity = new Opportunity();
26 opportunity.setName("Opportunity");
27 opportunity.setStageName("Prospecting");
28 Calendar dt = connection.getServerTimestamp().getTimestamp();
29 dt.add(Calendar.DAY_OF_MONTH, 7);
30 opportunity.setCloseDate(dt);
31 opportunity.setAccountId(accountId);
32
33 sr = connection.create(new com.sforce.soap.enterprise.sobject.SObject[] { note, task, opportunity } );
34
35 if(sr[0].isSuccess()) {
36 System.out.println("Successfully saved the note, task, and opportunity");
37
38
39 com.sforce.soap.enterprise.QueryResult qr = connection.query("SELECT Id FROM User WHERE FirstName = 'Jane' AND LastName = 'Doe'");
40 String newOwnerId = qr.getRecords()[0].getId();
41 account.setId(accountId);
42 account.setOwnerId(newOwnerId);
43
44 // Set owner change options so account's child note, task, and opportunity transfer to new owner
45 OwnerChangeOption opt1 = new OwnerChangeOption();
46 opt1.setExecute(true);
47 opt1.setType(OwnerChangeOptionType.TransferOwnedOpenOpportunities); // Transfer Open opportunities owned by the account's owner
48
49 OwnerChangeOption opt2 = new OwnerChangeOption();
50 opt2.setExecute(true);
51 opt2.setType(OwnerChangeOptionType.TransferOpenActivities);
52
53 OwnerChangeOption opt3 = new OwnerChangeOption();
54 opt3.setExecute(true);
55 opt3.setType(OwnerChangeOptionType.TransferNotesAndAttachments);
56
57 connection.setOwnerChangeOptions(new OwnerChangeOption[] {opt1, opt2, opt3});
58 connection.update(new com.sforce.soap.enterprise.sobject.SObject[] { account } );
59
60 // The account's note, task, and opportunity should be transferred to the new owner.
61 }
62
63 } else {
64 System.out.println("Account save failed: " + sr[0].getErrors().toString());
65 }
66}