+ Start a Discussion
tulasiram chtulasiram ch 

help me out in this trigger code error:

I am using custom settings in the below example : i don't know how to achieve this. i wrote below trigger but its not working please help me.
I stored some values in custom settings. Post an error  if any user try to insert a value already existed in custom settings.
  1. trigger comparingExistedNumbers on Order_Product_Item__c (before insert) 
  2. {
  3.     set<id> setIds = new set<id>();
  4.     for(Order_Product_Item__c orders : trigger.new)
  5.     {
  6.         if(orders.Enter_Number_Value__c != null)
  7.         {
  8.             setIds.add(orders.Enter_Number_Value__c);
  9.         }
  10.         list<Order_Product_Item__c> listofOrders = [select id, Enter_Number_Value__c from Order_Product_Item__c where id in:setIds];
  11.         list<string> values = new list<string>();
  12.         list<preDefinedValueList__c> cusList = preDefinedValueList__c.getAll().values(); // getting list of values in Custom settings
  13.         for(Order_Product_Item__c newOrders:listofOrders)
  14.         {
  15.         for(preDefinedValueList__c custom : cusList)
  16.             {
  17.             //system.debug('' +custom.code__c);
  18.            // system.debug('' +custom.Number__c);
  19.             values.add(custom.Number__c); // data set strings storing in values variable
  20.             }
  21.         for(integer i=0; i<values.size();i++)
  22.             {        //comparing values
  23.                 if(newOrders.Enter_Number_Value__c ==values[i]){
  24.                     newOrders.Enter_Number_Value__c.addError('Do not enter existing values');
  25.                 }
  26.             }
  27.         }
  28.     }
  29. }
Best Answer chosen by tulasiram ch
tulasiram chtulasiram ch
Finally i got it...Thanks guys for help, but i made it in my way. Below is the code.
  1. trigger comparingExistedNumbers on Order_Product_Item__c (before insert, before update) 
  2. {
  3.     if(trigger.isInsert || trigger.isUpdate){
  4.         for(Order_Product_Item__c newOrders: trigger.new){
  5.             if(newOrders.Enter_Number_Value__c != null){
  6.                 
  7.                 for(preDefinedValueList__c numbers:preDefinedValueList__c.getAll().values()){
  8.                     if(newOrders.Enter_Number_Value__c == integer.valueOf(numbers.Number__c)){
  9.                         newOrders.Enter_Number_Value__c.addError('Do not enter this value');
  10.                         
  11.                     }
  12.                 }
  13.             }
  14.         }       
  15.     }
  16. }

All Answers

HARSHIL PARIKH AKA:GovindHARSHIL PARIKH AKA:Govind
Can you tells us what is your requirements in depth?
Thank You.
tulasiram chtulasiram ch
I want to store some predifined numbers in Custom settings list(Like 1,3,5,7,9,11,13,15,17,19). When a user enters a value in the filed Enter_Number_value__c  already existed in that predefined list it will post an error like don't enter that number .

Example :custom settings list : 1,3,5,7,9,11,13,15,17,19
Enter_Number_value__c : 15
then it needs to post an error, dont enter that number.
HARSHIL PARIKH AKA:GovindHARSHIL PARIKH AKA:Govind
try using the following trigger:
You might want to test for Before Update too though since what happens when user enters number as Enter_Number_Value__c = 0, saves it and then chnage the number from 0 to 1.
Trigger comparingExistedNumbers On Lead (Before Insert, Before Update){
    
    list<preDefinedValueList__c> cusList = preDefinedValueList__c.getAll().values();
    
    If(Trigger.IsInsert || Trigger.IsUpdate)
    {
        For(Lead Ld : Trigger.New)
        {
            If(Ld.Enter_Number_Value__c != null)
            {
                For(Integer EveryInteger : cusList )
                {
                    If(Ld.Enter_Number_Value__c == EveryInteger)
                    {
                        Ld.addError('You can not add this number');
                    }
                }
            }
        }
    }
}

Hope this helps!
tulasiram chtulasiram ch
Here is the code: But i am getting an
error:
Invalid loop variable type expected preDefinedValueList__c was Integer

1.Custom settings Object:preDefinedValueList__c
Custom settings Field : Number__c(Number field)

2.Custom Object: Order_Product_Item__c
Field :Enter_Number_Value__c(Number)
  1. trigger comparingExistedNumbers on Order_Product_Item__c (before insert, before update) 
  2. {
  3.   list<preDefinedValueList__c> cusList = preDefinedValueList__c.getAll().values();
  4.     if(trigger.isInsert || trigger.isUpdate){
  5.         for(Order_Product_Item__c newOrders: trigger.new){
  6.             if(newOrders.Enter_Number_Value__c != null){
  7.                 for(integer listofIntegers:cusList){
  8.                     if(newOrders.Enter_Number_Value__c == listofIntegers){
  9.                         newOrders.Enter_Number_Value__c.addError('Do not enter this value');
  10.                     }
  11.                 }
  12.             }
  13.         }
  14.     }       
  15. }
HARSHIL PARIKH AKA:GovindHARSHIL PARIKH AKA:Govind
we need to make sure that the data type for preDefinedValueList__c and Enter_Number_Value__c is same.

If preDefinedValueList__c is Integer datatype then Enter_Number_Value__c should be a Number with Decimal 0 (18, 0)
tulasiram chtulasiram ch
Those two Fields are Number(18,0). But still i am getting sames error. Please help me out.
tulasiram chtulasiram ch
Finally i got it...Thanks guys for help, but i made it in my way. Below is the code.
  1. trigger comparingExistedNumbers on Order_Product_Item__c (before insert, before update) 
  2. {
  3.     if(trigger.isInsert || trigger.isUpdate){
  4.         for(Order_Product_Item__c newOrders: trigger.new){
  5.             if(newOrders.Enter_Number_Value__c != null){
  6.                 
  7.                 for(preDefinedValueList__c numbers:preDefinedValueList__c.getAll().values()){
  8.                     if(newOrders.Enter_Number_Value__c == integer.valueOf(numbers.Number__c)){
  9.                         newOrders.Enter_Number_Value__c.addError('Do not enter this value');
  10.                         
  11.                     }
  12.                 }
  13.             }
  14.         }       
  15.     }
  16. }
This was selected as the best answer