You need to sign in to do that
Don't have an account?
Rakul 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.
Hi
Please find the solution.
Please mark it as the best answer if it works for you.
Please do some changes according to your code.
Thank You
All Answers
Hi
Please find the solution.
Please mark it as the best answer if it works for you.
Please do some changes according to your code.
Thank You
Thank You!
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)
{
}
}