[SFDC integration] Recover from Restforce::ErrorCode::DuplicateValue on Quote creation
Problem
Currently, it is possible for CDot to create duplicate ClosedWon
opportunities which represents extra work to the Billing/Sales teams.
When duplicate opportunities are created from CDot, the second Quote
creation fails giving us a hint that a duplicate was created. Unfortunately, at this point the second opportunity ID has been already updated on the Zuora Subscription and the rest of the Quote objects are missing for it.
Proposal
Since the second opportunity raises a DuplicateValue
error, we could rescue that specific error and attempt to "undo" the second opportunity.
At the time this error is raised we have an error message that includes the Quote ID of the -non errored- opportunity like this:
#<Salesforce::QuoteNotCreated:"DUPLICATE_VALUE: duplicate value found: Zuora_Subscription_ID_GitLab__c duplicates value on record with id: a1jPL000000A4qLYAS\nRESPONSE: [{\"message\":\"duplicate value found: Zuora_Subscription_ID_GitLab__c duplicates value on record with id: a1jPL000000A4qLYAS\",\"errorCode\":\"DUPLICATE_VALUE\",\"fields\":[]}]">
If we could extract that id and load the Quote from SFDC to get the complete opportunity id, we could make sure the OpportunityID stamped on the Zuora Subscription is the complete one.
# find the "complete" opportunity id from the Quote id in the error and use it to update the Zuora Subscription when done
quote_id_from_error = 'a1jE20000073fSPIAY'
query = "SELECT zqu__Opportunity__c from zqu__Quote__c WHERE Id = '#{quote_id_from_error}'"
complete_opportunity_id = result.first["zqu__Opportunity__c"]
zuora_subscription.update_async('OpportunityID__c' => complete_opportunity_id)
On the same process we have the -errored- opportunity id, the subscription and invoice id. If we can mark the errored opportunity as a Duplicate of the complete one, then we wouldn't have Closed Won
duplicates. Currently, this can't be completed because the update raises FIELD_CUSTOM_VALIDATION_EXCEPTION
s.
# to update the duplicate opportunity
attrs = { :Id=>"006E2000008KQ6bIAG", :StageName=>"10-Duplicate", :Duplicate_Opportunity__c=> complete_opportunity_id, :ForecastCategoryName=>"Omitted" }
update_query = SalesforceClient.update!('Opportunity', attrs)
marking the opportunity as a Duplicate
we will need to set Stage
to 10-Duplicate
and Forecast Category
to Omitted
.