0

I have a trigger on the Opportunity object and am required to process the record in the trigger if the opportunity stage is at a specific value or later in the sales process.

new -> quoting -> negotiation -> signature -> closed won

if the stage is at the "negotiation" stage or later it should be processed.

The only way I can think to do this is by hardcoding the stage values for each sales process and seeing if the current stage is in that set.

But this is problematic if more stages are added later.

I also thought about comparing the stage's probability and if the current opportunity is >= the probability process. But this is also problematic because opportunity probability can be changed manually.

Is there a "Best Approach" to this problem?

1

Thanks to List.indexOf (new in v42.0), you can do something like this:

String[] stages = new String[0];
for(PicklistEntry value: Opportunity.StageName.getDescribe().getPicklistValues()) {
  stages.add(value.getValue());
}
for(Opportunity record: Trigger.new) {
  if(stages.indexOf('Negotation') <= stages.indexOf(record.StageName)) {
    // Do something here
  }
}

A map is slightly more efficient if you have a of stages, though:

Map<String, Integer> stages = new Map<String, Integer>();
Integer index = 0;
for(PicklistEntry value: Opportunity.StageName.getDescribe().getPicklistValues()) {
  stages.put(value.getValue(), index++);
}
for(Opportunity record: Trigger.new) {
  if(stages.get('Negotation') <= stages.get(record.StageName)) {
    // Do something here
  }
}
| improve this answer | |
  • What order does Opportunity.StageName.getDescribe().getPicklistValues() return the values in? The documentation doesn't say if it returns the values in the order of the sales process – TemporaryFix Feb 7 '18 at 22:41
  • @Programatic The documentation doesn't say it explictly, but it's the order of the list. See this question. – sfdcfox Feb 7 '18 at 22:44
  • Oh, that's more elegant than the answer I posted (I was working on it when you posted yours!) – PatMcClellan__c Feb 7 '18 at 23:16
0

Here's how to dynamically get a list of the stagenames:

Schema.DescribeFieldResult fieldResult = Opportunity.StageName.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
List<String> StageNames = new List<String>();
for(Schema.PicklistEntry s:ple){

    StageNames.add(s.getLabel());
} 

From there, you can get the index of "Negotiation" or whatever field you want, then set up a loop going from that index + 1 to StageNames.length() - 1 to build a list of stages after negotiation.

Then, just check to see if the current stageName is in the list of stages after negotiation.

Hope that helps.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.