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
SId.New devSId.New dev 

Trigger error

 

trigger TriggerOnLead on Lead (before insert) {
 
    Map<String, Lead> mapLead1 = new Map<String, Lead>();
    SYSTEM.DEBUG('!!!!!!!!!!mapLead1!!!!!!!!!!!'+mapLead1);
    Map<String, Lead> mapLead2 = new Map<String, Lead>();
    SYSTEM.DEBUG('+++++++++++mapLead2 +++++++++++++++'+mapLead2 );

    for (Lead lead : System.Trigger.new) {
          mapLead1.put(lead.concatinate_name_and_email__c.TOLOWERCASE().TRIM(), lead);
          mapLead2.put(lead.concatinate_name_and_phone__c.TOLOWERCASE().TRIM(), lead);
    }
     list<lead> lead1=[SELECT Id,Name,Email,Phone,concatinate_name_and_email__c FROM Lead WHERE concatinate_name_and_email__c  IN :mapLead1.KeySet()];
   
    list<lead> lead2=[SELECT Id,Name,Email,Phone,concatinate_name_and_phone__c FROM Lead WHERE concatinate_name_and_phone__c IN :mapLead2.KeySet()];
    for (Lead lead4 : System.Trigger.new) {
      if(lead4.Any_way_save_record__c!=true)
      {
  
    if(lead1.size()>0)               /*at this line error is coming. */
    {
     for (Lead lead :lead1){
      Lead newLead = mapLead1.get(lead.concatinate_name_and_email__c.TOLOWERCASE().TRIM());
       SYSTEM.DEBUG('*************mapLead1************************'+mapLead1);
         SYSTEM.DEBUG('@@@@@@@@@newLead@@@@@@@@@@@@@@@'+newLead);
        newLead.addError('already exists<a style=color:GREEN href=/apex/leaderror?id='+LEAD.ID+'>'+  lead.name + '</a>');
    }}else if(lead2.size()>0){
    for (Lead lead :lead2 ) {
      Lead newLead1 = mapLead2.get(lead.concatinate_name_and_phone__c.TOLOWERCASE().TRIM());
      SYSTEM.DEBUG('%%%%%%%%%%%%newLead1%%%%%%%%%%%%%%%%%%'+newLead1);
      
        
       newLead1.addError('already existse<a style=color:GREEN href=/apex/leaderror?id='+LEAD.ID+'>'+  lead.name + '</a>');

  
    }
    
   }
}
}
}

 TR: Sandbox: Developer script exception : TriggerOnLead : TriggerOnLead: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.TriggerOnLead: line 22, column 1

Best Answer chosen by Admin (Salesforce Developers) 
ForcepowerForcepower

It is blowing up on your lead.concatinate_name_and_email__c field being nul:

 

Try this instead:

 

      if (lead.concatinate_name_and_email__c != null) {    
          Lead newLead = mapLead1.get(lead.concatinate_name_and_email__c.TOLOWERCASE().TRIM());
           SYSTEM.DEBUG('*************mapLead1************************'+mapLead1);
             SYSTEM.DEBUG('@@@@@@@@@newLead@@@@@@@@@@@@@@@'+newLead);
            newLead.addError('already exists<a style=color:GREEN href=/apex/leaderror?id='+LEAD.ID+'>'+  lead.name + '</a>');
      }

Best,

Ram

All Answers

ForcepowerForcepower

It is blowing up on your lead.concatinate_name_and_email__c field being nul:

 

Try this instead:

 

      if (lead.concatinate_name_and_email__c != null) {    
          Lead newLead = mapLead1.get(lead.concatinate_name_and_email__c.TOLOWERCASE().TRIM());
           SYSTEM.DEBUG('*************mapLead1************************'+mapLead1);
             SYSTEM.DEBUG('@@@@@@@@@newLead@@@@@@@@@@@@@@@'+newLead);
            newLead.addError('already exists<a style=color:GREEN href=/apex/leaderror?id='+LEAD.ID+'>'+  lead.name + '</a>');
      }

Best,

Ram

This was selected as the best answer
SId.New devSId.New dev

Hi. Thanks for your reply. Had you worked on Contact share.please see why it is not working

 

public class Contacterrorclass {

  

public PageReference AssignPermissionforcontact() {
        return null;
    }

public list<user> u{get;set;}
id contactid;
public Contacterrorclass()
{
 contactid=ApexPages.CurrentPage().getParameters().get('id');
 }
   public PageReference AssignPermissionforcontacts() {
       
 
 
 Contactshare cs = new ContactShare(ContactAccessLevel='edit', ContactId=contactId, userOrGroupId=UserInfo.getUserId());

 system.debug('*****************************'+contactId);
  system.debug('____________________________'+cs);
 
 
 pagereference p1=new pagereference('/'+contactid);

return p1;

 
}
}

 in sufficient previlages error is coming.please try to solve this,

for detail:--

 

http://boards.developerforce.com/t5/Apex-Code-Development/ContactShare-using-apex/td-p/611543

ForcepowerForcepower
Sld.New,

Can you post the line on which you get the permissions error? Are you running this as a standard user or sys admin?

Ram