I have a simple trigger that updates lead address filed from another custom object. When I am trying to add lead id in set variable, the following error is thrown:

argument cannot be null exception

Snippet code:

Trigger trigger_name on lead (before insert){
    Set<id> setid = new set<id>();
    For(lead of : trigger.new )
  • 3
    Ids are not available in records before insert in trigger yet
    – kurunve
    Feb 22 '17 at 16:24
  • 1
    Please edit your question and provide valid code. This code does not compile, so we can't possibly help you.
    – sfdcfox
    Feb 22 '17 at 16:34

As @Samir said, the id field is not available in a before insert trigger because Id's are available after insert.

Also, there's a problem with your code. That's not how you instantiate a Set of Id's.

Here's the right code:

   Trigger yourTriggerName on lead (after insert){
       Set<Id> leadSet = new Set<Id>();
       for(Lead lead : Trigger.new)
  • missed the set instantiation.My Bad!!
    – Samir
    Feb 22 '17 at 16:38
  • No problem. There were so many problems in the code... Feb 22 '17 at 16:40

In before insert trigger,id is not available as the record is not yet inserted,To get the id, change your trigger definition to -

Trigger triggerName on lead (after insert)
  • 2
    Valid point, but that would not cause a NullPointerException (NPE).
    – sfdcfox
    Feb 22 '17 at 16:35
  • error can be because of the trigger name i guess. There should be no space in triggerName
    – Samir
    Feb 22 '17 at 16:36
  • Still iam getting the same error.....iam trying add text filed in to set(string)where external id is enabled on that filed.....
    – user42189
    Feb 22 '17 at 18:06
  • Please upload your full trigger
    – Samir
    Feb 22 '17 at 18:27

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.