+ Start a Discussion
MT0478MT0478 

Check for duplicates and Check a checkbox

Hi,

 

I'm new to Apex, and please advise me about Apex Trigger.

I could create a trigger for custom object to find duplicates and check a checkbox.

Below is my code. This trigger can check the checkbox of new record.

But I want to check the existing record's one.

How can i do this?

 

trigger CheckIsRevised on No__c (before insert, before update) {
   if (Trigger.isInsert)
   {
   	      for (No__c no : Trigger.new){
     
         No__c[] nos= [select id from No__c where Ano__c = :no.Ano__c ];
     
         if (nos.size() > 0) {
             no.revised_flag__c=true;
         }  
      }
   }
   else if (Trigger.isUpdate)
   {
       for (No__c no : Trigger.new)
       {
          No__c oldNo=Trigger.oldMap.get(no.id);
          if (no.Ano__c != oldNo.Ano__c)
          {
             No__c[] nos= [select id from No__c where Ano__c = :no.Ano__c];
     
             if (nos.size() > 0) 
             {
                no.revised_flag__c=true;
             }
              
          }
       }
   }	
}

 Thanks in advance!

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

You cantry this one

trigger CheckIsRevised on No__c (before insert, before update) 
{
   List<No__c> nos = new List<No__c>();
   if (Trigger.isInsert)
       {
             for (No__c no : Trigger.new){
         
              nos= [select id from No__c where Ano__c =: no.Ano__c ];
         
             if (nos.size() > 0) {
                 for(No__c noOld : nos) 
                    noOld.revised_flag__c=true;
             }  
          }
       }
   else if (Trigger.isUpdate)
       {
           for (No__c no : Trigger.new)
           {
              No__c oldNo = Trigger.oldMap.get(no.id);
              if (no.Ano__c != oldNo.Ano__c)
              {
                 nos = [select id from No__c where Ano__c =: no.Ano__c];
         
                 if (nos.size() > 0) 
                 {
                    for(No__c noOld : nos) 
                        noOld.revised_flag__c=true;
                 }
                  
              }
           }
       }
       
   if(nos.size() > 0)
       update nos;    
           
}

 

All Answers

Shashikant SharmaShashikant Sharma

You cantry this one

trigger CheckIsRevised on No__c (before insert, before update) 
{
   List<No__c> nos = new List<No__c>();
   if (Trigger.isInsert)
       {
             for (No__c no : Trigger.new){
         
              nos= [select id from No__c where Ano__c =: no.Ano__c ];
         
             if (nos.size() > 0) {
                 for(No__c noOld : nos) 
                    noOld.revised_flag__c=true;
             }  
          }
       }
   else if (Trigger.isUpdate)
       {
           for (No__c no : Trigger.new)
           {
              No__c oldNo = Trigger.oldMap.get(no.id);
              if (no.Ano__c != oldNo.Ano__c)
              {
                 nos = [select id from No__c where Ano__c =: no.Ano__c];
         
                 if (nos.size() > 0) 
                 {
                    for(No__c noOld : nos) 
                        noOld.revised_flag__c=true;
                 }
                  
              }
           }
       }
       
   if(nos.size() > 0)
       update nos;    
           
}

 

This was selected as the best answer
MT0478MT0478

Thanks to your code, i could implement the trigger properly.

Thank you, Mr? Shashikant Sharma:smileyhappy:

Devendra SawantDevendra Sawant

Hi,

 

I have gone through this trigger.

 

I want to avoid adding duplicate record. If user is adding a duplicate Name then message is diplayed on user interface and that particular record should not get save. The record should save only when it is not the duplicate value.

 

Cheers,

Devendra S