1

I got an object called 'Category' which has a master detail relationship with 'Scheme' object, 'category' has a pick list field called 'Type' & it has different values Ex:Cost,Time etc.

I need to write a trigger to validate selection of pick-list values. Once a pick-list value is selected it should not be used in another record and if user tries to select that same pick-list value and try to save in a new record, it should give an error and should not be able to save the record.

I'm bit confused as I have not done such scenario before.

  • You need a new trigger on Category Object? – Ramesh S Jul 8 '14 at 14:26
  • Yes, I need to write the trigger on category object. – Tinkerbell Jul 8 '14 at 14:34
  • I have posted the answer can you please try and let me know. – Ramesh S Jul 8 '14 at 14:46
  • @Ramesh Your code works perfectly fine ! Thanks alot! Just keen to know there's another scenario I need to work on, which is, there are two types (Ex: Cost, %Cost) the trigger should not let save another type of cost. Altogether there are four types (Cost, %Cost, Time, %Time). If there is 'Cost' type, now can only consider either 'Time' or '%Time' for the new record. Have you got any thoughts on this ? Thanks in advance! – Tinkerbell Jul 8 '14 at 15:22
1

Can you try the following code.It should work.

trigger Triggertovalidate on category__c (BEFORE INSERT,BEFORE UPDATE)
    {
        LIST<category__c> li = [select id,type from category__c where type != null]; 
        for(category__c c :trigger.new)
        {
         if(c.type != null){
          for(category__c existrecord :li)
          {
              if(existrecord.type == c.type){
              c.Type.adderror('Type is already used please select another one.');//If you need specify the SObject field also
              }
          }
         }
        }

    }
  • Thanks for your help. Your code works perfectly fine! There is another scenario I need to add to the trigger, which is if there are two types (Ex: Cost, %Cost) the trigger should not let save another type of cost. Altogether there are four types (Cost, %Cost, Time, %Time). If there is 'Cost' type, now can only consider either 'Time' or '%Time' for the new record. – Tinkerbell Jul 8 '14 at 15:18
  • If there are a lot of categories in the system, this may be a non-selective query as salesforce will have to do a table scan in order to fulfill the where clause. – Sean Jul 8 '14 at 16:59
  • @Butterfly If you have another question then please create another question, do not keep adding additional queries and answers to this one. – Alex Tennant Jul 9 '14 at 9:47
0

it will be a before insert, before update trigger on Category__c

In the trigger, you need to run a query on existing categories, and if it returns any results, throw an error.

From what it sounds, the SCheme object is irrelevant, unless you need to allow one picklist value per parent, not org-wide.

Something like below. You could also add the error to specific field.

trigger CategoryTypeTrigger on Category__c (before insert, before update) {

Set<String> types = new Set<String>();
for (Category__c cat : Trigger.new) {

     // skip records where Type is not populated
     if (cat.Type__c == null) continue;

     // on insert, get the Category Type
     if (Trigger.isInsert) {
         types.add(cat.Type__c);
         continue;
     }

     // on update, get Type only if Type had changed
     Category__c oldCat = Trigger.oldMap.get(cat.id);
     if (cat.Type__c != oldCat.Type__c) types.add(cat.Type__c);
}
if (catIds.isEmpty()) return;

List<Category__c> cats = [select id from Category__c where Type__c in :types limit 1];

if (cats.size() != 0) Trigger.new[0].addError('There is already a Category with this Type');   
}
  • Thanks for your answer. I think your logic is correct. I think you have not declared 'catIds' variable, i'm getting an error because of that. – Tinkerbell Jul 8 '14 at 16:08
0

Alternatively, you can use a unique field on the Category__c object to accomplish this. Once the pick list is populated, use a field update to populate the unique field with picklist value. If another record uses the same picklist value, the unique field validation will throw an error and prevent the save.

  • Yes I Agree to @Sean.We can also implement same scenario by using work-flow validation rule. – Ramesh S Jul 9 '14 at 5:17
  • I had same thoughts on this, but is there a way to check the records which are already saved if I use a validation rule? Because I need to check if a pick list value is used in a previous record in order to avoid saving the same pick-list value in a new record ? – Tinkerbell Jul 9 '14 at 8:15
  • So it would go like this: Picklist value is chosen, and user hit's save. While saving, the picklist value would update the unique field via field update workflow rule. If another record already has that value in the unique field, then an error will be thrown, rollback the save and then the user would be told that another record already has that picklist value. – Sean Jul 9 '14 at 15:28
0

Time, %Time, Cost, %Cost problem solved adding the given OR operators

if (Trigger.isInsert){
        LIST<Category__c> li = [select id, Type__c from Category__c where Type_of_Risk__c != null]; 
        for(Category__c c :trigger.new)
        {
         if(c.Type__c != null){
          for(Category__c existrecord :li)
          {
              if(existrecord.Type__c == c.Type__c || existrecord.Type__c == 'Cost' && c.Type__c == '%Cost'|| existrecord.Type__c == 'Time' && c.Type__c == '%Time'|| existrecord.Type__c == '%Cost' && c.Type__c == 'Cost'|| existrecord.Type__c == '%Time' && c.Type__c == 'Time'){
              c.Type__c.adderror('Type is already used please select another one.');
              }

          }
         }
        }
}
  • I wonder if I need to get the 'Scheme' Id as well since 'Category' object has a Master-Detail relationship with 'Scheme' object ? Any ideas on this ? – Tinkerbell Jul 9 '14 at 9:04
  • If you have another question then please create another question, do not keep adding additional queries and answers to this one. – Alex Tennant Jul 9 '14 at 9:46

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.