+ Start a Discussion
br.gabrielbr.gabriel 

Help on trigger: Error: execution of AfterUpdate caused by: System.NullPointerException

Hi guys I need help to solve this error:
InsertMilestonesDeploymentProject: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.InsertMilestonesDeploymentProject: line 31, column 1

trigger InsertMilestonesDeploymentProject on Projeto_Implantacao__c (after insert, after update) {
	List<Etapas_Projeto_de_implantacao__c> mList = new List<Etapas_Projeto_de_implantacao__c>();	
	Etapas_Projeto_de_implantacao__c milestone1;
	Etapas_Projeto_de_implantacao__c milestone2;
	Etapas_Projeto_de_implantacao__c milestone3;
	Etapas_Projeto_de_implantacao__c milestone4;
	Etapas_Projeto_de_implantacao__c milestone5;
	Etapas_Projeto_de_implantacao__c milestone6;
	Etapas_Projeto_de_implantacao__c milestone7;
	Etapas_Projeto_de_implantacao__c milestone8;
	
	//Set of accounts
	Set<Id> accountIds =  new Set<Id>();
	
	for(Projeto_Implantacao__c p:Trigger.new){
		accountIds.add(p.Implantacao__c);//Implantacao__c is a lookup field
	}
	//Map of accounts
	Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Id,Name,Unidade_de_atendimento__c FROM Account WHERE Id IN :accountIds]);
	
	
	
	if(Trigger.isAfter) {
    if(Trigger.isUpdate) {
       for(Projeto_Implantacao__c p: Trigger.new) {
       	for(Projeto_Implantacao__c old: Trigger.old) {
           
             milestone1 = new Etapas_Projeto_de_implantacao__c();
             milestone1.Name = '01 - Contato inicial';
             //restore info account master-detail Account>>Projeto_Implantacao__c>>Implantacao
             if(accountMap.get(p.Implantacao__r.Id).Unidade_de_atendimento__c == 'Campo Grande')//The problem It´s here
             	milestone1.Responsavel__c = '005G0000003oWY2';
             milestone1.Data_inicial_prevista__c = Date.today();
             milestone1.Data_final_prevista__c = Date.today().addDays(3);
             milestone1.Projeto_de_Implantacao__c = p.Id;
             mList.add(milestone1);
             
             milestone2 = new Etapas_Projeto_de_implantacao__c();
             milestone2.Name = '02 - Levantamento/Coleta de dados';
             milestone2.Data_inicial_prevista__c = Date.today().addDays(4);
             milestone2.Data_final_prevista__c = Date.today().addDays(10);
             milestone2.Projeto_de_Implantacao__c = p.Id;
             mList.add(milestone2);
             
             milestone3 = new Etapas_Projeto_de_implantacao__c();
             milestone3.Name = '03 - Conversão de dados';
             milestone3.Data_inicial_prevista__c = Date.today().addDays(11);
             milestone3.Data_final_prevista__c = Date.today().addDays(20);
             milestone3.Projeto_de_Implantacao__c = p.Id;
             mList.add(milestone3);
             
             milestone4 = new Etapas_Projeto_de_implantacao__c();
             milestone4.Name = '04 - Capacitação';
             milestone4.Data_inicial_prevista__c = Date.today().addDays(21);
             milestone4.Data_final_prevista__c = Date.today().addDays(30);
             milestone4.Projeto_de_Implantacao__c = p.Id;
             mList.add(milestone4);
             
             milestone5 = new Etapas_Projeto_de_implantacao__c();
             milestone5.Name = '05 - Instalação/Acompanhamento';
             milestone5.Data_inicial_prevista__c = Date.today().addDays(31);
             milestone5.Data_final_prevista__c = Date.today().addDays(44);
             milestone5.Projeto_de_Implantacao__c = p.Id;
             mList.add(milestone5);
             
             milestone6 = new Etapas_Projeto_de_implantacao__c();
             milestone6.Name = '06 - Ativação financeira';
             milestone6.Data_inicial_prevista__c = Date.today().addDays(45);
             milestone6.Data_final_prevista__c = Date.today().addDays(45);
             milestone6.Projeto_de_Implantacao__c = p.Id;
             mList.add(milestone6);
             
             milestone7 = new Etapas_Projeto_de_implantacao__c();
             milestone7.Name = '07 - Ativação do Service Desk';
             milestone7.Data_inicial_prevista__c = Date.today().addDays(49);
             milestone7.Data_final_prevista__c = Date.today().addDays(50);
             milestone7.Projeto_de_Implantacao__c = p.Id;
             mList.add(milestone7);                
             
             milestone8 = new Etapas_Projeto_de_implantacao__c();
             milestone8.Name = '08 - Encerramento da implantação';
             milestone8.Data_inicial_prevista__c = Date.today().addDays(46);
             milestone8.Data_final_prevista__c = Date.today().addDays(50);
             milestone8.Projeto_de_Implantacao__c = p.Id;
             mList.add(milestone8);                 
                        
           
       	}   
       }       
    }
    if(!mList.IsEmpty())
    { 
		insert milestone1;
		insert milestone2;
		insert milestone3;
		insert milestone4;
		insert milestone5;
		insert milestone6;
		insert milestone7;
		insert milestone8;
    }
      
  }
}


Best Answer chosen by br.gabriel
Mikola SenykMikola Senyk
Hi,
Change line with error for the following code:
if ( accountMap.containsKey(p.Implantacao__c) && accountMap.get(p.Implantacao__c).Unidade_de_atendimento__c == 'Campo Grande' )

All Answers

kevin Carotherskevin Carothers
Sorry to ask dumb questions, but is the trigger active?  And, is Implantacao__c required on entry?

Maybe you shoud try to query out Implantacao__c when you build accountMap;

//Map of accounts
Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Id,Name,
   Unidade_de_atendimento__c, Implanticao__c, Implantacao__r.Id FROM Account WHERE Id IN :accountIds]);

Just for future reference, not related to your problem, but I don't think lookup fields are populated on the before insert trigger context.
Sfdc CloudSfdc Cloud

Hi,

As error seems due to Implantacao__c object you need to do two minor changes in your code.
Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Id,Name,
   Unidade_de_atendimento__c, Implanticao__c, Implantacao__r.Id  FROM Account WHERE Id IN :accountIds]);

Hope this will helps :)

 

Vi$hVi$h
On line number 31 :
if(accountMap.get(p.Implantacao__r.Id).Unidade_de_atendimento__c == 'Campo Grande')//The problem It´s here

You dont get values through relationships in trigger context variables i.e p.Implantacao__r.id will not return any value, instead use p.Implantacao.
The look ups anyway store id so you dont need to traverse through it for id.
Also before you use fields from accountMap you could check for null condition.

if(accountMap.get(p.Implantacao)!=null){
      if(accountMap.get(p.Implantacao).Unidade_de_atendimento__c == 'Campo Grande'){
     // Your logic here..
}
}

Hope this helps.

Thanks,
Vishal Shinde
Mikola SenykMikola Senyk
Hi,
Change line with error for the following code:
if ( accountMap.containsKey(p.Implantacao__c) && accountMap.get(p.Implantacao__c).Unidade_de_atendimento__c == 'Campo Grande' )

This was selected as the best answer
br.gabrielbr.gabriel

Bether exception message and done.

InsertMilestonesDeploymentProject: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: 005G0000003oWY2: []: Trigger.InsertMilestonesDeploymentProject: line 96, column 1

Thanks for all the feedback everyone. And Mikola Senyk your suggestion helped me solve my question.