You need to sign in to do that
Don't have an account?
Is possible to create a trigger that checks if a Master-Detail Relationship exist, and if it doesn't exist create a new one ?
Hi guys ! Lately im having some seriuos troubles inserting Factura__c records because they have a master-detail relationship with accounts (called Empresa_Cliente__c) and sometimes the account doesnt exist and the insert fail. So i was building a trigger that checks if the account exist and if it dont create a new one with the field Codigo_NAF__c of Factura__c object. The problem is that trigger isnt working, it doesnt fire; do you guys now where maybe the issue ? Is that possible ?
Account has a custome field called Codigo_NAF__c just for info and the trigger is already active; but when i create a new record of type Factura__c and try to enter an Account that doesnt exist the trigger doesnt fire and i just receive the validation error that says: Please insert a valid account (something like that).
This is my trigger:
trigger insertAcc on Factura__c (before insert) {
System.debug(Logginglevel.ERROR , ' ::::::: Empresa Cliente :::::::::::::' + trigger.New[0].Empresa_Cliente__c) ;
List<Account> a = [select name, Codigo_NAF__c from account where Codigo_NAF__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.Codigo_NAF__c = trigger.New[0].Empresa_Cliente__c;
insert (acc);
System.debug(Logginglevel.ERROR , ' ::::::: acc :::::::::::::' + acc) ;
}
}
I hope someone can help me ! Thanks in advance !
There is no problem with ur trigger but u have to keep in mind that If a trigger is ececuted and a account record is inserted but the account is not yet commited to the database with out commiting how can u retrive the account from the database thats why ur getting that error.
Regards,
pradeep.
System.debug(Logginglevel.ERROR , ' ::::::: Empresa Cliente :::::::::::::' + trigger.New[0].Empresa_Cliente__c) ;
I have written this code and its working properly for me but while entering the record we have to enter the account record otherwise it will through validation error as required field missing.
regards,
pradeep.
@kiran_mutturu that exactly the problem the trigger is not even fired so i cant see the debug logs. The problem is that sales force checks if the master relationship exist before commiting the action so trigger is not even fired; i need to bypass the validation or do something more clever haha i hope you guys can help.
Thats what i said the validation will first execute in the order of execution so if u want to overcome that may be it is not possible.
Regards,
pradeep.
Thanks
How I missed that.... lol..
Yeah you can't get it done until you filled some value in the Master field.. If that is the case then there is no point of this trigger. I hope this is a deadlock case...
Thank you for your answer !
De nuevo yo :)
Si el campo es master-detail no puedes dejarlo en blanco, con lo que nunca pasaras esa validacion. Por codigo or por UI debes rellenarlo.
Por UI nunca se lanzara el trigger porque nunca llegaras al trigger, la propia validacion de Salesforce para campos Master - Detail the obliga a poner un valor y que ese valor exista.
Por codigo para crear un registro de tipo factura debes ponerle un Account ( Empresa_Cliente) que exista sino la propia validacion de Salesforce no te deja seguir.
No se lanza ningun trigger en ninguno de los casos ya que no estas cumpliendo los requisitos para llegar a ese trigger.
Master - detail es un tipo de campo : Maestro - detalle , padre- hijo, Un hijo no puede existir sin un padre, y este padre debe existir antes que el hijo.
Si lo que tu quieres es que por codigo se cree un Account si no se cumple el criterio, y que luego esta companñia se le asigne a tu objeto ( un poco lioso pero posible, ten cuidado con los limites ) debes de usar un Lookup field.
Posiblemente algo que tambien te pueda ayudar antes de escribir codigo es mirar como funcionan los Workflows y en caso quizas un workflow trigger.
Aunque si al final tras definir las necesidades de tu negocio/ bussines requirements/ tener todas las validaciones en un mismo sitio.. sigues necesitando un trigger ve a por ello.
Piensa siempre que la plataforma te da muchas cosas antes de que necesites escibir codigo, y aun asi si decides escribir codigo ten en cuenta las guias de Apex y los limites que puedes encontrar.
Espero que te ayude :)
Salu2,
Carolina.