且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Apex 触发期间连接不相关的对象

更新时间:2023-02-09 22:12:45

我找到了在同一个 Trigger 中回答这个问题的方法.Trigger.isAfter &&Trigger.isInsert 代码块解决了我的问题.

I was able to figure out a way to answer this issue within the same Trigger. The Trigger.isAfter && Trigger.isInsert code block is what solved my problem.

trigger newAccountCreated on Account (before insert, after insert, after delete) {

    List<Account> alist = Trigger.New;
    List<Account> oldlist = Trigger.old;

    if(Trigger.isBefore){

        for(Account a : alist) {

            if (a.RecordTypeId == '012i0000001Iy1H') {
                Portal_Content__c p = new Portal_Content__c(
                    Name=a.Name,
                    RecordTypeId='012i0000001J1zZ'
                );
                insert p;

                a.Portal_Content_Record__c = p.Id;
            }
        }
    }
    else if (Trigger.isAfter && Trigger.isDelete){
        for(Account a : oldlist){
            for(Portal_Content__c p : [SELECT ID FROM Portal_Content__c WHERE ID = :a.Portal_Content_Record__c]){
                delete p;
            }
        }

    }

    if (Trigger.isAfter && Trigger.isInsert){
        for(Account a : alist){

            List<Portal_Content__c> plist = [SELECT ID FROM Portal_Content__c WHERE Id = :a.Portal_Content_Record__c];

            for(Portal_Content__c p : plist){
                p.School_SFDC_ID__c = a.Id;
                update p;
            }

        }
    }

}

此代码块查询与帐户记录的 Portal_Content_Record__c 字段值匹配的 Portal_Contact__c 记录,该值在第一个代码块中分配.然后获取找到的 Portal_Content__c 记录,并将原始帐户的 ID 分配给记录的 School_SFDC_ID__c 字段值.

This code block does a query for Portal_Contact__c records that match the Account record's Portal_Content_Record__c field value, which is assigned in the first code block. It then takes the Portal_Content__c records found, and assigns the original Account's Id to the record's School_SFDC_ID__c field value.