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
Sayyad Amjad 7Sayyad Amjad 7 

Need help on this trigger trigger working fine on existing records but when i trying to create new records this error occurs

trigger oppattachment on Opportunity (before insert, before update) {


  Opportunity[] opptys = [SELECT Id, (SELECT Id, Name, ContentType FROM Attachments where  (name like  '%declaration%'))
                          //OR (name ='international declaration.txt'))  
                          FROM Opportunity 
                          where id IN :Trigger.newMap.keySet() AND (Not Billing_Address__c  like '%United Arab Emirates%') ];
  for(Opportunity o : opptys){
            Attachment[] attc = o.Attachments;
            System.debug('attc.size() : ' + attc.size());
            if(Trigger.newMap.get(o.Id).StageName=='Booked' || Trigger.newMap.get(o.Id).StageName=='Booked – Contract Received' )
            {
               if(attc.size()>0)
               {
                System.debug('Need to set Is_declaration_attached__c to true for Opportunity Id: ' + o.id);
                System.debug('just testing this: ' + Trigger.newMap.get(o.Id).Id);
             
                Trigger.newMap.get(o.Id).Is_declaration_attached__c = true;
                Trigger.newMap.get(o.Id).International_Client__c = True;
               // o.
               }
               else
               {
                Trigger.newMap.get(o.Id).addError('You are  International Clint Please provide the Internation Declaration Attachment file before saving this record the file name should be contain declaration');
                Trigger.newMap.get(o.Id).Is_declaration_attached__c = false; 
                Trigger.newMap.get(o.Id).International_Client__c = false;  
               }    
           }            
    }

}

 

when i created new opportunity this error Occurs but trigger working fine on exsiting Opportunity please help me need to solve urgent....

Error:
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger oppattachment caused an unexpected exception, contact your administrator: oppattachment: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.oppattachment: line 7, column 1

Best Answer chosen by Sayyad Amjad 7
Ashish Arun WaghmareAshish Arun Waghmare
Hi Sayyad,

In case of before insert you would not have the id of the opprotunity records, because they are still not created.

Hence the query that you are running below would not work, As you are using Trigger.newMap.keySet()  which will be null , it won't have  any values.

Query is working on existing records because Trigger.newMap.keySet() is not null.

  Opportunity[] opptys = [SELECT Id, (SELECT Id, Name, ContentType FROM Attachments where  (name like  '%declaration%'))
                          //OR (name ='international declaration.txt'))  
                          FROM Opportunity 
                          where id IN :Trigger.newMap.keySet() AND (Not Billing_Address__c  like '%United Arab Emirates%') ];


Hope this helps. !!!
 

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Sayyad,

Greetings to you!

Trigger.newMap: this map is only available in before update, after insert, and after update triggers
 
  • In the trigger, if you try to call trigger.newMap.keySet() on update event or after inserting it will return Id of records which are going to process.
  • In the trigger, if you try to call trigger.newMap.keySet() on before insert then it will return null.

In a "before insert" trigger, the sObjects don't yet have ID values so a Trigger.newMap keyed by the sObject ID can't be made available and Trigger.newMap returns null.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
Ashish Arun WaghmareAshish Arun Waghmare
Hi Sayyad,

In case of before insert you would not have the id of the opprotunity records, because they are still not created.

Hence the query that you are running below would not work, As you are using Trigger.newMap.keySet()  which will be null , it won't have  any values.

Query is working on existing records because Trigger.newMap.keySet() is not null.

  Opportunity[] opptys = [SELECT Id, (SELECT Id, Name, ContentType FROM Attachments where  (name like  '%declaration%'))
                          //OR (name ='international declaration.txt'))  
                          FROM Opportunity 
                          where id IN :Trigger.newMap.keySet() AND (Not Billing_Address__c  like '%United Arab Emirates%') ];


Hope this helps. !!!
 
This was selected as the best answer