0

I have a trigger that needs to prevent users from using assigning the same Ranking value to a record if the record Status is not 'Closed'

trigger RemedyForcePreventRankingDuplicate on BMCServiceDesk__Incident__c (BEFORE INSERT,BEFORE UPDATE) {

{
    LIST<BMCServiceDesk__Incident__c> li = [select id,Ranking__c,BMCServiceDesk__Status_ID__c from BMCServiceDesk__Incident__c where BMCServiceDesk__Status_ID__c != 'CLOSED' And Ranking__c!= null]; 
    for(BMCServiceDesk__Incident__c bmc :trigger.new)
    {
     if(bmc.Ranking__c != null && bmc.BMCServiceDesk__Status_ID__c != 'CLOSED' )
         {
          for(BMCServiceDesk__Incident__c existingbmcrecord :li)
             {
                 if(existingbmcrecord.Ranking__c == bmc.Ranking__c)
                     {
                     bmc.Ranking__c.adderror('Ranking value is already in use.  Please select another Ranking value.');
                     }
              }
         }
    }
}

}

Will someone point out what is incorrect about the trigger? I am able to save multiple records with the Ranking value. The Ranking value is a picklist field.

M

0

You need to check to see if the records being committed have the same ranking not just with the ones already in the DB, but also between themselves.

The way you have written this code is very inefficient. You would be far better off creating a Set of the type of the Ranking value (string for picklist) and simply add the existing rankings into this set first, then check and (if not there) add the rankings of the trigger objects as you go.

I say it is inefficient because you have nested for loops. For loops are really slow in Apex so nesting them is a really bad idea.

Try something like:

trigger RemedyForcePreventRankingDuplicate on BMCServiceDesk__Incident__c (before insert, before update) {
    Set<String> existingRankings = new Set<String>();
    List<BMCServiceDesk__Incident__c> incidents = Trigger.new;

    for (BMCServiceDesk__Incident__c incident : [
            SELECT Ranking__c FROM BMCServiceDesk__Incident__c
                WHERE Id NOT IN :incidents
                    AND BMCServiceDesk__Status_ID__c != 'CLOSED'
                    AND Ranking__c != NULL]) {
        existingRankings.add(incident.Ranking__c);
    }

    for (BMCServiceDesk__Incident__c incident : incidents) {
        if (incident.Ranking__c != null && incident.BMCServiceDesk__Status_ID__c != 'CLOSED') {
            if (existingRankings.contains(incident.Ranking__c)) {
                incident.Ranking__c.addError('Ranking value is already in use.  Please select another Ranking value.');
            } else {
                existingRankings.add(incident.Ranking__c);
            }
        }
    }
}
  • BTW, I realized there is a gap in this logic; when doing the query for existing items you need to exclude the incidents being updated. Will update. – Phil W May 2 at 10:12

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.