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
Ramprasath Subramaniam 17Ramprasath Subramaniam 17 

Copy field value from different custom object, based on the close date

HI,
I am new to Salesforce Devlopment and would appricate your help.

I have two custom objects, 
1. Commission line
2. Commission rate

commission rate object captures,
cateogory, commission rate, startdate, enddate.

I would like to populate "Rate" field in "commission line" object, based on the "category" field value in "Commission line" object.

for e.g. when Commission_line.Category =  hardware AND Commission_line.CloseDate field = 04/15/21

Comission rate object
CategoryRatesStart periodEnd period
hardware2.51/1/202112/31/2021
Software41/1/202112/31/2021
SMA31/1/202112/31/2021
hardware2.51/1/202212/31/2022

based on "Commission_line.Category"  and "Commission_line.closedate", I want to poulate the Commission_line.rate from the Rate value form the Commmission rate object. 
in this case, rate =  2.5. category falls within the date range
 
 
Naresh AltokkNaresh Altokk
Hi Ramprasath Subramaniam 17 ,

Try Formula field, That would definetly solves your issue. 

Thanks,
Naresh
Ramprasath Subramaniam 17Ramprasath Subramaniam 17
Hi Naresh,

would you be able to guide me on how this formual would be written
 
Suraj Tripathi 47Suraj Tripathi 47

Hi,

I can writte trigger like this.

public class PopulateData{
public static void changeData(List<Commission_line__c> comList){
 set<id> commissionRateSet=new set<id>();
    for(Commission_line__c com:comList){
        commissionRateSet.add(con.account__c);
    }
  List<Commission_rate__c> commissionRateList=new List<Commission_rate__c>([select id,Rate__c from Commission_rate__c where id in: commissionRateSet]);
    
    List<Commission_line__c> CommissionlineList=new List<Commission_line__c>();
    for(Commission_rate__c cmr:commissionRateList){
        for(Commission_line__c com:comList){
            if(cmr.id==com.account__c){
            
            if(com.category__c=='hardware' && com.CloseDate__c==date.newinstance(2021, 4, 15)){
                Commission_line__c comInstance=new Commission_line__c();
                comInstance.id=com.id;
                comInstance.Rate__c=cmr.Rate__c;
                CommissionlineList.add(comInstance);
            }
               
            }
        }
    }
    
    update CommissionlineList;

}
}


trigger

trigger ContactTrigger on Commission_line__c (after undelete, before delete,After Insert,before insert,before update) {
if(trigger.isAfter && trigger.isInsert){
	PopulateData.changeData(trigger.new);
}


here is the case Commission_line__c is a child and Commission_rate__c is a parent.
If it helps you please mark it as The Best Answer.

Thank You

Ramprasath Subramaniam 17Ramprasath Subramaniam 17
Hi Suraj,

thank you for sharing the code. really appreciate the help.

can this also be solutioned using FLOW in salesforce?