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
mcareymcarey 

Making "Competitor Required" validation on an Opportunity

Any pointers on how to make it required to have at least one value in the competitor related list on close lost/close won of an Opportunity?  This is not available in the standard validation on the Opp since I can't get to the Competitor fields.  Any thoughts or code you have written for something similar?

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

Yes you'll need a trigger for this.

 

trigger checkForCompetitors on Opportunity (before update) 
{
	Set<Id> closedOppIds = new Set<Id>();
    Set<Id> oppsHavingCompetitors = new Set<Id>();

     // fetch all the Opportunity Id's that have been closed
     for(Opportunity opp : trigger.new)
     {
          if(trigger.OldMap.get(opp.Id).StageName != 'Closed Won' && trigger.OldMap.get(opp.Id).StageName != 'Closed Lost'
             && (opp.StageName == 'Closed Won' || opp.StageName == 'Closed Lost'))
          {
                closedOppIds.add(opp.Id);
          }
     }

     // fetch Opportunity Id's that have been closed and have a Competitor
     for(OpportunityCompetitor oc : [Select OpportunityId From OpportunityCompetitor Where OpportunityId IN : closedOppIds ])
     {
          oppsHavingCompetitors.add(oc.OpportunityId);
     }

     // compare the two sets (one has closed opportunity id's and other has closed opps hacing a competitor)
     // Left out opp's should not be allowed to close, so we throw an error.
     for(Opportunity o : trigger.new)
     {
           if(closedOppIds.contains(o.Id) && !oppsHavingCompetitors.contains(o.Id))
                o.addError('Atleast one competitor is required before closing an Opportunity');
     }
}

 Hope this helps :)

All Answers

Hengky IlawanHengky Ilawan

Hi,

 

Maybe you can write a trigger to check into the OpportunityCompetitor object if there is at least one record tagged to the current opportunity?

 

Regards,

Hengky

vishal@forcevishal@force

Yes you'll need a trigger for this.

 

trigger checkForCompetitors on Opportunity (before update) 
{
	Set<Id> closedOppIds = new Set<Id>();
    Set<Id> oppsHavingCompetitors = new Set<Id>();

     // fetch all the Opportunity Id's that have been closed
     for(Opportunity opp : trigger.new)
     {
          if(trigger.OldMap.get(opp.Id).StageName != 'Closed Won' && trigger.OldMap.get(opp.Id).StageName != 'Closed Lost'
             && (opp.StageName == 'Closed Won' || opp.StageName == 'Closed Lost'))
          {
                closedOppIds.add(opp.Id);
          }
     }

     // fetch Opportunity Id's that have been closed and have a Competitor
     for(OpportunityCompetitor oc : [Select OpportunityId From OpportunityCompetitor Where OpportunityId IN : closedOppIds ])
     {
          oppsHavingCompetitors.add(oc.OpportunityId);
     }

     // compare the two sets (one has closed opportunity id's and other has closed opps hacing a competitor)
     // Left out opp's should not be allowed to close, so we throw an error.
     for(Opportunity o : trigger.new)
     {
           if(closedOppIds.contains(o.Id) && !oppsHavingCompetitors.contains(o.Id))
                o.addError('Atleast one competitor is required before closing an Opportunity');
     }
}

 Hope this helps :)

This was selected as the best answer
mcareymcarey

Thanks, I think this is what I need.  I'll try it out on the sandbox.