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
BillbayBillbay 

How to auto-populate MasterObject.field-x when DetailObject.field-y is populated?

Hi,

I have defined Object A (master) and Object B (detail) in  a master-detail relationship. I have also defined Object A.field-x inside Object B as Object B.field-x.

When Object B.field-y is populated, I would like to auto-populate Object B.field-x where ObjectB.field-y is the controlling field? Unfortunately, the standard auto-populate fields update using a workflow rule does not work as Object B.field-x is not one of the fields available for auto-population.

Thanks,
Maythil
Best Answer chosen by Billbay
JayantJayant

Replace

obj.Invoice_Number__c = i.Name;

with

obj.Invoice_Number__c = i.Id;

While Relationship fields in SFDC show the Name field in UI to end-user, its the Id of record that's actually stored in the Database.


Good Luck !

All Answers

JayantJayant
What's the Usecase ?

Seems like Object B.field-x is a Cross-Object formula field pulling in the value from  Object A.field-x. If this is the case then why would you need to update Object A.field-x based on Object B.field-x ?
Rather than using a formula to pull Object B.field-x from Object A, you may use a workflow field update instead to populate Object A's field-x from Object B's field-x. Once you make field-x (on Object B) as a WF update rather than a formula, it would become available in WF field updates.
BillbayBillbay
Jayant, thanks for the information.

Usecase is as follows. Master Object is an Invoice. Child is Line Item. Business case is such that line item information cis known first. ie) Think of car rental trip costs per trip. Each trip is a line item. On a regular basis, these need to be rolled into a periodic invoice for customer, containing all trips (ie. lines) not yet billed. Each customer has only one open invoice at any time. When a trip is assigned to a customer (lookup relationship), I want the line item to be linked to the one and only open invoice.
BillbayBillbay
Hi, Trying to overcome this via Apex Trigger as follows:

trigger InvoiceNumberUpdate on Permit__c (before update) {
for(Permit__c obj : trigger.new)
{

List<bb_Invoice__c> invoice =
[select Name from bb_Invoice__c i where 
i.Customer_Name__c = :obj.Customer_Name__c AND 
i.Status__c='Open' 
order by i.Name desc nulls last 
limit 1];

for (bb_Invoice__c i : invoice) {
obj.Invoice_Number__c = i.Name;
}

}
}


Compiled ok but when doing a record update, I get the following error:

Apex script unhandled trigger exception by user/organization: 00528000000REmV/00D28000000KV3o
InvoiceNumberUpdate: execution of BeforeUpdate
caused by: System.StringException: Invalid id: INV000001
Trigger.InvoiceNumberUpdate: line 14, column 1

Looks like something wrong with the following statement; any advice?
obj.Invoice_Number__c = i.Name;
 
JayantJayant

Replace

obj.Invoice_Number__c = i.Name;

with

obj.Invoice_Number__c = i.Id;

While Relationship fields in SFDC show the Name field in UI to end-user, its the Id of record that's actually stored in the Database.


Good Luck !
This was selected as the best answer
BillbayBillbay
Jayant, Super - Works now. Thanks for the sharing!
JayantJayant
You are welcome.

Please don't forget to mark the question as Resolved.
You may choose the answer that you think actually helped you the most to resolve your query or post your own solution (how you overcame it) and mark that as the Best Answer.