在子广告系列上提供目标的顶点触发器会在父广告系列上生成错误

时间:2014-07-29 11:27:09

标签: salesforce apex-code apex

我遇到一个触发器问题。我试图建立解决方案,这使我有可能为广告系列分配灵活的目标。我试图通过公式字段解决它,但它太复杂了,我被告知它需要是顶点触发器。我创建了列表类型的自定义设置,然后我可以在顶点触发器中查询它。我创建了" Apex_Calculator__c"合并两个字段的值的字段(事件区域[位于父级广告系列级别]和已开始广告系列[位于子广告系列级别])。来自" Apex_Calculator__c"的值字段即将带来与自定义设置中相同的值。

我对Apex触发器的了解有限,所以我在互联网上寻求帮助。我收到了帮助,触发器在子广告系列中引入了目标值。但是,问题发生在父级广告系列级别上。至于我想要更改事件区域(正如我提到的那样是" Apex_Calculator__c"公式字段的一部分),我开始收到错误消息:

*

  

PopulateTarget:由以下原因导致的BeforeUpdate执行:   System.NullPointerException:尝试取消引用null对象:   Trigger.PopulateTarget:第18行,第1列

*

trigger PopulateTarget on Campaign (before insert, before update)
{
Map <String, Id> recordTypes = new Map <String, Id> ();

for (RecordType recordType : [SELECT Id, DeveloperName FROM RecordType WHERE sObjectType = 'Campaign'])
{
    if (recordType.DeveloperName == 'Conference')
    {
        recordTypes.put(recordType.DeveloperName, recordType.Id);
    }
}

for(Campaign campaign : Trigger.new)
{
    // Make sure that the campaign record type is not in the map (in the map we keep the ones that we want to exclude)
    if (recordTypes.get(campaign.RecordTypeId) == null && String.isNotBlank(campaign.Apex_Calculator__c) == true)
    {
        String target = DSTargets__c.getInstance(campaign.Apex_Calculator__c).Target__c;
        campaign.DS_Target_Multiplier__c = Target;
    }
}
}

我觉得这里真的输了什么错。任何人都可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:0)

您似乎没有为某些campaign.Apex_Calculator__c值设置DSTargets自定义设置。

试试这个:

    // Make sure that the campaign record type is not in the map (in the map we keep the ones that we want to exclude)
if (recordTypes.get(campaign.RecordTypeId) == null && String.isNotBlank(campaign.Apex_Calculator__c) == true)
{

    if (DSTargets__c.getInstance(campaign.Apex_Calculator__c) == null)
    {
        System.debug('### A DSTargets named \'' + campaign.Apex_Calculator__c + '\' doesn't exist');
        continue;
    }


    String target = DSTargets__c.getInstance(campaign.Apex_Calculator__c).Target__c;
    campaign.DS_Target_Multiplier__c = Target;
}