+ Start a Discussion
Cesar Ramirez Vasquez005391619375684564Cesar Ramirez Vasquez005391619375684564 

Trigger not firing before insert ?? HELP

Hi i have created a trigger that checks before inserting a record of type Factura__c, if the master-relationship field (Empresa_Cliente__c) exist, and if it dont the trigger create a new one with the value contained in the field Empresa_Cliente__c.
The master relationship is between Factura__c (custom object) and Account. But when i try to save a record the trigger doesnt fire and i keep receiving the salesforce validation error (Error: No matches found.) in that field. Any suggestions this is my trigger :

trigger insertAcc on Factura__c (before insert) {
System.debug(Logginglevel.ERROR , ' ::::::: Empresa Cliente :::::::::::::' + trigger.New[0].Empresa_Cliente__c) ;
if(Trigger.isBefore)
  {
     if(Trigger.isInsert)
      {
  
List<Account> a = [select name, CodigoNAF__c from account where CodigoNAF__c = :trigger.New[0].Empresa_Cliente__c];
System.debug(Logginglevel.ERROR , ' ::::::: List :::::::::::::' + a) ;
if (a == null){
Account acc = new Account();
acc.Name = trigger.New[0].Empresa_Cliente__c;
acc.CodigoNAF__c = trigger.New[0].Empresa_Cliente__c;
insert (acc);

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

}
}
}

The trigger is not firing when i try to save a record it just keep throwing me an error in the master-relationship field (Error:Error: No matches found) . Remember the value i put in the field doesnt exist that is the point of the trigger, create a new Account with the value provided in Empresa_Cliente__c and then save the new Factura__c record without errors.

Ravi NarayananRavi Narayanan

//if(if (a == null)  \
use  

if(a.size()==0

you are having master detail relationship between account and factura. but while saving a new record, are you specifying the masterfield correctly? you cannot save a child record without specifying values in master field. 

 

 

Cesar Ramirez Vasquez005391619375684564Cesar Ramirez Vasquez005391619375684564
Hi @Ravi Narayanan, yes i have a master-detail relationship in object Factura__c that links to Account. I am specifying the value every time that is what i want to accomplish create a new Account record if the value does not exist. The problem is that salesforce checks if the value exist before commiting the action so the trigger does not fire. Any suggestions or work arounds: i need the master-detail relationship beacause i have tons of reports using roll up summary in Account object.
Ravi NarayananRavi Narayanan
The field "Empresa_Cliente__c" is a master detail relationship field and will contain the ID of related master   account record.

i hope the query should be like below if  CodigoNAF__c is not a ID field. 

List<Account> a = [select name, CodigoNAF__c from account where id = :trigger.New[0].Empresa_Cliente__c];
Cesar Ramirez Vasquez005391619375684564Cesar Ramirez Vasquez005391619375684564
Codigo_NAF__c is a custom field in Account object not an id. But the main problem is that the trigger is not firing. When i hit save salesforce validation pops out with an error
Ravi NarayananRavi Narayanan

:trigger.New[0].Empresa_Cliente__c  ---- this field will have ID  of relatedAccount.

-- you are checking if this field in account equals :trigger.New[0].Empresa_Cliente__c .  

What is the value the field CodigoNAF__ will contain?????