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
suneel.patchipulusu@gmail.comsuneel.patchipulusu@gmail.com 

How to check the existing field value in an custom Object?

Hello All

 

Scenario: I have an Invoicenumber field (its an text data type).So, every user enters his data and save the record. After saving the record it will display the red or green flag

 

red-- indicates invoicenumber already exists,

Green--- indicates not exists the invoicenumber so no problem.

 

I dont want to use Unique check box. Through trigger I like to check whether this Invoicenumber already exists in custom Object or not

 

Could any one have any idea???

Please share ur suggestions?

 

Thankyou

Best Answer chosen by Admin (Salesforce Developers) 
suneel.patchipulusu@gmail.comsuneel.patchipulusu@gmail.com

 for(Claim__c s : Trigger.new) {
                        s.Unique__c=s.Claim_Invoice_Number__c+s.Claim_Invoice_Position__c
                        +s.Claim_Document_Line_Item_Number__c+s.Claim_Line_Item__c;
                        
                 }  
                 
                 Map<String, Claim__c> sclmap = new Map<String, Claim__c>();
                 for (Claim__c scl : System.Trigger.new){
                      //////////////////////////////////////////////////
                     //Make sure we don't treat an Unique__c that isn't
                     //changing during an update as a duplicate
                     //////////////////////////////////////////////////
                     if( (System.Trigger.isInsert ||
                              (scl.Unique__c != System.Trigger.oldMap.get(scl.id).Unique__c)))
                       {
                              if (sclMap.containsKey(scl.Unique__c))
                                {
                                  scl.Unique__c.addError('already Invoice is present');                                    
                                }     
                     else{
                          sclMap.put(scl.Unique__c, scl);
                         }
                       }
                    }
                  //////////////////////////////////////////////////////////////////////////
                  //Using a single database query, find all Invoice nos in the claim table
                  //that have the same number as any of other claims being inserted or updated
                  ////////////////////////////////////////////////////////////////////////////
                  for(Claim__c c : [select Unique__c from Claim__c
                                          where Unique__c IN : sclMap.KeySet()] )
                      {
                          Claim__c newscl = sclMap.get(c.Unique__c);
                          newscl.Unique__c.addError('already Invoice is present');
                      }                    
                                 
 I tried like this but the above mentioned code finds the duplicate Invoice number but I need to show this error after saving the record, here from the above code I am not able to save it throws an error 

 

Please give some suggestiions on this??

                          

All Answers

suneel.patchipulusu@gmail.comsuneel.patchipulusu@gmail.com

How it display red or green??

 

It displays through if Invoicenumber is null after update it shows red.otherwise green

ItswasItswas

HI Suneel,

 

Can you Please elaborate your requirment .As per my knowledge u cannot set a flag green/red thrugh trigger.
But if you are using some satndard VF page which will override then u can set the forecolor (Invoice Number Field) to red or green after the save action of that record.

 

Please let me know the requirment and post it if the above solution is fine for you.

 

 

Regards,
Itsaws.

suneel.patchipulusu@gmail.comsuneel.patchipulusu@gmail.com

Hello

 

For seeting red and green flags

 

IF (ISBLANK(Unique__c),
IMAGE("/img/samples/flag_red.gif","Red", 20, 20),
IMAGE("/img/samples/flag_green.gif","Green", 20, 20)
)

 

But the ISBLANK is not current in my situation any way the user entering the Invoice number and after clicking the sava button it has to show whether the Invoice number is already exists in the object or not.

 

 

For this I am using two custom fields

 

1. Invoicenumber__c(user enters in to this)

2.after clicking save

3.Unique__c (this field displays red or green flag)

 

Both the custom fields availble in same object.

 

I tried with seeeting vlookup but doesnt help

 

Is it possible to do in Force.com

 

 Thanks in advance

 

 

suneel.patchipulusu@gmail.comsuneel.patchipulusu@gmail.com

 for(Claim__c s : Trigger.new) {
                        s.Unique__c=s.Claim_Invoice_Number__c+s.Claim_Invoice_Position__c
                        +s.Claim_Document_Line_Item_Number__c+s.Claim_Line_Item__c;
                        
                 }  
                 
                 Map<String, Claim__c> sclmap = new Map<String, Claim__c>();
                 for (Claim__c scl : System.Trigger.new){
                      //////////////////////////////////////////////////
                     //Make sure we don't treat an Unique__c that isn't
                     //changing during an update as a duplicate
                     //////////////////////////////////////////////////
                     if( (System.Trigger.isInsert ||
                              (scl.Unique__c != System.Trigger.oldMap.get(scl.id).Unique__c)))
                       {
                              if (sclMap.containsKey(scl.Unique__c))
                                {
                                  scl.Unique__c.addError('already Invoice is present');                                    
                                }     
                     else{
                          sclMap.put(scl.Unique__c, scl);
                         }
                       }
                    }
                  //////////////////////////////////////////////////////////////////////////
                  //Using a single database query, find all Invoice nos in the claim table
                  //that have the same number as any of other claims being inserted or updated
                  ////////////////////////////////////////////////////////////////////////////
                  for(Claim__c c : [select Unique__c from Claim__c
                                          where Unique__c IN : sclMap.KeySet()] )
                      {
                          Claim__c newscl = sclMap.get(c.Unique__c);
                          newscl.Unique__c.addError('already Invoice is present');
                      }                    
                                 
 I tried like this but the above mentioned code finds the duplicate Invoice number but I need to show this error after saving the record, here from the above code I am not able to save it throws an error 

 

Please give some suggestiions on this??

                          

This was selected as the best answer
suneel.patchipulusu@gmail.comsuneel.patchipulusu@gmail.com

Hey guys

 

I got the answer