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 

Pull data from other object

Hi, my case is I have 2 objects, objects 1 and 2 with their own fields. both of them linked with the account name. I want to pull a few data that have the same account name from object one to object 2.

Let's say, Object 1 has 10 records under account Ali, so I want to pull data (item, quantity, total) from this object to object 2 under Ali`s name.

May I know how to do it? Is there any trigger code that I can refer to? If you not really understand the case, kindly let me know.
Best Answer chosen by LIM AI KHOON
CharuDuttCharuDutt
Hii Lim Ai Khoon
Try Below Trigger
trigger testtrigger34324 on Object1__c (before insert ,after update) {
    set<id> lstId = new set<id>();
    list<Object2__c> lstOb2 = new list<Object2__c>();
    if((trigger.IsBefore && trigger.IsInsert) || (trigger.IsAfter &&trigger.IsUpdate)){
    for(Object1__c o : trigger.new){
        if(o.Account__c != null){
        lstId.Add(o.Account__c);
        	}
    	}
    }
    
    list<Object1__c> lstO = [Select Id,Name,Account__c,Item__c,Quantity__c,Total__c,
                             (Select Id,Name,Account__c,Item__c,Quantity__c,Total__c From Object2__r) 
                             from Object1__c Where Account__c In :lstId];
    for(Object1__c Ob:lstO){   
        for(Object2__c Ob2:ob.Object2__r){
            Ob2.Test_Text__c = Ob.Item__c;
            Ob2.Test_Text__c = Ob.Qunatity__c;
            Ob2.Test_Text__c = Ob.Total__c;
lstOb2.add(Ob2);
        }
    }
   if(lstOb2.size()>0){
        update lstOb2;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!


 

All Answers

CharuDuttCharuDutt
Hii Lim Ai Khoon
Try Below Trigger
trigger testtrigger34324 on Object1__c (before insert ,after update) {
    set<id> lstId = new set<id>();
    list<Object2__c> lstOb2 = new list<Object2__c>();
    if((trigger.IsBefore && trigger.IsInsert) || (trigger.IsAfter &&trigger.IsUpdate)){
    for(Object1__c o : trigger.new){
        if(o.Account__c != null){
        lstId.Add(o.Account__c);
        	}
    	}
    }
    
    list<Object1__c> lstO = [Select Id,Name,Account__c,Item__c,Quantity__c,Total__c,
                             (Select Id,Name,Account__c,Item__c,Quantity__c,Total__c From Object2__r) 
                             from Object1__c Where Account__c In :lstId];
    for(Object1__c Ob:lstO){   
        for(Object2__c Ob2:ob.Object2__r){
            Ob2.Test_Text__c = Ob.Item__c;
            Ob2.Test_Text__c = Ob.Qunatity__c;
            Ob2.Test_Text__c = Ob.Total__c;
lstOb2.add(Ob2);
        }
    }
   if(lstOb2.size()>0){
        update lstOb2;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!


 
This was selected as the best answer
LIM AI KHOONLIM AI KHOON
Didn't understand relationship 'From Object2__r' in FROM part of query call. 
Hi Charudutt,

Thank you for your help. I received the above error, It comes from the query line. Actually, 'both of them linked with the account name' means object 1 has a lookup value to the account, and object 2 also has a lookup value to the account. So I want few data from object 1 to object 2. I'm sorry for making you confusing. So let's say a user in object 1 user filling lookup field name Ali, and object to also look up to Ali account, then data from object 1 will be displayed in object 2.