-3

Trigger on document Object. Whenever I update document its populating corresponding platform name on document level.

 trigger s360_DocumentSOSLTrigger on Document__c (before insert,before update) {
if((trigger.isInsert || trigger.isUpdate) && trigger.isBefore){
    S360_DocumentDetails.documentupdate(trigger.new);
}}

  public class S360_DocumentDetails {
public static void documentupdate(List<Document__c> docList){
     set<id> Platformset=new set<id>();
    for(Document__c doc:docList){
        if(doc.Platform__c!=null){
             Platformset.add(doc.Platform__c);
        }

    }
    map<id,Platform__c> paltformmap=new map<id,Platform__c>();
    for(Platform__c plat:[select id,Aircraft_Manufacturer__c,name from Platform__c where Id in:Platformset]){
        paltformmap.put(plat.id,plat);
    }

    for(Document__c D2:docList){
     if(paltformmap.get(D2.Platform__c).name!=null && 
      plaformmap.containskey(D2.Platform__c)){
           D2.PlatformSOSL__c=paltformmap.get(D2.Platform__c).name;
        }else
        {  
            D2.PlatformSOSL__c='';
        }

    }
    }}

Issue facing:

I have executed following code in Anonymous window. before that what I did I just updated "platform" in document level. For perticular that platform I got the error:

Script I have executed in Anonymous window:

  List<Document__c> docList=[select 
 Id,Platform__c,Platform__r.name,Revision__c,Send_Revision_Email__c from 
     Document__c];
  List<Document__c> doclist1=new List<Document__c>();
 for(Document__c doc:docList){
  doc.Send_Revision_Email__c=true;
  doclist1.add(doc);
}
update doclist1;

Error Image:

enter image description here

7
  • 1
    You would need to add a null check for D2.Platform__c AND/OR paltformmap.containsKey(D2.Platform__c) – Raul May 4 '18 at 9:38
  • I have done null check and even all these condition if(paltformmap.get(D2.Platform__c).name!=null && plaformmap.containskey(D2.Platform__c)).. still I am getting same issue – SFDC Learner May 4 '18 at 9:40
  • @AnnappaPH did you add check of D2.Platform__c != null – Tushar Sharma May 4 '18 at 10:27
  • yes.Please check my updated code – SFDC Learner May 4 '18 at 10:31
  • @AnnappaPH In your second for loop add check of add check of D2.Platform__c != null – Tushar Sharma May 4 '18 at 10:33
0

In your second for loop add check of add check ofD2.Platform__c != null as you are checking null for this in first for loop so chances are it is coming null and you are getting this exception.

0
1

You need to check if the map contains the key before accessing its data. Use the below code.

public class S360_DocumentDetails {
public static void documentupdate(List<Document__c> docList){
     set<id> Platformset=new set<id>();
    for(Document__c doc:docList){
        if(doc.Platform__c!=null){
             Platformset.add(doc.Platform__c);
        }

    }
    map<id,Platform__c> paltformmap=new map<id,Platform__c>();
    for(Platform__c plat:[select id,Aircraft_Manufacturer__c,name from Platform__c where Id in:Platformset]){
        paltformmap.put(plat.id,plat);
    }

    for(Document__c D2:docList){
        if (paltformmap.containsKey(D2.Platform__c)) {
            D2.PlatformSOSL__c=paltformmap.get(D2.Platform__c).name;
            D2.AircraftManufacturerSOSL__c=paltformmap.get(D2.Platform__c).Aircraft_Manufacturer__c;
        }
    }
}

Here you can see I have used the Map.containsKey(Id) to check if the map contains this key or not. By doing this we won't face null exception.

1
  • THANKS. thats the same thing I have mentioned in comment. Please check my comment – SFDC Learner May 4 '18 at 10:21

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.