0

I'm new with triggers and am having trouble with making one work.

I have a child custom object called "Actividad" which has a formula field "Actor Principal Indicador" that gets updated from its' parent object "Indicador". "Actividad" is also parent for the object "Entidad participante".

What I want my trigger to do is to update a field ("Actor Principal Indicador" - yes, it has the same name as the field in the other object) in "Entidad participante" getting the information from its parent's field "Actor Principal Indicador" (which originally gets its information from the "Indicador" objetc)

Is this possible?

Any help would be appreciated!

This is what I have so far:

trigger ActualizarActorPrincipal on Actividad__c (after update, after insert) {    
    Set<ID> IDActividad = new Set<ID>();
    for(Actividad__c A: Trigger.new) {
        List<Actividad__c> ActividadActualizada = 
            [SELECT Id, Actor_principal_indicador__c, (Select Id, Actor_principal_indicador__c from Organizaciones_participantes__r)  
             FROM Actividad__c 
             WHERE Id in :IDActividad];
        List<Entidad_participante__c> EntParticipActualizar = 
            new List<Entidad_participante__c>();
        for (Actividad__c AA: ActividadActualizada){
            for(Entidad_participante__c EP : AA.Organizaciones_participantes__r) {
                if(EP.Actor_principal_indicador__c == null)
                    EP.Actor_principal_indicador__c = AA.Actor_principal_indicador__c;
                EntParticipActualizar.add(EP);
            }
            update EntParticipActualizar;
        }
    }
}
  • 1
    Two observations: (1) How is IDActividad populated? Right now, it's empty, but you're using it in the SELECT, and (2) You should define EntParticipActualizar at the top, outside the for loops, and move the update out after the for loops. Using DML statements such as update within a loop is not best practice. – metadaddy Aug 29 '13 at 21:05
1

:)

I think what you would like to do is update the field in the "Entidad Participante" object.

To do that, you would need something like this:

A trigger in your "Indicador" object that will update the field in the "Entidad Participante" object. Because you can't use a formula field on "Entidad Participante" that will reflect the value from the other formula field in "Actividad" (master) object when this parent changes and obviously it changes and this change occurs when Indicator dictates.

Therefore each time that the users create -update a "Indicator" object the formula field in "Actividad" will change and the same way you would like that a field ( non formula field ) changes in "Participate" .

Here is where your trigger should be : in "Indicator" ( Trigger won't run in a formula change)

In the moment that a record(s) "Indicator" is created-updated the trigger linked will search in database for that "Actividad" record(s) where the relationship contains the "Indicator" ( current record(s) ) id(s).--> here you will have all the "Actividad" records linkd to the "Indicator" record(s) in changing process.

With "Actividad" Id(s) you will search again and will get all the "Entidad Participante" record(s) related to the "Actividades" --> NOW YOU HAVE ALL THE "Entidad Participante" RECORDS THAT YOU WOULD LIKE TO UPDATE.

SCHEMA:

1.-Indicator record is created - updated 2.-Trigger in indicator runs: List actividades = SEARCH ID From Actividad_c where Indicator_c in {current Indicator(s) Id(s)} List posibbleEntidadesAActualizar = SEARCH ID, ActorPrincipalIndicador_c FROM EntidadParticipante_c where Id IN {actividad.Id(s)}

Now you have the possibleEntidadesAActualizar and HERE is when you can update them with the values that currently you have in the Indicator.

ANOTHER GOOD FILTER OPTION WHEN SEARCHING FOR "Entidad Participante" could be to directly search for that EntidadesParticipantes that need to be change, what I mean is if you have already the value that will be set in the "Actor Principal Indicator" field, then use it in the query filters, like : WHERE ID IN :actividadesId AND ActorPrincipalIndicador__c != :newValueinActorPrincipalIndicador

Important! - Don't forget to don't allow the users to modify the ActorPrincipalIndicator__c in "Entidad Participante" as I think it will be only populated depending on "Indicator" :)

Hope it helps Tamara.

Buena Suerte :)

( I'll keep an eye in the comments if I can help further)

0

Your query in the loop is not adviced.

If Entidad is linked to Actidad, why don't you use another formula field in Entidad object ?

  • Hey, thanks! You're right but what I forgot to say is that I wanted to use a resume field using this "actor principal indicador" field as a filter, and as far as I could test, it won't let me use a formula for a resume field. am I right? – TamaraU Sep 2 '13 at 12:47
  • Yes Tamara, you are right. It is not possible to do a roll-up sum field from a formula field :( – Carolina Jan 24 '14 at 15:18

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.