I have two objects 'Job Document History' and 'Job Position Transactional'. JPT is the master and JDH is child. In JDh i have field version. It should be unique for each relationship with JPT. I wrote trigger on JDH and trying to validate the duplicate values. Here the problem is Map is Case Sensitive. I want But i am

getting NullPointerException

when i am trying to enter opposite letters(like if 'A' already there but i given 'a').

Below is the my code:

trigger DuplicateVersion on Job_Document_History__c (before insert, before update) {
    Map<String, Job_Document_History__c> versionMap = new Map<String, Job_Document_History__c>();
    Set<string> jobPositionTran = new Set<string>();

    for (Job_Document_History__c jobDocHis : System.Trigger.new) {

        if ((jobDocHis.Version__c != null) && (System.Trigger.isInsert || (jobDocHis.Version__c != System.Trigger.oldMap.get(jobDocHis.Id).Version__c))) 

            if (versionMap.containsKey(jobDocHis.Version__c)) {
                jobDocHis.Version__c.addError('Another new document history has the same version Name.');
            } else {
                versionMap.put(jobDocHis.Version__c.toUpperCase(), jobDocHis);

    for (Job_Document_History__c jobDocHis : [SELECT Version__c FROM Job_Document_History__c WHERE Job_Position_Transactional__c IN :jobPositionTran and Version__c IN :versionMap.KeySet()]) {
        Job_Document_History__c newjobDocHis = versionMap.get(jobDocHis.Version__c.toUpperCase());
        newjobDocHis.Version__c.addError('This version is already exists.');
  • Basic idea would be add to map string in lower case and check that as well. F.e. versionMap.containsKey(jobDocHis.Version__c.toLowerCase())) or versionMap.put(jobDocHis.Version__c.toLowerCase(), jobDocHis);
    – kurunve
    Jan 19 '16 at 14:36
  • Hi Kurunve, I tried this but it's not working. Jan 19 '16 at 14:48
  • So, what do you want to achieve -- make map case insensitive?. And what kind of exception appears?
    – kurunve
    Jan 19 '16 at 14:55
  • 2
    Why don't you just make the field unique case-insensitive instead of reinventing the wheel in apex?
    – Adrian Larson
    Jan 19 '16 at 16:30

You can use toUpperCase() and covert the key to all in upper case.

versionMap.put(jobDocHis.Version__c.toUpperCase(), jobDocHis);
  • i tried this also but still it showing the error message. Error message is "Error: Invalid Data. Review all error messages below to correct your data. Apex trigger HR_DuplicateVersion caused an unexpected exception, contact your administrator: HR_DuplicateVersion: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.HR_DuplicateVersion: line 26, column 1". Line no 26 is "newjobDocHis.Version__c.addError('This version is already exists.');". Jan 19 '16 at 14:50
  • @VijayKumar versionMap.get(jobDocHis.Version__c); -> you need to use here a versionMap.get(jobDocHis.Version__c.toUpperCase());.
    – kurunve
    Jan 19 '16 at 14:59
  • @VijayKumar - that error means the key already exists thus your code is working as you expected. You asked for it to be case insensitive.
    – Eric
    Jan 19 '16 at 15:10
  • 1
    @kurunve - Yup. I read it wrong - need coffee......
    – Eric
    Jan 19 '16 at 15:15
  • 1
    I got the result as expected . Added .toUpperCase() in put() and get() methods. Updated latest code in the question. Jan 20 '16 at 5:04

If you want to bypass the difference between 'A' and 'a' ,try to use containsIgnoreCase('string') method of String.By using this u can make the map key unique in ur case.

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.