-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop replacing consecutive spaces (#5)
Add example script for data transfer
- Loading branch information
Showing
5 changed files
with
138 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 29 additions & 15 deletions
44
force-app/main/default/classes/DynamicSOQLDMLController.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/*** | ||
* This script moves Account, Contact and Opportunity data from a source instance to the current SF instance. | ||
* The source instance is expected to be configured using a named credential named 'source' | ||
*/ | ||
let baseurl = "callout:source/services/data/v48.0/query?q="; | ||
this.httpRequest.setEndpoint(baseurl + "Select+Id,Name+from+Account"); | ||
this.httpRequest.addHeader("Accept", "application/json"); | ||
let resp = await this.httpRequest.send(); | ||
resp = JSON.parse(resp.body); | ||
|
||
if (!resp.done) { | ||
alert("Request to fetch Accounts failed"); | ||
} | ||
let accts = resp.records; | ||
this.httpRequest.setEndpoint( | ||
baseurl + "Select+Id,LastName,FirstName,Email,AccountId+from+Contact" | ||
); | ||
resp = await this.httpRequest.send(); | ||
resp = JSON.parse(resp.body); | ||
if (!resp.done) { | ||
alert("Request to fetch Contacts failed"); | ||
} | ||
let contacts = resp.records; | ||
|
||
this.httpRequest.setEndpoint( | ||
baseurl + | ||
"Select+Id,Name,CloseDate,StageName,AccountId,ContactId+from+Opportunity" | ||
); | ||
resp = await this.httpRequest.send(); | ||
resp = JSON.parse(resp.body); | ||
if (!resp.done) { | ||
alert("Request to fetch Opportunities failed"); | ||
} | ||
let opps = resp.records; | ||
|
||
let acctsToInsert = accts.map((a) => { | ||
let b = { ...a }; | ||
delete b.Id; | ||
delete b.attributes; | ||
return b; | ||
}); | ||
|
||
let newAcctIds=[]; | ||
try{ | ||
newAcctIds = || insert Account(acctsToInsert) ||; | ||
}catch(err){ | ||
alert(JSON.stringify(err)); | ||
} | ||
let acctIdMap = accts.reduce( | ||
(obj, a, i) => ({ ...obj, [a.Id]: newAcctIds[i] }), | ||
{} | ||
); | ||
|
||
let contactsToInsert = contacts | ||
.map((c) => { | ||
let d = { ...c, AccountId: acctIdMap[c.AccountId] }; | ||
delete d.Id; | ||
delete d.attributes; | ||
return d; | ||
}) | ||
.filter((c) => c.AccountId); | ||
|
||
let newCtctIds = []; | ||
try { | ||
newCtctIds = || insert Contact(contactsToInsert) ||; | ||
} catch (err) { | ||
alert(JSON.stringify(err)); | ||
} | ||
let ctctIdMap = contacts.reduce( | ||
(obj, c, i) => ({ ...obj, [c.Id]: newCtctIds[i] }), | ||
{} | ||
); | ||
|
||
let oppsToInsert = opps | ||
.map((o) => { | ||
let p = { | ||
...o, | ||
AccountId: acctIdMap[o.AccountId], | ||
ContactId: ctctIdMap[o.ContactId] | ||
}; | ||
delete p.Id; | ||
delete p.attributes; | ||
return p; | ||
}) | ||
.filter((o) => o.AccountId); | ||
|
||
let newOppIds = []; | ||
try { | ||
newOppIds = || insert Opportunity(oppsToInsert) ||; | ||
} catch (err) { | ||
alert(JSON.stringify(err)); | ||
} | ||
alert('Data transfer complete'); |