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
kittu9kittu9 

Code not working fine for Update event.

The req is i have pick list name is "Rank" have values like 100,200,300 and i want to keep these values as unique. And i have one more field "Role". If Role is CPD and rank is already used by any other record have to display error message for that i written trigger. That is working fine for insert event. Not working for update can any one help me on this.

 

The following is the trigger: 

 

trigger CheckRankValues on Account_Team_Commission__c (before insert)
{
  public List<Account_Team_Commission__c> c;
 for(Account_Team_Commission__c ct :Trigger.new)
 {
  if(Trigger.isBefore && Trigger.isInsert)
  {
      c = [select Id,Name,Rank__c,Team_member_role__c  from Account_Team_Commission__c];
      
      if(c != null && c.size() > 0)
      {
        for ( Integer i = 0 ; i< c.size() ; i++)
        {
          if ( (ct.Rank__c == c.get(i).Rank__c) && (ct.Team_member_role__c == 'Corporate Partnership Directors'))
           ct.addError('Rank  Must be Unique value : '+ct.Rank__c);
        }
      }
  }
  if(Trigger.isBefore && Trigger.isUpdate)
  {
     c = [select Id,Name,Rank__c,Team_member_role__c  from Account_Team_Commission__c];
      
      if(c != null && c.size() > 0)
      {
        for ( Integer i = 0 ; i< c.size() ; i++) //Account accts = Trigger.oldMap.get(acc.Id);
        {
          Account_Team_Commission__c atc = Trigger.oldMap.get(c.get(i).Id);
          if ( (atc.Rank__c == c.get(i).Rank__c) &&(atc.Team_member_role__c == 'Corporate Partnership Directors'))
           ct.addError('Rank Must be Unique value : '+ct.Rank__c);
        }
      }
  }
 }
}

 

Scenario:

------------

       Name    Role   Rank

Eg: ABC      CPD    1     (Correct)

       DEF      CPD   2     (Correct)

       GHI       CPD  1  (Incorrect ) with same role and rank already used so wont allow this type

For this i written above trigger,working for insert event not working for Update. 

 

Puja_mfsiPuja_mfsi

Hi,

You have written the trigger only for insert event not update.You need to add "before update" event in your trigger.

trigger CheckRankValues on Account_Team_Commission__c  (before insert, before update ) {

}

please let me know if the issue is not resolved.

 

kittu9kittu9
ok sure thanks