You need to sign in to do that
Don't have an account?
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 !
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 !
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;
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.
Thanks
Explain type of the following fields:
It helps to understand the purpose of your trigger.
@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).
Yes, it is possible to check Codigo_NAF__c in related to Factura__c master account.
You can use the following code:
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.