Skip to main content
Emmanuel Corona 님이 #Apex에 질문했습니다
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!

 
답변 2개
  1. 2018년 8월 27일 오후 1:17
    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
0/9000