You need to sign in to do that
Don't have an account?
dev per
Contains method on empty Set<>
Hi,
All I am trying to do is if the Opty status is changed to Stage A or Stage B and if there are no Service contract associated with it then perform some action.
But my question is , Do I need to check set size before calling contains() method on a set.
I have a trigger on opty where a set of Opty Ids are created, if a Service contract is associated to it. But the set size (Set.size()>0) is not checked before using contains method (Set.sontains()). Even though its not throwing Dereference to null object , I want to understand what would a Contains method check if the set is empty and why is it not throwing any error.
Please advise.
Below is my code
trigger trg_aftr_ins_upd on Opportunity (after insert, after update) {
Boolean skipValidations = false;
skipValidations = SkipTriggerValidation.performTriggerValidations();
if(skipValidations == false){
Set<String> optyCancelledStages = new Set<String>();
optyCancelledStages.add('Stage A');
optyCancelledStages.add('Stage B');
Map<Id, String> optyIdProjectStatusMap = new Map<Id, String>();
Set<id> scOpptySet=new Set<id>();
if(trigger.isupdate){
for(ServiceContract sc:[select id,opportunity__c from ServiceContract where Opportunity__c in:trigger.newMap.keyset()]){
scOpptySet.add(sc.opportunity__c);
}
}
for(Opportunity o:Trigger.new)
{
Opportunity oldOpportunity = null;
if(trigger.isUpdate){
oldOpportunity = trigger.oldMap.get(o.Id);
}
if(trigger.isUpdate && oldOpportunity.stageName != o.StageName && optyCancelledStages.contains(o.StageName))
{
if( !scOpptySet.contains(o.id) ) /// this is currently working but will this throw Derefernce to Null pointer in any scenario?
optyIdProjectStatusMap.put(o.Id, wfUtil.CANCELLED);
}
if(optyIdProjectStatusMap != null && !optyIdProjectStatusMap.isEmpty()){
//Perform some action here......
}
}
}
All I am trying to do is if the Opty status is changed to Stage A or Stage B and if there are no Service contract associated with it then perform some action.
But my question is , Do I need to check set size before calling contains() method on a set.
I have a trigger on opty where a set of Opty Ids are created, if a Service contract is associated to it. But the set size (Set.size()>0) is not checked before using contains method (Set.sontains()). Even though its not throwing Dereference to null object , I want to understand what would a Contains method check if the set is empty and why is it not throwing any error.
Please advise.
Below is my code
trigger trg_aftr_ins_upd on Opportunity (after insert, after update) {
Boolean skipValidations = false;
skipValidations = SkipTriggerValidation.performTriggerValidations();
if(skipValidations == false){
Set<String> optyCancelledStages = new Set<String>();
optyCancelledStages.add('Stage A');
optyCancelledStages.add('Stage B');
Map<Id, String> optyIdProjectStatusMap = new Map<Id, String>();
Set<id> scOpptySet=new Set<id>();
if(trigger.isupdate){
for(ServiceContract sc:[select id,opportunity__c from ServiceContract where Opportunity__c in:trigger.newMap.keyset()]){
scOpptySet.add(sc.opportunity__c);
}
}
for(Opportunity o:Trigger.new)
{
Opportunity oldOpportunity = null;
if(trigger.isUpdate){
oldOpportunity = trigger.oldMap.get(o.Id);
}
if(trigger.isUpdate && oldOpportunity.stageName != o.StageName && optyCancelledStages.contains(o.StageName))
{
if( !scOpptySet.contains(o.id) ) /// this is currently working but will this throw Derefernce to Null pointer in any scenario?
optyIdProjectStatusMap.put(o.Id, wfUtil.CANCELLED);
}
if(optyIdProjectStatusMap != null && !optyIdProjectStatusMap.isEmpty()){
//Perform some action here......
}
}
}
You code look good to me. You can use contains method without any null check. Because you already create the object of your set.
Set<id> scOpptySet=new Set<id>();
Contains Method is used to check values is available in Set ot not.