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
RAM RRAM R 

How to get the values from the map , please suggest me?

HI , i am facing de-reference null object please suggest me the solution and find below my code..

trigger zipcodeupdateTrigger on Contact (after insert, after update) {
    set<string> zipcodeName = new set<string>();
    set<string> firmchannelName = new set<string>();
    Map<Id, Zipcode__c> zipCodeMap = new Map<Id, Zipcode__c>();
    List<Contact> updateList = new List<Contact>();
    
    //Map<Id, Address__C> addressMap = new Map<Id, Address__C>();
    List<Address__C> addressList = new List<Address__C>([select id, Name, contact_Level__C, zipcode__c from Address__c where type__C = 'physical']);
    for(Address__c a:addressList){
        //addressMap.put(a.zipcode__C, a);
        firmchannelName.add(a.contact_level__c);
        zipcodeName.add(a.Zipcode__c);
    }
    List<Zipcode__C> zipList = new List<zipcode__c>([select id, Name from Zipcode__c where Name IN :zipcodeName]);
    for(Zipcode__c z: zipList){
        zipCodeMap.put(z.Id, z);
    }
    for(Contact c:trigger.New){
        Zipcode__C zp = zipcodeMap.get(c.zipcode__c);------------------->i am not able to get the value?
        if((c.Level__c != null) && (c.Level__c != trigger.oldMap.get(c.Id).Level__C)){
            system.debug('********* : '+ zp.Name);
            c.zipcode__c = zp.Name;
            updateList.add(c);
            system.debug('********* : '+ zp.Name);
        }
    }
    Update updateList;
}
Best Answer chosen by RAM R
pconpcon
What is in your Contact.Zipcode__c field?  Is it the value "27606" or is it an Id that looks up to your Zipcode__c object?  Your trigger does not make sense since you get the zipcode from the Map but then save the right back to the same field.  Is there another field like Zipcode_Lookup__c that you are trying to store this in?  If that's what you are doing, take a look at the code below that will do this for you.  Also, if you do this in a before trigger then you do not need to do the update of the contact.
 
trigger zipcodeupdateTrigger on Contact (before insert, before update) {
    Set<String> zipcodeName = new Set<String>();
    Map<Id, Zipcode__c> zipCodeMap = new Map<Id, Zipcode__c>();

    for (Address__c address : [
        select Name,
            Contact_Level__c,
            Zipcode__c
        from Address__c
        where Type__c = 'physical'
    ]) {
        zipcodeName.add(address.Zipcode__c);
    }

    zipcodeName.remove(null);

    for (Zipcode__c zip : [
        select Name
        from Zipcode__c
        where Name in :zipcodeName
    ]) {
        zipCodeMap.put(z.Name, z);
    }

    for (Contact c : Trigger.new) {
        if (!zipCodeMap.containsKey(c.Zipcode__c)) {
            continue;
        }

        if (
            c.Level__c != null &&
            c.Level__c != trigger.oldMap.get(c.Id).Level__c
        ) {
            c.Zipcode_Lookup__c = zipCodeMap.get(c.Zipcode__c).Id;
        }
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors