function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
edoardo_spanoedoardo_spano 

Opportunity Probability doesn't work

Hi all,

I have a custom object with Opportunity as parent object.
When a new record of this custom object is created, a trigger updates the Stage of the opportunity, but the Probability doesn't updated.

Example:

These are the Opportunity's Stages:
- Stage1 (10%)
- Stage2 (30%)
- Stage3 (70%)
- Stage4 (100%)

The opportunity "OPP1" now is in "Stage2" and has Probability=30%. After inserting the record "CO1" as child record of "OPP1", the stage of "OPP1" is updated in "Stage3", but its Probability is still 30%.

Someone can explain to me the reason of this behavior.

Thanks in advance.
Best Answer chosen by edoardo_spano
edoardo_spanoedoardo_spano
I have solved! The code looks like this:

Map<String, OpportunityStage> stageMap = new Map<String, OpportunityStage>();
for(OpportunityStage oStage : [select MasterLabel,DefaultProbability from OpportunityStage where isActive = true]){
stageMap.put(MasterLabel,DefaultProbability);
}

Schema.RecordTypeInfo rt = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('3 - Development');
Opportunity project = [SELECT Name, StageName, RecordTypeId, Collection__c
                                       FROM Opportunity
                                       WHERE Id = :off.OpportunityId];
if(project.StageName != 'Open Order' && project.StageName != 'Won'){
    project.StageName = 'Open Development';
    project.RecordTypeId = rt.getRecordTypeId();
    project.probability = stageMap.get(project.StageName);
}
project.Collection__c = off.Collection_def__c;
update project;

All Answers

Sonam_SFDCSonam_SFDC
Can you please share the code of the trigger you have written to update the stage of the opportunity record - I can try that on my test org and troubleshoot..Ideally it should update the probability of the opp.
edoardo_spanoedoardo_spano
I have solved! The code looks like this:

Map<String, OpportunityStage> stageMap = new Map<String, OpportunityStage>();
for(OpportunityStage oStage : [select MasterLabel,DefaultProbability from OpportunityStage where isActive = true]){
stageMap.put(MasterLabel,DefaultProbability);
}

Schema.RecordTypeInfo rt = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('3 - Development');
Opportunity project = [SELECT Name, StageName, RecordTypeId, Collection__c
                                       FROM Opportunity
                                       WHERE Id = :off.OpportunityId];
if(project.StageName != 'Open Order' && project.StageName != 'Won'){
    project.StageName = 'Open Development';
    project.RecordTypeId = rt.getRecordTypeId();
    project.probability = stageMap.get(project.StageName);
}
project.Collection__c = off.Collection_def__c;
update project;
This was selected as the best answer