Triggers 关于触发器的问题?

Triggers 关于触发器的问题?,triggers,salesforce,apex-code,apex,Triggers,Salesforce,Apex Code,Apex,我有以下要求: 1) 获取已更改个人资料的所有用户的列表 2) 然后查询FRUP(它是一个自定义对象),以检索与配置文件已更改的用户相关联的所有记录。(FRUP对象将包含所有用户在所有对象上创建的所有记录的列表,如Account、Opportunity) 3) 更新FRUP 为了实现这一点,我编写了一个触发器,通过它我能够获取其配置文件已更改的所有用户的列表,如下所示: Trigger UserProfileTrigger on User (before update) { List<U

我有以下要求:

1) 获取已更改个人资料的所有用户的列表

2) 然后查询FRUP(它是一个自定义对象),以检索与配置文件已更改的用户相关联的所有记录。(FRUP对象将包含所有用户在所有对象上创建的所有记录的列表,如Account、Opportunity)

3) 更新FRUP

为了实现这一点,我编写了一个触发器,通过它我能够获取其配置文件已更改的所有用户的列表,如下所示:

Trigger UserProfileTrigger on User (before update) {

List<User> usr = new List<User>();
Map<String,String> userMap = new Map<String,String>();

   for(User u: Trigger.new){

   //Create an old and new map so that we can compare values
        User oldOpp = Trigger.oldMap.get(u.ID);    
        User newOpp = Trigger.newMap.get(u.ID);

   //Retrieve the old and new profile            
        string oldProfileId = oldOpp.profileId;
        string newProfileId  = newOpp.profileId;

   //If the fields are different, the profile has changed
       if(oldProfileId != newProfileId){
          System.debug('Old:'+oldProfileId);
          System.debug('New :'+newProfileId);
          usr.add(u);
          System.debug('User :'+usr);

       }
   }

}
Trigger UserProfileTrigger on User(更新前){
List usr=新列表();
Map userMap=newmap();
for(用户u:Trigger.new){
//创建一个新旧地图,以便我们可以比较值
用户oldOpp=Trigger.oldMap.get(u.ID);
用户newOpp=Trigger.newMap.get(u.ID);
//检索旧的和新的配置文件
字符串oldProfileId=oldOpp.profileId;
字符串newProfileId=newOpp.profileId;
//如果字段不同,则说明配置文件已更改
if(oldProfileId!=newProfileId){
System.debug('Old:'+oldProfileId);
System.debug('New:'+newProfileId);
usr.add(u);
系统调试('User:'+usr);
}
}
}
以下是自定义对象FRUP上的字段: 1) 所有者 2) 名称
3) 记录ID
4) 文件夹ID
5) 创建人
6) 最后修改人


有什么帮助/建议吗?

我不确定FRUP上的哪个字段引用了与之相关的用户Id,但您可以使用以下内容循环FRUP对象:

List<FRUP> frupToUpdate = new List<FRUP>();
for (FRUP f : [
    Select
        OwnerId,
        Name, //etc.
        UserId //the field that references the user Id
    from FRUP
    where userId = : usr]) {
//update field on FRUP, e.g. f.Name = 'New Name';
frupToUpdate.add(f);
}
列表更新=新列表();
对于(FRUP f:[
挑选
所有者,
名称,//等。
UserId//引用用户Id的字段
来自FRUP
其中userId=:usr]){
//FRUP上的update字段,例如f.Name='新名称';
fruptupdate.add(f);
}
这将选择与配置文件已更改的用户相关的FRUP记录。然后,它更新记录上的一个字段,并将该记录添加到列表中进行更新。请注意,这应该在(用户u:Trigger.new)的循环
之后。然后更新已更改的FRUP记录:

更新frupToUpdate

Hi@Rudra,Salesforce.stackexchange.com上有一个专门针对Salesforce的新stackexchange站点。来加入那边的社区吧!:)