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
Rakul SrivastavRakul Srivastav 

need to update a field on an object based on another object's name

I have a requirement to update a field of an Object A, with the names of another object B which are related to A.

Example Object A can have a,b,c related Object B, new field on object A should show me a,b,c(which are object B record names) and so on, whenever i add a new related object B to object A.

I need to use bulk approach and trigger on Object B.

 

Best Answer chosen by Rakul Srivastav
Suraj Tripathi 47Suraj Tripathi 47

Hi 

Please find the solution.

public class UpdateObjectData(){
public static void updateObjectB(List<objectB__c> objectBList){
set<Id> onjectAset=new set<Id>();
for(objectB__c obj:objectBList){
onjectAset.add(obj.objectA__c);
}

List<objectA__c> objectAList=new List<objectA__c>([Select Id,Name,a__c,b__c,c__c from objectA__c where Id in: onjectAset]);

map<Id,objectA__c> objectAmap=new map<Id,objectA__c>([Select Id,Name,a__c,b__c,c__c from objectA__c where Id in: onjectAset]);
List<objectA__c> updateObjectAList=new List<objectA__c>();
for(objectB__c obj:objectBList){
 objectA__c objA=new objectA__c();
 objA.Id=objectAmap.get(obj.objectA__c);
 objA.a__c=obj.a__c;
 objA.b__c=obj.b__c;
 objA.c__c=obj.c__c;
 updateObjectAList.add(objA);
}

if(updateObjectAList.size()>0){
update updateObjectAList;
}

}

}
 
trigger updateData on objectB__c(after insert){

 
if(trigger.isAfter && trigger.isInsert){
UpdateObjectData.Data(trigger.new);
}

}


Please mark it as the best answer if it works for you.

Please do some changes according to your code.

Thank You

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi 

Please find the solution.

public class UpdateObjectData(){
public static void updateObjectB(List<objectB__c> objectBList){
set<Id> onjectAset=new set<Id>();
for(objectB__c obj:objectBList){
onjectAset.add(obj.objectA__c);
}

List<objectA__c> objectAList=new List<objectA__c>([Select Id,Name,a__c,b__c,c__c from objectA__c where Id in: onjectAset]);

map<Id,objectA__c> objectAmap=new map<Id,objectA__c>([Select Id,Name,a__c,b__c,c__c from objectA__c where Id in: onjectAset]);
List<objectA__c> updateObjectAList=new List<objectA__c>();
for(objectB__c obj:objectBList){
 objectA__c objA=new objectA__c();
 objA.Id=objectAmap.get(obj.objectA__c);
 objA.a__c=obj.a__c;
 objA.b__c=obj.b__c;
 objA.c__c=obj.c__c;
 updateObjectAList.add(objA);
}

if(updateObjectAList.size()>0){
update updateObjectAList;
}

}

}
 
trigger updateData on objectB__c(after insert){

 
if(trigger.isAfter && trigger.isInsert){
UpdateObjectData.Data(trigger.new);
}

}


Please mark it as the best answer if it works for you.

Please do some changes according to your code.

Thank You

This was selected as the best answer
CharuDuttCharuDutt
Hii rakul try below trigger
trigger ContactTrigger on Contact (After Insert,After Update,After Delete) {
   List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Contact con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Contact con : Trigger.old) { 
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,Description ,(Select id,name from contacts) from Account where Id in : setAccIds]){
      String s ='';
    
        for(Contact Con :acc.contacts){
            s+=Con.Name +',';
        }
        acc.Description =  s.removeEnd(',');
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As Best Answer If It helps
Thank You!
Rakul SrivastavRakul Srivastav

hi Suraj,

 

I'm using the below Code, where the new field where i want to populate the records is 'newField__c'.

I'm able to print and check the records coming up in  'eeUpdate.newField__c', but when i query the field, the data is not populated there and I'm also getting 'duplicate Id in list' exception. Could you please help to modify this code to remove the duplicate Id in list exception.

 

 

public static void populateNumbers(TriggerFramework.Context context){
        List<Object B__c> newList = (List<Object B__c>) context.newList; 
        Boolean afterInsert = context.isAfter && context.isInsert ? true : false;
        Boolean afterUpdate = context.isAfter && context.isUpdate ? true : false;    
        Object A__c eeUpdate;
        String IFAppend = '';    
        List<Object A__c> UpdateList = new List<Object A__c>();
        
        Map<Id,String> iFCountMap = new Map<Id,String>();
        Set<Id> recordIdList = new Set<Id>();  
        
        try{
            if(afterInsert || afterUpdate){
                
                for(Object B__c newObjB : newList)
                {
                    if(!String.isBlank(newObjB.Event__c)){
                        iFCountMap.put(newObjB.Event__c, null);
                          
                    }
                    
                }
                 
            }
            if(!iFCountMap.keySet().isEmpty()){
                List<Object B__c> IFList = [Select Id, 
                                                                  Event__c,
                                                                  Name, 
                                                                  CreatedDate 
                                                                  FROM Object B__c
                                                                  WHERE Event__c IN : iFCountMap.keySet()
                                                                  LIMIT 49999];
                
                
                for(Object B__c IFLoop : IFList){
                    
                    
                    if(iFCountMap.containsKey(IFLoop.Event__c)){
                        if(iFCountMap.get(IFLoop.Event__c) == null){
                            iFCountMap.put(IFLoop.Event__c, IFLoop.Name);
                           
                        }else{
                            IFAppend= iFCountMap.get(IFLoop.Event__c) + ',' + IFLoop.Name;
                            iFCountMap.put(IFLoop.Event__c, IFAppend);
                            eeUpdate = new Object A__c();
                            eeUpdate.Id = IFLoop.Event__c;
                            eeUpdate.newField__c = IFAppend;
                            UpdateList.add(eeUpdate);
                            IFAppend = '';
                            
                        }
                         
                    } 
                    
                            
                }
                
                if(!UpdateList.isEmpty()){
                    List<Database.SaveResult> eeUpdateResult = Database.update(UpdateList, false);
                    
                   
                }
                
            }
             
            
        }catch(Exception e)
        {
            
        }
        }

Suraj Tripathi 47Suraj Tripathi 47
I am not able to get you