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
Lucas AndradeLucas 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:
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; 
}
}
Best Answer chosen by Lucas Andrade
Rajneesh Ranjan 23Rajneesh Ranjan 23
Hi Lucas,

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.
Trigger updateBalanceHours on Worker__c (Before Insert){
    
    List<Worker__c> wList = [SELECT Worker_Name__c, Last_Year_Balance__c from Worker__c WHERE Hour_Assigned_Year__c = 2015];    
    Map<String,Integer> empHoursMap = new Map<String, Integer>();
     
    For(Worker__c w1 : wList){
        empHoursMap.put(w1.Worker_Name__c, Integer.valueof(w1.Last_Year_Balance__c));     
    }
    
    for(Worker__c w2 : Trigger.New){            
        w2.Last_Year_Balance__c = empHoursMap.get(w2.Worker_Name__c);        
    }
}
Worker

Hope this make sense.

Thanks,
Rajneesh
 

All Answers

Neetu_BansalNeetu_Bansal
Hi Lucas,

Try this trigger:
trigger SaldoAnterior on timesheet__c( before insert )
{
	List<timesheet__c> ts = [ SELECT saldo_hora_ano_registro__c FROM timesheet__c WHERE ano_referencia__c = LAST_YEAR ];
    
	for( Timesheet__c t : Trigger.New )
	{
        t.saldo_ano_anterior__c = ts[0].saldo_hora_ano_registro__c; 
	}
}
Let me know if you need any other help.

Thanks,
Neetu
R Z KhanR Z Khan
trigger SaldoAnterior on timesheet__c (before insert) {
	Set<Id> collabroatorIds = new Set<Id>();
	Map<Id, String> colaboradorIdToSaldoHaroMap = new Map<Id, String>();

	//Gets lsat year adn convert to string
	Integer lastYear = Date.today().addYears(-1).year();
	String yearToQuery = String.valueOf(lastYear);

  	for(timesheet__c t : Trigger.New){
  		collabroatorIds.add(t.colaborador__c);
	}

	 List<timesheet__c> ts = [SELECT saldo_hora_ano_registro__c FROM timesheet__c WHERE colaborador__c IN: collabroatorIds AND ano_referencia__c = :yearToQuery];
	for(timesheet__c t : ts){
  		colaboradorIdToSaldoHaroMap.put(t.colaborador__c, t.saldo_ano_anterior__c);
	}

	for(timesheet__c t : Trigger.New){
  		t.saldo_ano_anterior__c = colaboradorIdToSaldoHaroMap.get(t.saldo_hora_ano_registro__c);
	}
        
}
Lucas AndradeLucas Andrade
Hi Neetu, it works better, thanks a lot!
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?
R Z KhanR Z Khan
Hi Lucas,

the code i posted above should help you with buldifying the query
Rajneesh Ranjan 23Rajneesh Ranjan 23
Hi Lucas,

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.
Trigger updateBalanceHours on Worker__c (Before Insert){
    
    List<Worker__c> wList = [SELECT Worker_Name__c, Last_Year_Balance__c from Worker__c WHERE Hour_Assigned_Year__c = 2015];    
    Map<String,Integer> empHoursMap = new Map<String, Integer>();
     
    For(Worker__c w1 : wList){
        empHoursMap.put(w1.Worker_Name__c, Integer.valueof(w1.Last_Year_Balance__c));     
    }
    
    for(Worker__c w2 : Trigger.New){            
        w2.Last_Year_Balance__c = empHoursMap.get(w2.Worker_Name__c);        
    }
}
Worker

Hope this make sense.

Thanks,
Rajneesh
 
This was selected as the best answer
Lucas AndradeLucas Andrade
Hey R Z Khan and 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!