Triggers 将图像从子记录复制到父记录的触发器

Triggers 将图像从子记录复制到父记录的触发器,triggers,salesforce,apex,Triggers,Salesforce,Apex,我正在尝试编写一个触发器,该触发器将使用子对象上RTA字段的图像更新父对象上的RTA字段 我在创建子对象时遇到以下错误 Apex trigger Updateparent导致意外异常,请与管理员联系:Updateparent:执行AfterInsert的原因:System.StringException:无效id:…此处显示图像…外部入口点 信息 manutd是子对象 Account是父对象 Image_u_c是子对象上的RTA字段 Copy_image___c是帐户上的RTA字段 这是触发代码

我正在尝试编写一个触发器,该触发器将使用子对象上RTA字段的图像更新父对象上的RTA字段

我在创建子对象时遇到以下错误

Apex trigger Updateparent导致意外异常,请与管理员联系:Updateparent:执行AfterInsert的原因:System.StringException:无效id:…此处显示图像…外部入口点

信息 manutd是子对象 Account是父对象 Image_u_c是子对象上的RTA字段 Copy_image___c是帐户上的RTA字段

这是触发代码

在插入后,在更新后,在Man_Utd_1___c上触发Updateparent{ Map parentAccounts=新地图; List listIds=新列表; 对于Man_Utd_1__c childObj:Trigger.new{ listIds.addchildObj.Image\uu c; } parentAccounts=new Map[选择id,选择id,从Man_Utd_s__r从id所在的帐户中选择图像:ListID]; 对于Man_Utd_1__c manu:Trigger.new{ Account myParentAccounts=parentAccounts.getmanu.Image\uuu\c; myParentAccounts.Copy\u image\u c=manu.image\u c; } 更新parentAccounts.values; } 是否有人可以建议如何纠正此问题,或者是否有可能纠正此问题?

编辑以回答评论 这一个对我有效,对空检查有一点修改。试一试?该字段在Account和Contact上都称为Image_u;c

trigger rollupImage on Contact (after insert, after update) {

    Set<Account> parents = new Set<Account>();
    for (Contact child : trigger.new) {
        if(child.AccountId != null && child.Image__c != null){
            parents.add(new Account(Id = child.AccountId, Image__c = child.Image__c));
        }
    }

    if(!parents.isEmpty()){
        List<Account> toUpdate = new List<Account>();
        toUpdate.addAll(parents);
        update toUpdate;
    }
}
优化-第2轮

这看起来有点愚蠢-我们查询帐户,但我们只需要获取Id。。。但我们已经知道身份了,对吗?所以,看看这个专业的把戏,因为我的同事是ManU的超级粉丝

trigger Updateparent on Man_Utd_1__c (after insert, after update) {

    Set<Account> parents = new Set<Account>();
    for (Man_Utd_1__c child : trigger.new) {
        if(child.Account__c != null){
            parents.add(new Account(Id = child.Account__c, Copy_Image__c = child.Image__c));
        }
    }

    if(!parents.isEmpty()){
        List<Account> toUpdate = new List<Account>();
        toUpdate.addAll(parents);
        update toUpdate;
    }
}
注意:在编写触发器时,优化是关键

在编写触发器时,请确保遵循所有最佳实践:

关于触发器的基调:

优化-我已经在第一行和大写字母中指定了它,因为它是编写逻辑的关键 适当的缩进和代码注释,以便任何处理同一代码的人只需浏览注释即可轻松理解逻辑。确保您在初始注释上更新了lastModifiedBy行,这样有助于跟踪上次工作/更新的人员以及日期。最好在每一行保留适当的代码注释-如果需要的话 具有专有名称的变量,例如:如果变量为列表类型,则为'lst'前缀或'list'后缀,然后为专有名称,例如:lstContactsToInsert或lstContactsToUpdate 始终创建一个单独的处理程序类,不要将所有逻辑都写在触发器本身上“作为最佳实践” 有适当的触发上下文。通过有关触发器的salesforce文档了解何时使用适当的触发器上下文。如果可以使用此方法处理,则使用“Before”上下文,如果不能,则使用“After”上下文,这样可以限制DML。 关于Handler的注释:

触发器的前3个步骤也适用于此处 在编写逻辑的地方使用公共私有方法,并在其他方法中使用 关于TestClass的基调:

始终使用测试类中的“通用测试数据类”编写测试类。 使用适当的测试方法名称,如:test_method_One'或'testImageUpdates' 使用断言并测试所有用例-阴性和阳性测试 一,。触发

二,。处理者

这可以参考您的用例。 如果这有帮助,请标记为“最佳答案”

忠诚度-儿童Obj

字段:

lightingdata\uuuu\uu联系人\uuu\c关系 lightingdata\uuuu imagenims\uuuu c//图像字段 联系人-父对象

字段:lightingdata\uuu Imagefromloyalty\uuu\c

在pareant对象字段中,它将返回具有URL的图像字段。
然后,您可以创建一个公式字段,只需参考公式中的lightingdata\uuuu Imagefromloyalty\uuu c,它将显示图像。

您好,谢谢您的详细回复。我真的是Apex的新手,所以非常感谢你解释的细节。我尝试了您的代码,但得到以下错误--错误:编译错误:第6行第42列SObject Man_Utd_1___c的字段Account___c无效,来自此行parents.addnew AccountId=child.Account___c,Copy_Image_c=child.Image__c;你知道为什么会抛出这个错误吗?再次感谢。转到安装->创建,这里是Man\uUd\uUc对象的定义。找到标记为LookupAccount的字段并使用该字段的API名称?您好,再次感谢。我用正确的字段标签替换了Account_u_c,并且我能够保存到代码中。我也可以创建子记录,但不幸的是,图像没有复制过来。该字段仍然为空。调试日志是否给出任何提示?帐户上最后一次修改的日期是否与孩子的日期不同?您好,我刚刚注意到,当我创建自定义对象记录并通过查找将其与帐户对象关联时。帐户对象记录中的图像消失。之后,它们具有相同的上次修改日期和时间 每次更新。
Set<Id> ids = new Set<Id>(); // Set will ensure we won't have duplicate values
for (Man_Utd_1__c childObj : Trigger.new) {
    ids.add(childObj.Account__c);
}
parentAccounts = new Map<Id, Account>([SELECT id FROM Account WHERE ID IN :ids]);
// I've removed the subquery, you don't need it, right?

for (Man_Utd_1__c manu : Trigger.new) {
    if(parentAccounts.containsKey(manu.Account__c)){
        parentAccounts.get(manu.Account__c).Copy_image__c = manu.Image__c;
    }
}
update parentAccounts.values();
trigger Updateparent on Man_Utd_1__c (after insert, after update) {

    Set<Account> parents = new Set<Account>();
    for (Man_Utd_1__c child : trigger.new) {
        if(child.Account__c != null){
            parents.add(new Account(Id = child.Account__c, Copy_Image__c = child.Image__c));
        }
    }

    if(!parents.isEmpty()){
        List<Account> toUpdate = new List<Account>();
        toUpdate.addAll(parents);
        update toUpdate;
    }
}
   /**
    *   @TriggerName : ContactTrigger
    *   @CreatedBy   : Rajesh Kamath
    *   @CreatedOn   : 30-Nov, 2016
    *   @Description : Trigger to update the image on parent object 'Account' based on the child object 'Contact' 
    *   @LastModified: None
    */  
    trigger ContactTrigger on Contact(after insert, after update) {

        //Instantiate the handler class
        ContactTriggerHandler objContactHandler = new ContactTriggerHandler();

        /* Trigger Context */
        if(Trigger.isAfter) {

            //Fire on Insert
            if(Trigger.isInsert) {

                objContactHandler.onAfterInsert(trigger.new); 
            }

            //Fire on Update
            if(Trigger.isUpdate) {

                objContactHandler.onAfterUpdate(trigger.new, trigger.oldMap);
            }
        }
    }
   /**
     *   @HandlerName   : ContactTriggerHandler 
     *   @TriggerName   : ContactTrigger 
     *   @TestClassName : ContactTriggerHandlerTest [Make sure you create this with the steps at the end of this handler] 
     *   @CreatedBy   : Rajesh Kamath
     *   @CreatedOn   : 30-Nov, 2016
     *   @Description : Trigger to update the image on parent object 'Account' based on the child object 'Contact' 
     *   @LastModified: None
     */  
    public with sharing class ContactTriggerHandler {

        /*
           @MethodName : onAfterInsert
           @Parameters : List<Contact> lstNewContactRecords
           @LastModifiedBy : None [Make sure when anyone updates even this line will be updated for help for other users who work on later phase]
        */
        public void onAfterInsert(List<Contact> lstNewContactRecords){

            //Call a common method where we have logic
            reflectChildImageOnParent(lstNewContactRecords, null);      
        }

        /*
           @MethodName : onAfterUpdate
           @Parameters : List<Contact> lstContactRecords, Map<Id, Contact> mapOldContactRecords
           @LastModifiedBy : None [Make sure when anyone updates even this line will be updated for help for other users who work on later phase]
        */
        public void onAfterUpdate(List<Contact> lstContactRecords, Map<Id, Contact> mapOldContactRecords){

            //Call a common method where we have logic
            reflectChildImageOnParent(lstContactRecords, mapOldContactRecords);     
        }

        /*
           @MethodName : reflectChildImageOnParent
           @Parameters : List<Contact> lstContactRecords, Map<Id, Contact> mapOldContactRecords
           @LastModifiedBy : None [Make sure when anyone updates even this line will be updated for help for other users who work on later phase]
        */
        private static void reflectChildImageOnParent(List<Contact> lstContactRecords, Map<Id, Contact> mapOldContactRecords){

            /* Local variable declaration */
            List<Account> lstAccountsToUpdate = new List<Account>();

            for(Contact objContact : lstContactRecords) {

                //Execute on insert and update
                if((Trigger.isInsert || (Trigger.isUpdate && objContact.Child_Image__c != mapOldContactRecords.get(objContact.Id).Child_Image__c)) && objContact.Account__c != null) {

                    //List of accounts to update
                    lstAccountsToUpdate.add(new Account(Id = objContact.Account__c, Parent_Image__c = objContact.Child_Image__c));                  
                }
            }

            //Update the account records
            if(!lstAccountsToUpdate.isEmpty()) {

                update lstAccountsToUpdate;
            }
        }
    }
trigger loyaltytocon on Loyalty__c (after insert, after update) {
    Map<id, Contact> conlist;
        List<Id> idlist=new List<id>();
        for (Loyalty__c loy:trigger.new) {    
            idlist.add(loy.lightingdata__Contact__c);
        }
        conlist= new Map<id, contact>([select id, lightingdata__Imagefromloyalty__c from contact where id In : idlist]);

        for(Loyalty__c lo:trigger.new){
            contact con = conlist.get(lo.lightingdata__Contact__c) ;
            system.debug('con data' + con);
            con.lightingdata__Imagefromloyalty__c = lo.lightingdata__imagenims__c;
        }
     update  conlist.values();
}