From Mrityunjoy Chowdhury: Adding Multiple Shift records using a CSV file
In the Spring ‘22 release, Scheduler introduced Shift Rostering Management which can be used to setup working hours for resources.
Instead of adding shift records one at a time, we can add multiple shifts in one go! Salesforce Scheduler allows adding multiple shifts. In this document we will demonsrate how to upload shifts using Salesforce bulk API capability.
Here I’ve even provided a template to help you create the final CSV which you need to upload!
Quick Help Video on process
Video how to create BulkShift csv data
Step 1.a: Creating Shifts CSV file directly
If you plan to share this with business users, try the format in 1.b which allows you to create the Id & record mapping and the business user only needs to select the record names.
- Create spreadSheet/Excel file
- Copy Paste the first row of the below table
- StartTime/EndTime Should be in GMT format (Local Time and GMT time difference should be adjusted)
- Status should be Published,Confirmed,Tentative
- TimeSlotType should be Normal
- File -> Download/Export-> download as CSV file
- Open Convert CSV to JSON and upload the file
- Copy the CSV data and use it as CSV shift data in Upload Shift data step.
StartTime | EndTime | Status | ServiceTerritoryId | ServiceResourceId | OwnerId | TimeSlotType |
2022-03-30T04:30:00.000Z | 2022-03-30T16:30:00.000Z | Confirmed | 0HhS70000004EkMKAU | 0HnS700000007xwKAA | 005S7000000VndRIAS | Normal |
2022-03-29T04:30:00.000Z | 2022-03-29T16:30:00.000Z | Confirmed | 0HhS70000004EkMKAU | 0HnS700000007xwKAA | 005S7000000VndRIAS | Normal |
2022-03-26T04:30:00.000Z | 2022-03-29T16:30:00.000Z | Confirmed | 0HhS70000004EkMKAU | 0HnS700000007xwKAA | 005S7000000VndRIAS | Normal |
Step 1.b: Create bulk Shift CSV from customized spreadsheet (Optional step).
- Download this File: BulkShifts
- Navigate to the “ShiftsMetadata” and add all the metadata of the Shifts. (Step done by Admin)
- Service Resource Name,Service Resource Id
- Service Territory Name,Service Territory Id,
- Owner Name,Owner Id,
- WorkTypeGroup Name,WorkTypeGroup Id,
- GMT Time difference. (GMT time difference is the time difference from your local time to GMT Time).
- Navigate to next sheet “DraftShifts” Create your required shifts based on the data you have uploaded in ShiftMetadata sheet,(Note No need to add any data in “GMT Start Time” and “GMT End Time”, it will populate the data automatically on dragging the column. Calculation is already done.Remember that don’t delete the first column because it has all the formulas, just update the first row as per you need.
- Navigate to FinalShiftData and you will see the status is already populated.
Drag the rest of the columns to populate all the data from DraftShits to Final Shifts. If there is #NA value present for any cell , remove those values to make value empty.(Note: Do not delete the first row as it has all the formulas. It will populate the value and then drag the column)
- Export the FinalShiftData sheet as CSV file
File -> Download -> Comma Separated Value.
- Open Convert CSV to JSON and upload the file
- Copy the CSV data and use it as CSV shift data in Upload Shift data step.
Step 2: Create a Bulk Job
Bulk API is rest based so you can use the salesforce workbench to make bulk API requests.
A. Open Workbench
B. Select Environment and API version (Note: shift for Scheduler is introduced after 54.0)
C. login to workbench
D. In the top menu, select utilities | Rest Explorer
We will use /job/ingest resource to create create a Job
URI | /services/data/vXX.X/jobs/ingest |
Request Method | POST |
Request Header | Content-Type: application/json; charset=UTF-8Accept: application/json |
Request Body | { “object”: “Shift”, “contentType”: “CSV”, “operation”: “insert”, “lineEnding”: “CRLF”} |
You should get a response that includes the job ID, with a job state as Open. You’ll use the job ID in Bulk API 2.0 calls in the next steps
Step 3: Upload your CSV data
After creating a job, you’re ready to upload your data. You will provide record data using the CSV formatted data or file you created in steps 1.
URI | /services/data/vXX.X/jobs/ingest/jobId/batches |
Request Method | PUT |
Request Header | Content-Type: text/csvAccept: application/json |
Request Body | Sample Data: EndTime,StartTime,Status,ServiceResourceId,ServiceTerritoryId,OwnerId,TimeSlotType,WorkTypeGroupId,WorkTypeId2022-01-14T14:00:00.000Z,2022-01-14T09:30:00.000Z,Tentative,0HnS700000007xwKAA,0HhS70000004EkMKAU,005S7000000VndPIAS,Normal,,2022-01-15T14:00:00.000Z,2022-01-15T09:30:00.000Z,Tentative,0HnS70000004EFyKAM,0HhS70000004EkMKAU,005S7000000VndRIAS,Normal,, |
Step 4: Upload complete
Once you’re done submitting data, you can inform Salesforce that the job is ready for processing by closing the job.
URI | /services/data/vXX.X/jobs/ingest/jobId |
Request Method | PATCH |
Request Header | Content-Type: application/json; charset=UTF-8Accept: application/json |
Request Body | { “state” : “UploadComplete”} |
Step 5: Check the job status and results.
To get basic status information on a job, such as the overall job state or the number of records processed, use a GET request with the following details:
URI | /services/data/vXX.X/jobs/ingest/jobId |
Request Method | GET |
Request Header | Content-Type: application/json; charset=UTF-8Accept: application/json |
job has been completed and is in the JobComplete state (or Failed state)
Step 6: Check the processed data.
To verify that record was successfully processed or not successfulResults for processed data and failedResults for the records are not processed
URI | /services/data/vXX.X/jobs/ingest/jobId/successfulResultsservices/data/vXX.X/jobs/ingest/jobId/failedResults |
Request Method | GET |
Request Header | Content-Type: application/json; charset=UTF-8Accept: application/json |