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
LIM AI KHOONLIM AI KHOON 

Child object retrieve data from other child object

Hello,

My case is an Account object that has 2 children. Let's name it object 1 and object 2. Object 1 and 2 has a lookup to account field. So may I know how to retrieve data from object 1 to object 2 where payment status is 'unbill'? 

Account: has field name, ect..
Object 1: has field Name(lookup to account object), item, quantity, total item, payment status, ect...
object 2: has a field Name(lookup to account object), etc...

So every time we create a new form for object 2, it will retrieve data from object 1 that payment status is unbill automatically. Maybe we can retrieve the data in a rich text field?

Is there any trigger code that I can refer to?
 
Best Answer chosen by LIM AI KHOON
CharuDuttCharuDutt
Hi Lim Ai Khoon
Try Below Code
trigger TestTrigger on object2__c (before Insert){
    set<Id> lstId = new set<Id>();
    for(object2__c ob:trigger.new){
        if(ob.Account__c != null){
            lstId.add(ob.Account__c);
        }
    }
    list<object1__c> lstOb1 =[select Id,Name,Account__c,item__c,quantity__c, total_item__c, payment_status__c 
                              from object1__c where Account__c IN :lstId AND payment_status__c ='unbill' limit 1];
    
    for(objec2__c ob2: trigger.new){
        ob2.Textarea__c = lstOb1[0].item__c+''+lstOb1[0].quantity__c+''+lstOb1[0].total_item__c+''+lstOb1[0].payment_status__c;
    }
}
Please Mark It As best Answer If It Helps
Thank You!