function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sergio Ruiz Barrios 5Sergio Ruiz Barrios 5 

Help with a Trigger

Hi All,

I need to create a trigger that flags a checkbox when two fields of different objects have the same value:
I have two objects with a field a want to map:
OBJECT: Radio_Taxi__c----> FIELD: Radio_Taxi_Trip_ID__c
OBJECT: Gastos__c----> FIELD: ID_Radio_Taxi__c

We always create a new Gasto record before a new Radio Taxi Record. 
I need to set to TRUE a field (Gasto_creado_en_SF__c) in Radio Taxi Object every time a new Radio Taxi record is created and the  Radio_Taxi_Trip_ID__c and  ID_Radio_Taxi__c fields have the same value.

Thanks!!!
 
Best Answer chosen by Sergio Ruiz Barrios 5
PavanKPavanK
trigger RADIOTAXI on Radio_Taxi__c (after insert)
{
set<Id> setRadioTxi = new set<id>();
List<Gastos__c> lstGasto = List<Gastos__c>();
for(Radio_Taxi__c obj : trigger.new)
        setRadioTxi.add(obj.Radio_Taxi_Trip_ID__c);
for(Gastos__c objGas: [select id, ID_Radio_Taxi__c from Gastos__c where Gasto_creado_en_SF__c=false and ID_Radio_Taxi__c!=Null and ID_Radio_Taxi__c In : setRadioTxi])
{
objGas.Gasto_creado_en_SF__c=true;
lstGasto.add(objGas);
}
if(lstGasto.size()>0)
update lstGasto;
}

 

All Answers

PavanKPavanK
Write trigger on Radio_Taxi__c- with below algorithm

trigger on Radio_Taxi__c
{
set<Id> setRadioTxi = new set<id>();
list<Gastos__c> lstGasto = list<Gastos__c>();
for(Radio_Taxi__c obj : trigger.new)
        setRadioTxi.add(obj.Radio_Taxi__c);
for(Gastos__c objGas: [select id, ID_Radio_Taxi__c from Gastos__c where ID_Radio_Taxi__c=false and ID_Radio_Taxi__c!=Null and ID_Radio_Taxi__c In : setRadioTxi])
{
objGas.ID_Radio_Taxi__c=true;
lstGasto.add(objGas);
}
if(lstGasto.size()>0)
update lstGasto;
}

Please note above code is not compiled or tested.

I will suggest to check recursive trigger if you habe trigger on Gastos object also.

Please mark answer as best if it helped ny way.


 
Sergio Ruiz Barrios 5Sergio Ruiz Barrios 5
Hi PavanK, thanks for your help and fast reply. I get an error when saving: unexpected token: 'List' at line 4 column 27

trigger RADIOTAXI on Radio_Taxi__c (after insert)
{
set<Id> setRadioTxi = new set<id>();
List<Gastos__c> lstGasto = List<Gastos__c>();
for(Radio_Taxi__c obj : trigger.new)
        setRadioTxi.add(obj.Radio_Taxi__c);
for(Gastos__c objGas: [select id, ID_Radio_Taxi__c from Gastos__c where ID_Radio_Taxi__c=false and ID_Radio_Taxi__c!=Null and ID_Radio_Taxi__c In : setRadioTxi])
{
objGas.ID_Radio_Taxi__c=true;
lstGasto.add(objGas);
}
if(lstGasto.size()>0)
update lstGasto;
}
PavanKPavanK
replace

List<Gastos__c> lstGasto = List<Gastos__c>();

with

List<Gastos__c> lstGasto = new List<Gastos__c>();
Sergio Ruiz Barrios 5Sergio Ruiz Barrios 5
Still not working :(
Error: Compile Error: Invalid field Radio_Taxi__c for SObject Radio_Taxi__c at line 6 column 26
PavanKPavanK
trigger RADIOTAXI on Radio_Taxi__c (after insert)
{
set<Id> setRadioTxi = new set<id>();
List<Gastos__c> lstGasto = List<Gastos__c>();
for(Radio_Taxi__c obj : trigger.new)
        setRadioTxi.add(obj.Radio_Taxi_Trip_ID__c);
for(Gastos__c objGas: [select id, ID_Radio_Taxi__c from Gastos__c where Gasto_creado_en_SF__c=false and ID_Radio_Taxi__c!=Null and ID_Radio_Taxi__c In : setRadioTxi])
{
objGas.Gasto_creado_en_SF__c=true;
lstGasto.add(objGas);
}
if(lstGasto.size()>0)
update lstGasto;
}

 
This was selected as the best answer