+ Start a Discussion
Cesar Ramirez Vasquez005391619375684564Cesar Ramirez Vasquez005391619375684564 

Im trying to create a trigger before insert; but the trigger is not firing? Any ideas?

Hi everyone ! This is my scenario; i have an object called Facturas__c, when i insert a record in that object it has a lookup to Account (lookup to my custom field Codigo_NAF__C); so my trigger basically search if the Account exist and if the account does not exist, it create a new one so the record can be inserted without any issues.

trigger insertAcc on Factura__c (before insert) {

System.debug(Logginglevel.ERROR , ' ::::::: Empresa Cliente :::::::::::::' + trigger.New[0].Empresa_Cliente__r.Codigo_NAF__c) ; 
List<Account> a = [select name, Codigo_NAF__c from account where Codigo_NAF__c = :trigger.New[0].Empresa_Cliente__r.Codigo_NAF__c];
System.debug(Logginglevel.ERROR , ' ::::::: List :::::::::::::' + a) ; 
if (a == null){
Account acc = new Account();
acc.Name = trigger.New[0].Empresa_Cliente__c;
acc.Codigo_NAF__c = trigger.New[0].Empresa_Cliente__c;
insert (acc);

trigger.New[0].Empresa_Cliente__c = acc.id;

System.debug(Logginglevel.ERROR , ' ::::::: acc :::::::::::::' + acc) ;


Any help what i am missing would be great ! Thanks for your time !
pradeep naredlapradeep naredla
Hi ceser,
        Are u getting any errors if yes post the error.
       And why you written this code could u explain   trigger.New[0].Empresa_Cliente__c = acc.id;
Hi , 

I guess you might not be getting any value in the debug for trigger.New[0].Empresa_Cliente__r.Codigo_NAF__c.
You cannot traverse through relationships in trigger context variable.
Also if the custom object has a relationship with account using custom field Codigo_NAF__c, shouldn't the where condition for query be something like this 
where Codigo_NAF__c =trigger.New[0].Codigo_NAF__c.
If you want to get the account value from some other object then you will have to fire a query on that object.

Hope this helps.


Mikola SenykMikola Senyk
Hi Cesar,

Explain type of the following fields:
  • Account.Codigo_NAF__c - ?
  • Factura__c.Empresa_Cliente__c - ?

It helps to understand the purpose of your trigger.
Cesar Ramirez Vasquez005391619375684564Cesar Ramirez Vasquez005391619375684564
Hi evryone thanks for your answers @pradeep naredla: no im not getting any errors the trigger is active but is no firing, i cant even see the debug logs. And trigger.New[0].Empresa_Cliente__c = acc.id i forgot to erase that.
@Vishal Shinde: trigger.New[0].Empresa_Cliente__r.Codigo_NAF__c should be trigger.New[0].Empresa_Cliente__c my bad.
@Mikola Senyk Account.Codigo_NAF__c is a custom field in Account object and Factura__c.Empresa_Cliente__c is a master relationship to account (specifically to that custom field Codigo_NAF__c).

Cesar Ramirez Vasquez005391619375684564Cesar Ramirez Vasquez005391619375684564
What i actually want i not sure if its possible is to check before insert if the master relationship (specifically Codigo_NAF__c exist) and if it dont create a new one with the value i am inserting in the field Factura__c.Empresa_Cliente__c; that way every record that i insert in Factura__c would be inserted without any issues. 
Mikola SenykMikola Senyk
Hi Cesar,

Yes, it is possible to check Codigo_NAF__c in related to Factura__c master account.
You can use the following code:
// get ID of master Accounts
Set<Id> accIds = new Set<Id>();
for (Factura__c factura: Trigger.new) {

// find master Account with Codigo_NAF__c field
Map<Id,Account> accMap = new Map<Id,Account>([
    SELECT Name, Codigo_NAF__c FROM Account WHERE Id IN :accIds

// go through new records
for (Factura__c factura: Trigger.new) {
    Account a = accMap.get(factura.Empresa_Cliente__c);
    if ( a.Codigo_NAF__c == null ) { // I DO NOT KNOW WHAT CONDITION DO YOU TRY TO CHECK
        // TODO put logic to create new Account here
I don't understand what do you try to check with Account.Codigo_NAF__c. But in the code above a.Codigo_NAF__c holds actual value, so, you can change it accordingly.