Forum Discussion
AP_TC_ECASD
Jan 23, 2025Copper Contributor
New-MgBookingBusinessService CustomQuestions
Hi!
I'm working my way though BookingBusiness with PowerShell, so please forgive me if this is obvious. I've tried combing documentation but nothing seems to work. I have this script, and first, I think I have to create the customQuestions and have done so successfully and have the reference IDs for the questions to use in the New-MgBookingBusinessService script. I cannot seem to get the customQuestion piece to work. I'm first getting the available business staff and the custom questions I've already created. Everything works until I try the CustomQuestions piece. Here is what I have if you could please provide any assistance or guidance. Thank you!
# Define the Booking Business ID
$bookingBusinessId = "SCRUBBED"
# Path to your CSV file
$csvFilePath = "SCRUBBED"
# Import CSV file
$staffEmails = Import-Csv -Path $csvFilePath
# Retrieve all staff members for the booking business
Write-Host "Fetching all staff members for booking business ID: $bookingBusinessId"
$allStaff = Get-MgBookingBusinessStaffMember -BookingBusinessId $bookingBusinessId
# Ensure $allStaff is not null or empty
if (-not $allStaff) {
Write-Error "No staff members found for the booking business ID: $bookingBusinessId"
return
}
# Debugging: Display all staff members retrieved (with Email and ID)
Write-Host "Staff members retrieved (Email and ID):" -ForegroundColor Green
$allStaff | ForEach-Object {
$email = $_.AdditionalProperties["emailAddress"]
$id = $_.Id
$displayName = $_.AdditionalProperties["displayName"]
Write-Host "DisplayName: $displayName, Email: $email, ID: $id" -ForegroundColor Yellow
}
# Retrieve all custom questions for the booking business
Write-Host "Fetching all custom questions for booking business ID: $bookingBusinessId"
$allCustomQuestions = Get-MgBookingBusinessCustomQuestion -BookingBusinessId $bookingBusinessId
# Ensure $allCustomQuestions is not null or empty
if (-not $allCustomQuestions) {
Write-Error "No custom questions found for the booking business ID: $bookingBusinessId"
return
}
# Debugging: Display all custom questions retrieved (with ID and DisplayName)
Write-Host "Custom questions retrieved (ID and DisplayName):" -ForegroundColor Green
$allCustomQuestions | ForEach-Object {
$id = $_.Id
$displayName = $_.DisplayName
Write-Host "ID: $id, DisplayName: $displayName" -ForegroundColor Yellow
}
# Loop through each staff member in the CSV and create an individual service for them
Write-Host "Creating individual booking services for each staff member..."
$staffEmails | ForEach-Object {
$email = $_.staffemail.Trim().ToLower()
# Find the matching staff member by email
$matchingStaff = $allStaff | Where-Object {
$_.AdditionalProperties["emailAddress"] -and ($_.AdditionalProperties["emailAddress"].Trim().ToLower() -eq $email)
}
if ($matchingStaff) {
$staffId = $matchingStaff.Id
Write-Host "Match found: Email: $email -> ID: $staffId" -ForegroundColor Cyan
# Create the booking service for the matched staff member
try {
$serviceParams = @{
BookingBusinessId = $bookingBusinessId
DisplayName = "$($matchingStaff.AdditionalProperties["displayName"]) Family Conference"
StaffMemberIds = @($staffId) # Create a service only for this staff member
DefaultDuration = [TimeSpan]::FromHours(1)
DefaultPrice = 50.00
DefaultPriceType = "free"
Notes = "Please arrive 10 minutes early for your booking."
CustomQuestions = $allCustomQuestions | ForEach-Object {
@{
Id = $_.Id
IsRequired = $true # or $false depending on your requirement
}
}
}
# Log the parameters being sent
Write-Host "Service Parameters for $($matchingStaff.AdditionalProperties["displayName"]):" -ForegroundColor Blue
$serviceParams.GetEnumerator() | ForEach-Object { Write-Host "$($_.Key): $($_.Value)" }
New-MgBookingBusinessService @serviceParams
Write-Host "Booking service successfully created for $($matchingStaff.AdditionalProperties["displayName"])!" -ForegroundColor Green
} catch {
Write-Error "Failed to create booking service for $($matchingStaff.AdditionalProperties["displayName"]): $_"
}
} else {
Write-Warning "No match found for email: $email"
}
}
- lucheteSteel Contributor
Hello AP_TC_ECASD
It looks like you're getting the staff members and custom questions correctly, and you’re looping through the staff and trying to create a new booking service with custom questions. It seems to me that the issue may be specifically with how you're passing the CustomQuestions property in the New-MgBookingBusinessService call.
I would try to modify the way you construct the CustomQuestions property in $serviceParams. Try using a New-Object to create proper objects rather than creating hashtables like:CustomQuestions = $allCustomQuestions | ForEach-Object { New-Object PSObject -Property @{ Id = $_.Id IsRequired = $true # or $false depending on your requirement } }
I think this will ensure that each custom question is represented as an object rather than a simple hashtable.
You can also add a debug printout to see the structure of the CustomQuestions that you are sending with this line of code:Write-Host "Custom Questions being sent: $($serviceParams.CustomQuestions)" -ForegroundColor Cyan
This way, you can confirm that the CustomQuestions is being built correctly before making the New-MgBookingBusinessService call.
Hope this helps!
- AP_TC_ECASDCopper Contributor
That worked!!!
Part 2 of this... I know how to Get-MgBookingBusinessAppointment but how do I get the customer responses to the Custom Questions?
- lucheteSteel Contributor
Hi AP_TC_ECASD
Nice hear that worked! Now for getting the customer responses to the custom questions, probably i'll try to use the Get-MgBookingBusinessAppointment command to fetch the appointment details. Them, after that, the responses to the custom questions will be part of the CustomQuestions property in the appointment object.Here’s a way to try to get the responses:
$appointmentId = "your-appointment-id" # Replace with the actual appointment ID $appointment = Get-MgBookingBusinessAppointment -BookingBusinessId $bookingBusinessId -AppointmentId $appointmentId # Display the custom questions and responses $appointment.CustomQuestions | ForEach-Object { Write-Host "Question: $($_.DisplayName), Response: $($_.Answer)" -ForegroundColor Green }
You only have to replace $appointmentId with the ID of the appointment you're interested in, and this should give you the responses to the custom questions for that appointment. I think this may work. Let me know how it goes!