简体   繁体   English

Apex更新触发器 - 更新查找字段

[英]Apex update trigger - update lookup field

I'm trying to write my first Apex update trigger and struggling slightly as I'm trying to set the value of a custom lookup field. 我正在尝试编写我的第一个Apex更新触发器,并在我尝试设置自定义查找字段的值时稍微挣扎。

I have an object called Deployment__c which is the master in a master-detail relationship with an object called Service__c (names simplified!) 我有一个名为Deployment__c的对象,它是一个主 - 细节关系中的主对象,它与一个名为Service__c的对象(名称简化!)

The Service__c field has no Owner in Salesforce as it is the child/detail in a master-detail relationship. Service__c字段在Salesforce中没有Owner ,因为它是主 - 详细信息关系中的子/详细信息。 Therefore we have created a pseudo-owner field called Owner__c on this object, which is a lookup field looking up the User object in Salesforce. 因此,我们在此对象上创建了一个名为Owner__c的伪所有者字段,该字段是查找Salesforce中User对象的查找字段。

The aim of the trigger is to set the value of Service__c.Owner__c to that of the user who is the owner of Deployment__c , if there is no Owner__c already set on Service__c . 触发器的目的是将Service__c.Owner__c的值设置为Deployment__c所有者的用户的值,如果Service__c上没有设置Owner__c

The code I have is as follows: 我的代码如下:

trigger AfterServiceUpdate on Service__c (before update) {
    for (Service__c oldService : Trigger.new) {
        if (oldService.Owner__c == null) {
            User defaultUser = [Select Id FROM User WHERE User.Id = :oldService.Deployment__r.OwnerId];
            oldService.Owner__c = defaultUser.Id;
        }
    }
}

However, I get an error when I try to save this trigger: 但是,当我尝试保存此触发器时出现错误:

Error: Compile Error: Invalid field Owner__c for SObject ASE_Deployment__c at line 5 column 13 错误:编译错误:第5行第13列的SObject ASE_Deployment__c的字段Owner__c无效

I've verified that I have the field name correct, but can't get this to work. 我已经确认我的字段名称是正确的,但无法使其正常工作。 Any pointers welcome! 欢迎任何指示!

I just recreated that object structure you described in a test org and was unable to reproduce the error you got. 我刚刚重新创建了您在测试组织中描述的对象结构,但无法重现您所获得的错误。 On another note I was able to get the trigger you described to work with the following code which has been bulkified for you: 另一方面,我能够获得您描述的触发器以使用以下代码,该代码已经为您批量化:

trigger AfterServiceUpdate on Service__c (before update) 
{
    //instantiate set to hold unique deployment record ids
    Set<Id> deplomentIds = new Set<Id>();
    for(Service__c s : Trigger.new)
    {
        deplomentIds.add(s.Deployment__c);
    }

    //instantiate map to hold deployment record ids to their corresponding ownerids
    Map<Id, Deployment__c> deploymentOwnerMap = new Map<Id, Deployment__c>([SELECT Id, OwnerId FROM Deployment__c WHERE Id IN: deplomentIds]);

    for (Service__c s : Trigger.new) 
    {
        if (s.Owner__c == null && deploymentOwnerMap.containsKey(s.Deployment__c)) 
        {
            s.Owner__c = deploymentOwnerMap.get(s.Deployment__c).OwnerId;
        }
    }
}

I also created a test class for you to test the result: 我还为您创建了一个测试类来测试结果:

@isTest
private class AfterServiceUpdateTest {

    @isTest static void test_method_one() {
        Deployment__c d = new Deployment__c();
        insert d;

        Service__c s = new Service__c();
        s.Deployment__c = d.Id;
        insert s;

        Id deploymentOwnerId = [SELECT Id, OwnerId FROM Deployment__c WHERE Id=:d.Id].OwnerId;

        test.startTest();

        update s;

        Service__c updatedService = [SELECT Id, Owner__c FROM Service__c WHERE Id=:s.Id];

        system.assertEquals(deploymentOwnerId, updatedService.Owner__c);

        test.stopTest();
    }

}

Hope this helps! 希望这可以帮助!

尝试将owner__c更改为Ownerld

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM