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
Olga Kim 5Olga Kim 5 

populate a field with parent object data

I need to create a record on a BILLING object every time when a bill checked the box on another TEST object is checked. 

My problem is that there is a required field on the BILLING record called rate__c. 
The rate__c field has to be populated by a field from PROJECT which is a parent object of TEST.  (There is a lookup relationship between TEST and PROJECT. And lookup relationship between PROJECT and BILLING)

I wrote a code that works only when I prepopulated a rate__c field on the BILLING record. 

I also wrote a code that retrieves a rate field from project object, but for some reason, I get an error message that required field - rate__c is missing.  

 Please, help me to resolve this issue
  
public class testBillingLineCreate {
    public static void createBillingLine (list<TEST__c> TestList, map<id, TEST__c> oldMap)
    {
       list<AcctSeed__Billing_Line__c> billingLineList=new List<AcctSeed__Billing_Line__c>();      
     
       
        for(TEST__c objtest:TestList)
        {
        if(objtest.Billed__c==true && oldMap.get(objtest.Id).Billed__c==false)
            
        {
            AcctSeed__Billing_Line__c bill=new AcctSeed__Billing_Line__c();
            bill.AcctSeed__Billing__c=objtest.Billing__c;
            bill.AcctSeed__Project__c=objtest.Project__c;
            bill.AcctSeed__Hours_Units__c=objtest.time__c;                  
                      
  
            
            bill.AcctSeed__Rate__c= objtest.Project__r.Billing_Rate__c;
          
            billingLineList.add(bill);
        }
            
        }
        
        insert billingLineList;
    }
    }
Best Answer chosen by Olga Kim 5
ANUTEJANUTEJ (Salesforce Developers) 
Hi Olga,

I found the below developer forum thread that has a similar requirement can you please have a look at it once:

>> https://success.salesforce.com/answers?id=9063A000000ibeiQAA

Another way I could think of is you could have a trigger such that you can store the field value according to the value selected in the lookup field.

In case if this comes handy can you please choose this answer as the best answer so that it can be used by others in the future.

Regards,
Anutej

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Olga,

I found the below developer forum thread that has a similar requirement can you please have a look at it once:

>> https://success.salesforce.com/answers?id=9063A000000ibeiQAA

Another way I could think of is you could have a trigger such that you can store the field value according to the value selected in the lookup field.

In case if this comes handy can you please choose this answer as the best answer so that it can be used by others in the future.

Regards,
Anutej
This was selected as the best answer
Olga Kim 5Olga Kim 5
Thank you. it was helpful!!!