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
ECoronaECorona 

Event: Updated Related record in account

Hi! I'm looking how i can writte a trigger to do this:

In my accounts, i have a lookup field to object B, this field is not master-detail.
When i schedule a meeting in my account, i want to update a datetime field in object B.

I'll really appreciate your ideas!
 
Best Answer chosen by ECorona
Khan AnasKhan Anas (Salesforce Developers) 
Hi ECorona,

I trust you are doing very well. It is a pleasure to be in touch with you again.

Please try the below code. Kindly modify the code as per your requirement.

Parent sObject : Test1__c
Field (DateTime) : Dt__c

ChildsObject : Account
Field (DateTime) : Schedule__c
Lookup (Test1__c) : Test1__c


Trigger:
trigger DateTimeT on Account (after insert, after update) {
    
    Map<ID, Test1__c> parentOpps = new Map<ID, Test1__c>();
    List<Id> listIds = new List<Id>();
    
    for (Account childObj : Trigger.new) {
        listIds.add(childObj.Test1__c);
    }
    
    parentOpps = new Map<Id, Test1__c>([SELECT Id, Dt__c FROM Test1__c WHERE ID IN :listIds]);
    
    List<Test1__c> updatedParentOpps = new List<Test1__c>();
    
    for (Account con: Trigger.new){
        if(parentOpps.containsKey(con.Test1__c)) { 
            Test1__c myAccount = parentOpps.get(con.Test1__c);                                                
            if(con.Schedule__c!=null) { 
                myAccount.Dt__c = con.Schedule__c;  
                updatedParentOpps.add(myAccount); 
                
            }
        }
    }
    
    if(parentOpps.size()>0){
        update parentOpps.values();
    }
}

I hope it helps you.

Kindly let me inform if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi ECorona,

I trust you are doing very well. It is a pleasure to be in touch with you again.

Please try the below code. Kindly modify the code as per your requirement.

Parent sObject : Test1__c
Field (DateTime) : Dt__c

ChildsObject : Account
Field (DateTime) : Schedule__c
Lookup (Test1__c) : Test1__c


Trigger:
trigger DateTimeT on Account (after insert, after update) {
    
    Map<ID, Test1__c> parentOpps = new Map<ID, Test1__c>();
    List<Id> listIds = new List<Id>();
    
    for (Account childObj : Trigger.new) {
        listIds.add(childObj.Test1__c);
    }
    
    parentOpps = new Map<Id, Test1__c>([SELECT Id, Dt__c FROM Test1__c WHERE ID IN :listIds]);
    
    List<Test1__c> updatedParentOpps = new List<Test1__c>();
    
    for (Account con: Trigger.new){
        if(parentOpps.containsKey(con.Test1__c)) { 
            Test1__c myAccount = parentOpps.get(con.Test1__c);                                                
            if(con.Schedule__c!=null) { 
                myAccount.Dt__c = con.Schedule__c;  
                updatedParentOpps.add(myAccount); 
                
            }
        }
    }
    
    if(parentOpps.size()>0){
        update parentOpps.values();
    }
}

I hope it helps you.

Kindly let me inform if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
This was selected as the best answer
ECoronaECorona
Hello Khan! Thank you for your suggestion, it works great!!! :D

I owe you a beer