You need to sign in to do that
Don't have an account?
Lucas Andrade
Create a trigger to populate a field
Hi!
I need to create a trigger that will populate a field with a value of other record of the same object. Here's the case:
I have a object called "timesheet__c" that contains an balance of hours worked for everybody in my company and we create new records every year to register new values. One of my fields calls "Last year balance" with the balance of hours of the previous year and i need that field to autopopulate everytime i create a new record in the object. How can i do this?
I have something like this, but is not working:
I need to create a trigger that will populate a field with a value of other record of the same object. Here's the case:
I have a object called "timesheet__c" that contains an balance of hours worked for everybody in my company and we create new records every year to register new values. One of my fields calls "Last year balance" with the balance of hours of the previous year and i need that field to autopopulate everytime i create a new record in the object. How can i do this?
I have something like this, but is not working:
trigger SaldoAnterior on timesheet__c (before insert) { for(timesheet__c t : Trigger.New){ List<timesheet__c> ts = [SELECT saldo_hora_ano_registro__c FROM timesheet__c WHERE colaborador__c = : t.colaborador__c AND ano_referencia__c = '2015']; t.saldo_ano_anterior__c = ts.saldo_hora_ano_registro__c; } }
I tried one.. Please find the code snippet below:
I am using another custom object called Worker__C which is having below fields -
Assigned_Hours__c (Number) - Yearly assigned hour
Hour_Assigned_Year__c(Number) - Current Year i.e. 2016
Last_Year_Balance__c(Number) - Hours balance from previous year i.e. 2015
This trigger is populating Last_Year_Balance__c from existing row with same Worker_Name__c, Here you can use any other field that is unique/key for your timesheet object.
Hope this make sense.
Thanks,
Rajneesh
All Answers
Try this trigger: Let me know if you need any other help.
Thanks,
Neetu
But i need to refine my SOQL criteria because i have multiple records in this object and i need one specific, that's why i tried to put de list inside the for loop and use the "colaborador__c = : t.colaborador__c' criteria. There's some way to do this using your example?
the code i posted above should help you with buldifying the query
I tried one.. Please find the code snippet below:
I am using another custom object called Worker__C which is having below fields -
Assigned_Hours__c (Number) - Yearly assigned hour
Hour_Assigned_Year__c(Number) - Current Year i.e. 2016
Last_Year_Balance__c(Number) - Hours balance from previous year i.e. 2015
This trigger is populating Last_Year_Balance__c from existing row with same Worker_Name__c, Here you can use any other field that is unique/key for your timesheet object.
Hope this make sense.
Thanks,
Rajneesh
I'll try both and see which one help me better and then i vote the best answer, but thanks anyway fot both of you!