0

Attachment and checkbox is true then show error message record already activated.

I tried this trigger

trigger con on contact (before insert) { 

Boolean active= false; 

    if (Trigger.isBefore) { 


                  email.Adderror('Record already activated'); 
       else active= false; 
            } 
}
3
  • Vipin you need trigger. You need to query record and check if there is an already activated record is present then show error
    – Ratan Paul
    Commented Feb 16, 2016 at 11:20
  • so, the user should not be able to add attachment to activated record?
    – ipradhansk
    Commented Feb 16, 2016 at 11:27
  • Possible duplicate of how to apply validation rule on checkbox?
    – Jarvis
    Commented Feb 16, 2016 at 11:54

3 Answers 3

1

Use trigger before insert, before update you can achieve this. You can put SOQL Query also in trigger to check the activated record. If that record exist then you throw error.

1
  • Could someone explain me please why this answer is not useful. Commented Feb 16, 2016 at 11:34
1

Try this piece of code.This will help you.

trigger trig_IsActive on eTMFFolder__c (before insert) { 

Boolean isFirstTime = false; 
Set<string> typeSet = new Set<string>();
    if (Trigger.isBefore) { 

        if (Trigger.isInsert) { 
            for (eTMFFolder__c fol : Trigger.new) { 
               if(fol.isActive__c)
                  typeSet.add(fol.Type__c);
            } 
           List<eTMFFolder__c > folList = [select id from eTMFFolder__c  WHERE Type__c  IN: typeSet];
          if(folList.size() > 0)
                Trigger.new[0].Adderror('Record already activated');
        } 
    } 
}
8
  • If user inserting 2 records and first records and first record isActive__c false but second record isActive__c true then first will receive an error.?
    – Ratan Paul
    Commented Feb 16, 2016 at 11:35
  • 1
    @Ratan yes it is but it is a sample code. There are many case which come when you write final code. OP need to handle and bulkfy this code. Commented Feb 16, 2016 at 11:37
  • 1
    @VipinIndora add before update event as well
    – Ratan Paul
    Commented Feb 16, 2016 at 11:45
  • @VipinIndora test may be some other records are checked Commented Feb 17, 2016 at 6:40
  • @VipinIndora are you doing this in bulk update? Commented Feb 17, 2016 at 6:49
0

You can have a before insert trigger which will check the Type__c and isActive__c field if found a single record with the same type and isActive__c equal true then Use addError method.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .