0

I'm trying to use SFDX to export users, add a MobilePone, then upsert them back in.

I used the following to get a .json file of user records:

sfdx force:data:tree:export -q "SELECT Id, Username, Email, Phone, MobilePhone FROM User" -d ./path/to/my/path -u <username_here>

This creates a User.json in my path. I then try to upsert by using the following:

sfdx force:data:bulk:upsert -s User -f ./path/to/my/path/User.json -i Id -w 2 -u <usernamehere>

I get the error:

Invalid Opening Quote: a quote is found inside a field at line 2

I think because the -f is expecting a .csv file, though I could be wrong.

Any better way of approaching this?

3
  • Is there a reason why you need to do this with sfdx and not with the REST API directly?
    – Gilhil
    Commented Feb 20, 2023 at 8:07
  • Easier to do when it's a 1-off and I can use my own computer as credentials rather than a bearer token Commented Feb 20, 2023 at 16:21
  • for a 1 off i would look into using the salesforce dataloader then where you get a csv that you can edit in excel or other tools and then upload again through the dataloader
    – Gilhil
    Commented Feb 21, 2023 at 8:08

1 Answer 1

1

sf or sfdx does not support upsert using json format yet. We can follow below approach [I have used sf instead of sfdx but both should work]:

1.Export data in CSV format using below command & bulk option :

sf data query -q "SELECT Id, Username, Email, Phone, MobilePhone FROM User" --resultformat csv --bulk --wait 0

Equivalent sfdx command :

sfdx force:data:soql:query -q "SELECT Id, Username, Email, Phone, MobilePhone FROM User" --resultformat csv --bulk --wait 0

2.Fetch the result using resume command, job id from 1st step & export to a file:

sf data query resume -I <job-id-from-first-step> --target-org <org-alias> > ./csvFile.csv 

3.Upsert data using sf command[There are some issues in CSV w.r.t. file format]:

sf data upsert bulk -s User -f csvFile.csv -i ID -w 2

Refer this link incase you get invalid field error for step 3

4
  • Thanks, is there any reason to preference csv format over JSON? Commented Feb 19, 2023 at 14:49
  • I think CSV is preferred because it consumes less bandwidth , easy to read & faster to process compared to JSON. Do consider marking it as the answer and upvoting if this helps.
    – Rohit
    Commented Feb 19, 2023 at 14:56
  • Thanks but this doesn't work as expected with sfdx. You didn't add an SFDX command that exports. Commented Feb 19, 2023 at 15:46
  • It's there in the answer, step 1 & 2 is about export... sf & sfdx are equivalent.
    – Rohit
    Commented Feb 19, 2023 at 16:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .