Triggers 尝试从元数据更新标准对象中的值

Triggers 尝试从元数据更新标准对象中的值,triggers,salesforce,metadata,apex,apex-code,Triggers,Salesforce,Metadata,Apex,Apex Code,我正在使用触发器isBefore 在System.debug(opp.get(metaData.get(0).Opportunity\u Field\u Name\u c)中,它在Opportunity对象中显示正确的值,但不更新 下面是触发器及其Apex类触发器 trigger MetadataObjectFieldMapping on Opportunity (before insert, before update) { if(Trigger.isInsert || Trigge

我正在使用触发器
isBefore

System.debug(opp.get(metaData.get(0).Opportunity\u Field\u Name\u c)
中,它在
Opportunity对象中显示正确的值,但不更新

下面是
触发器
及其
Apex类触发器

trigger MetadataObjectFieldMapping on Opportunity (before insert, before update) 
{
    if(Trigger.isInsert || Trigger.isUpdate )
    {
        MetadataObjectFieldMappingHandler oppHandler = new MetadataObjectFieldMappingHandler();
        oppHandler.Show(Trigger.new);
    }
}
触发器

trigger MetadataObjectFieldMapping on Opportunity (before insert, before update) 
{
    if(Trigger.isInsert || Trigger.isUpdate )
    {
        MetadataObjectFieldMappingHandler oppHandler = new MetadataObjectFieldMappingHandler();
        oppHandler.Show(Trigger.new);
    }
}
Apex类

public class MetadataObjectFieldMappingHandler {
    List<String> strAccField = new List<String>();
    //Getting List of MetaData Values
    List<Object_Field_Mapping__mdt> metaData = new List<Object_Field_Mapping__mdt>
                                                        ([SELECT Account_Field_Name__c,
                                                             Opportunity_Field_Name__c 
                                                                 FROM Object_Field_Mapping__mdt]);
    //Function to check if Field Name Exists in Object or not
    public Boolean hello(String objName, String fieldName)
    {
        Boolean temp = False;
        //Creating Schema to get all fields from Account and Opportunity Object
        Map<String, Schema.SObjectField> accFields = Schema.getGlobalDescribe().get(objName).getDescribe().fields.getMap();     
        for(Schema.SObjectField field : accFields.values())
        {
                    strAccField.add(field+'');
        }
        //Calling Account and Opportunity Object in fieldName
        if(strAccField.contains(fieldName)){
            System.debug('PASS '+fieldName);
            temp = true;
        }
        return temp;
    }
    public void Show(List<opportunity> newOppList)
    {       
        Boolean test1 = hello('Account',metaData.get(0).Account_Field_Name__c);
        Boolean test2 = hello('Opportunity',metaData.get(0).Opportunity_Field_Name__c);
        //If both Field Value exists
        if(test1 && test2){
            //Getting value from Opp using dynamic Query
            String query = 'Select Account.'+metaData.get(0).Account_Field_Name__c+', '+metaData.get(0).Opportunity_Field_Name__c+' from Opportunity where Id IN : newOppList ';
            List<Opportunity> oppList =database.query(query);
            for(Opportunity opp : oppList){
                opp.put(
                    metaData.get(0).Opportunity_Field_Name__c, 
                    opp.Account.get(metaData.get(0).Account_Field_Name__c)
                );
                System.debug(opp.get(metaData.get(0).Opportunity_Field_Name__c));
            }
        }
}
公共类MetadataObjectFieldMappingHandler{
List strAccField=新列表();
//获取元数据值列表
列表元数据=新列表
([选择帐户\字段\名称\ c,
机会\字段\名称\ c
从对象_字段_映射__mdt]);
//函数检查对象中是否存在字段名
公共布尔hello(字符串对象名、字符串字段名)
{
布尔温度=假;
//创建架构以从Account和Opportunity对象获取所有字段
Map accFields=Schema.getGlobalDescripte().get(objName.getDescripte().fields.getMap();
对于(Schema.SObjectField字段:accFields.values())
{
strAccField.add(字段+“”);
}
//调用fieldName中的Account和Opportunity对象
if(strAccField.contains(fieldName)){
系统调试('PASS'+字段名);
温度=真;
}
返回温度;
}
公共无效显示(列表newOppList)
{       
布尔test1=hello('Account',metaData.get(0).Account\u Field\u Name\u c);
布尔test2=hello('Opportunity',metaData.get(0).Opportunity\u字段\u名称\u c);
//如果两个字段值都存在
if(test1和test2){
//使用动态查询从Opp获取值
字符串查询='Select Account.'+metaData.get(0).Account_Field_Name_c+','metaData.get(0).Opportunity_Field_Name_c+'来自Opportunity,其中Id位于:newOppList';
List=database.query(查询);
for(Opportunity opp:oppList){
反对票(
metaData.get(0).Opportunity\u字段\u名称\u\c,
opp.Account.get(元数据.get(0).Account\u字段\u名称\u c)
);
调试(opp.get(metaData.get(0.Opportunity)字段名称c));
}
}
}

您能告诉我,为什么在
调试日志中显示时,
Opportunity对象中的值没有更新?

我认为这里多次失败

如果('a'='a')
,则Apex不区分大小写。但是当比较集合(列表、集合、映射键)中的字符串时,它突然变得区分大小写

List<String> fields = new List<String>{'Id', 'Name'};
System.debug(fields.contains('name')); // false
List fields=新列表{'Id','Name'};
System.debug(fields.contains('name');//false
(顺便说一句,为了性能和逻辑可读性,这应该是一个
集)。因此,我怀疑这里有些可疑。您没有显示元数据,但检查了这一行(您现在只有一行,对吗?如果您有多行,我们将使用
元数据。get(0)
基本上返回一个随机行)

我也不喜欢从
Schema.SObjectField
转换为字符串


下一步:
String query='Select Account.+metaData.get(0).Account.\u Field.\u Name.\u c+','+metaData.get(0).Opportunity.\u Field.\u Name.\u c+'from Opportunity where Id IN:newOppList';

这有可能在更新之前在
中工作
。但请确保在插入之前在
中不工作
。数据库中还没有任何内容,您的查询将返回零结果。您必须循环执行trigger.new、collec accountid、query Accounts(直接,而不是通过Opportunity表),然后执行写入数据的最终循环


您已将
newOppList
传递给
Show()
。如果您想免费保存到数据库,您应该在
newOppList
上修改原始的值。而不是修改内存中的查询结果(
oppList
)。不会发生任何事情,它们将被丢弃。如果要保存它们,则必须手动保存(但这样会有进入更新触发器循环的风险,SF将阻止您)



您确定这必须是代码吗?听起来像是工作流或process builder的工作。或者将其设置为公式字段,以便始终显示新值,而不是这样的复制…当某些内容因故发生更改时,它不会自动级联到所有OPP,除非您执行下一个触发器/process builder…

我认为这里多次失败

如果('a'='a')
,则Apex不区分大小写。但是当比较集合(列表、集合、映射键)中的字符串时,它突然变得区分大小写

List<String> fields = new List<String>{'Id', 'Name'};
System.debug(fields.contains('name')); // false
List fields=新列表{'Id','Name'};
System.debug(fields.contains('name');//false
(顺便说一句,为了性能和逻辑可读性,这应该是一个
集)。因此,我怀疑这里有些可疑。您没有显示元数据,但检查了这一行(您现在只有一行,对吗?如果您有多行,我们将使用
元数据。get(0)
基本上返回一个随机行)

我也不喜欢从
Schema.SObjectField
转换为字符串


下一步:
String query='Select Account.+metaData.get(0).Account.\u Field.\u Name.\u c+','+metaData.get(0).Opportunity.\u Field.\u Name.\u c+'from Opportunity where Id IN:newOppList';

这有可能在更新之前在
中工作
。但请确保在插入之前在
中不工作
。数据库中还没有任何内容,您的查询将返回零结果。您必须循环执行trigger.new、collec accountid、query Accounts(直接,而不是通过Opportunity表),然后执行写入数据的最终循环


您已将
newOppList
传递到
Show()
。如果要免费保存到数据库,应修改