0

I'm recently with apex and I am making a trigger code to autopopulate a primary field for a custom object (this field is mandatory) and I need to run the trigger before insert the record, but when I save my code Apex console shows me this message: "error: variables does not exist for Nombre_Trabajador__r". If you see this field ends with "__r" is a relationship object to put before the field, and here is my complete code:

    trigger CorrelativoRJ on Retenci_n_Judicial__c (before insert) {
Retenci_n_Judicial__c.Name = Nombre_Trabajador__r.Rut_Trabajador__c + Empresa_Trabajador__r.RUT__c + toString(Tipo_Retenci_n_Judicial__c) + toString(CreatedDate);
}

For example, Nombre_Trabajador__r.Rut_Trabajador__c the first part "Nombre_Trabajador__r." is the relationship object, and the next word is "Rut_Trabajador__c" and this is the field of the object.

It suppose that this trigger auto populate the mandatory primery field "Name" for a custom object before insert the record data.

What could be the problem? Thanks for everyone.

2

This Apex contains a number of syntax errors and logical mistakes. While I'm happy to highlight some issues that you can fix, I'd strongly recommend starting with some core Apex Trailhead modules to come to grips with the fundamentals of the language.

trigger CorrelativoRJ on Retenci_n_Judicial__c (before insert) {
    Retenci_n_Judicial__c.Name = Nombre_Trabajador__r.Rut_Trabajador__c + Empresa_Trabajador__r.RUT__c + toString(Tipo_Retenci_n_Judicial__c) + toString(CreatedDate);
}

Retenci_n_Judicial__c.Name does not refer to a data field (it's actually a schema reference here). You must loop over trigger objects in Trigger.new to modify data, and access properties of the records in trigger context on the object instances you obtain from Trigger.new and other context variables.

The Apex Triggers module provides an introduction, and it's critical to pay attention to the Bulk Apex Trigger patterns.

Nombre_Trabajador__r.Rut_Trabajador__c would be a property of a single Retenci_n_Judicial__c record, but you cannot reference it standalone. Additionally, you must query relationship data in a trigger context - it is not populated for you.

Note that this is going to be just a bit trickier than usual here because you are in before insert context, so your records do not have Ids. You'd have to accumulate Ids of related records, write queries against the related objects into a Map, iterate over your trigger records, use the Maps to obtain related records, and finally update the data. The other alternative is to move to after insert context and query the trigger records directly, with related records, but incur an extra DML to perform the update.

toString() is not a function - it is a method you must call on a value, such as myRecord.CreatedDate.toString().

1
  • Hi David, thanks for your answer. I will learn apex with more details and I hope get better use of this programming language. Aug 26 '20 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.