+ Start a Discussion
Anthony Suda RMAnthony Suda RM 

Updating Opportunity to Closed Won via APEX doesn't update Probability to match 100%

private Opportunity currentOpp;

    public ConvertOppToProjectControllerExtension(ApexPages.StandardController stdController) {
        this.currentOpp = (Opportunity)stdController.getRecord();
    public ConvertOppToProjectControllerExtension(Opportunity currentOpp) {
      this.currentOpp = currentOpp;

      currentOpp = [SELECT Id, Abbreviation__c, AccountId, CloseDate, Name, OwnerId, Probability, Description, StageName, Portfolio__c,
              NextTaskNumber__c, Company__c, Service_Type__c, AM_Sales_Ops__c,
                (SELECT Name, Approved__c, Opportunity__c, Estimate_Total_Dollars__c FROM Estimates__r)
          FROM Opportunity
          WHERE Id = :currentOpp.Id];

      //Convert the Opportunity itself
      currentOpp.StageName = 'Closed Won';
      currentOpp.CloseDate = System.now().date();      

We have the Closed Won stage set at 100% Probability. If we manually move the Oppty to Closed Won and save, the Probability updates to 100% jsut fine. But when using the code below, it updates the stage correctly but the Probability remains where it was. We have this working in our Prod instance, but we're attempting some stage renames (probabilities staying the same - and not changing Closed Won), but it's not updating the Probability field.


The probability field is directly linked to the stage value selected, however, the probability can be manually edited by users who have edit access to this field. Since Salesforce allows this, it means that the probability value doesn't act as a fixed value, instead, it works as a default value.

For example, if you choose Closed/Won on the stage, Salesforce will give you a default value of 100%, but you can manually edit this value to 57% for example.

Because of this, when you change the probability linked to a stage value under Setup, existing opportunities will not show the new probability value, since the probability was already saved on the record before the change. If you then go to one of these opportunities and manually change the stage, you will now see its new default value now you can select it too.

Hope this helps !!
Anthony Suda RMAnthony Suda RM
Let me clarify - this is testing a brand new setup with a brand new Oppty. I'm not manually setting the Probability at all in these tests nor am I editing it anywhere else in code. It's staying set and moving with the stage changes.
Well technically speaking yes, just changing the stage name should have done that.
Can you try running the below query in your code and checking the probability there :
[SELECT DefaultProbability, masterlabel FROM OpportunityStage WHERE masterlabel =: <StageName>];
Anthony Suda RMAnthony Suda RM
DefaultProbability			MasterLabel
100							Closed Won

I don't see any issue why it should not update, because I have a working code in my dev org, that seems to be updating the probability just fine.
I won't remain same, unless there is some code in the trigger that is updating it to the old probability.
Anthony Suda RMAnthony Suda RM
That exactly our thoughts. It's happening in both our Sandboxes, but not Production. We don't have any code/field updates that touch Probability. Which is why we wanted to log a case on this but since we're not "Premier" partners, we had to log the issue here in stead. 

We've setup dozens of Salesforce Orgs in the past with custom integrations and code and have never seen this happen before. Oppty Stage change always updates Probability to match... now all of a sudden it's not, and we can't find the answer as to why. 
Ok, just a check perhaps, no workflow or visualflows or process builder to do this manipulation.
What I would suggest is to refer the debug logs closely to see at what point the value is getting manipulated.
Anthony Suda RMAnthony Suda RM
That was the second thing I did - I setup debug logs and watched an Oppty from being first created all the way through conversion/closed won. At no point is the Probability % field edited by anything other than the Stage change.
Susan NessonSusan Nesson
Hi Anthony,
Did you find a solution to your problem? I'm seeing something like this with Auctions for Salesforce when updating an opportunity at checkout. Could be something completely different, but I'm stumped.
Anthony Suda RMAnthony Suda RM
No we haven't found any solution to this. The logs don't show us any errors or updates attempted to the Stage. We're at a complete loss.
João AlmeidaJoão Almeida
If the parameter "currentOpp" is coming from the trigger, it is read-only. So, to update opportunity data, you should select to a new object with the currentOpp.Id and then update fields on the new object from the query