在 SFDC 中创建新的自定义对象时如何填充查找字段 [英] How to populate lookup field when creating a new custom object in SFDC

查看:50
本文介绍了在 SFDC 中创建新的自定义对象时如何填充查找字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这种情况下苦苦挣扎..我在 SFDC(机会)中有一个标准对象,它有一个指向用户对象的自定义查找字段,我想要做的是用名称填充该字段创建在商机布局中可用的自定义对象的用户...

I'm struggling with this situation.. I have a Standard Object in SFDC (Opportunity) that has a custom look up field pointing to the User object what I'm trying to do is populate this field with the name of the user that creates a custom object that is available in the Opportunity layout...

即新建 GOP 清单 --- 然后选择清单的类型 --- 然后填写所有必填字段并单击保存,这将指向机会视图.首先,这是可行的吗?我知道查找字段可能很棘手.我的第二个问题是以编程方式(触发)或使用工作流和字段更新功能执行此操作的最佳方法是什么?

i.e. New GOP Checklist --- Then choose the type of checklist--- and then fill all the required fields and click save, this is pointing back to the Opportunity view. To start with is this something doable ? i know that look up fields can be tricky. and my second question is what's the best way to do this Programatically (trigger) or using the workflow and field update functionality ?

谢谢!!

trigger TR_OrderChecklist on Order_Checklist__c (before insert) {

//----------------------------------------------------------------------------------
// Function 1: Update COS Operations Attribute in Opportunity
//----------------------------------------------------------------------------------

for(Order_Checklist__c o : trigger.new){
  if(o.Opportunity__r.CARE_Operations__c == null) {
    o.Opportunity__r.CARE_Operations__c = UserInfo.getUserId();
  }
}

}

这是他们想出来的.在标准机会对象中,我们有一个与用户相关的查找字段.. CARE_Operations__c.. 现在触发器应该做的是以下内容..

This is what they came up with. In the Standard Opportunity Object we have a lookup field tied to the user.. CARE_Operations__c.. Now what the trigger is supposed to do is the following..

1.- 在创建新的 GOP 清单时,如果用户在名为 COSOOperations_c 的 GOP 对象中填充新的自定义查找字段,然后保留该名称,2.- 如果用户未填充 COSOOperations_c 字段,但填充了 Opp 级别 CARE_Operations__c 中的字段,请使用该名称.3.- 如果 CARE_Operations_c 或 COSOOperations_c 均未填充(用户输入),则 COSOOperations__c 将成为创建 GOP 对象的人.

1.- When creating a new GOP Checklist if the user populate a new custom lookup field in the GOP object named COSOperations_c then keep that name, 2.- If the User didn't populate the COSOperations_c field but the field in the Opp level CARE_Operations__c is populated use that name. 3.- If neither CARE_Operations_c or COSOperations_c are populated (user input) then COSOperations__c is going to be the person that just create the GOP Object.

这就是我目前所拥有的..

This is what i have so far..

trigger TR_OrderChecklist on Order_Checklist__c (before insert) {
List<Opportunity> COS_Op = new List<Opportunity>();
COS_Op = [select CARE_Operations__c from Opportunity where id in (select   Opportunity__c from Order_Checklist__c where COSOperations__c != null)];
for(Order_Checklist__c OC : trigger.new) {
    if(OC.COSOperations__c != null) {
       break;}
    if(COS_Op != null){
       OC.COSOperations__c = OC.Opportunity__r.CARE_Operations__c;} 
    if(OC.COSOperations__c == null){
       OC.COSOperations__c = UserInfo.getUserId();}
}       
} 

我的问题是在第二个 if 语句中.. 其他 2 个条件工作正常..!有任何想法吗 ?谢谢!!!

My problem is in the second if statement.. the other 2 conditions are working properly.. ! Any ideas ? Thanks !!!

推荐答案

我(第二次)修复您发布的触发代码:

My (second) take on fixing up the trigger code you posted:

trigger TR_OrderChecklist on Order_Checklist__c (after update) {
    List<Opportunity> opptsToUpdate = new List<Opportunity>();
    for(Order_Checklist__c o : trigger.new) {
        if(o.Opportunity__r.CARE_Operations__c == null) {
            o.Opportunity__r.CARE_Operations__c = UserInfo.getUserId();
            // Queue it up for one update statement later
            opptsToUpdate.add(o.Opportunity__r);
        }
    }
    // Commit any changes we've accumulated for the opportunity records
    if (opptsToUpdate.size() > 0)
        update opptsToUpdate;
}

<小时>

假设你有 Opportunity.My_User__c 作为 lookup(User) 和 My_Object__c.Opportunity__c 作为 lookup(Opportunity) 这个触发器是一个好的开始:


Assuming you have Opportunity.My_User__c as lookup(User) and My_Object__c.Opportunity__c as lookup(Opportunity) this trigger is a good start:

trigger HandleMyObjectInsert on My_Object__c (before insert) {
    User actingUser = [SELECT id FROM User WHERE Id = :UserInfo.getUserId()];
    List<Opportunity> oppts = new List<Opportunity>();
    for (My_Object__c myobj : trigger.new) {
        Opportunity o = new Opportunity();
        o.Id = myobj.Opportunity__c;
        o.My_User__c = actingUser.Id;
        oppts.add(o);
    }
    update oppts;
}

为了证明 Lookup(User) 实际上只是一个 Id 字段,试试这个练习.在名为My User"的 Opportunity 对象上创建一个新的 Lookup(User) 字段.

For proof that Lookup(User) is really just an Id field, try out this exercise. Create a new Lookup(User) field on the Opportunity object named "My User."

  • 数据类型:查找关系
  • 相关:用户
  • 字段标签:我的用户
  • 字段名称:My_User(变成 My_User__c,也可以通过 My_User__r 访问)

从 Salesforce 网页中,选择现有机会记录并将我的用户字段设置为某个随机用户并保存记录.现在从开发者控制台,执行这个匿名 APEX:

From the Salesforce web page, pick an existing opportunity record and set the My User field to some random user and save the record. Now from the Developer Console, execute this anonymous APEX:

Opportunity[] oppts = [
    SELECT id, My_User__c, My_User__r.Name
    FROM Opportunity
    WHERE My_User__c != null
];
for (Opportunity o : oppts) {
    system.debug('##### Opportunity.My_User__c = ' 
        + o.My_User__c 
        + ', o.My_User__r.Name = ' 
        + o.My_User__r.Name);
}

关闭花哨的日志视图并单击打开原始日志",您将看到如下一行:

Close the fancy log view and click the "Open Raw Log" and you'll see a line like this:

16:42:37.077 (77645000)|USER_DEBUG|[7]|DEBUG|##### Opportunity.My_User__c = 00530000000grcbAAA, o.My_User__r.Name = John Doe

看,Salesforce 将 __c 查找字段视为 Id 字段.在这种情况下,它是与 User 对象的 Id 的外键关系.Salesforce 让您认为 Name 字段是主键,但实际上它是 Id 字段(好吧,我实际上没有看到 Salesforce 的 ERD,但我很确定这是正确的).请注意,您可以通过 __r 构造访问查找对象的字段.

See, Salesforce thinks of __c lookup fields as Id fields. In this case, it's a Foreign Key relationship to the Id of the User object. Salesforce makes you think the Name field is the Primary Key but really it's the Id field (ok I haven't actually seen Salesforce's ERD, but I'm pretty sure that's correct). Notice that you can get to the lookup object's fields through the __r construct.

更新字段只是更改 My_User__c id 的问题:

Updating the field is just a matter of changing the My_User__c id:

Opportunity[] oppts = [
    SELECT id, My_User__c
    FROM Opportunity
    WHERE My_User__c != null
    LIMIT 1
];
for (Opportunity o : oppts) {
    o.My_User__c = :UserInfo.getUserId();
}
update oppts;

或者,您可以从这样的 soql 查询中获取用户 ID:

Or, you can get the User Id from a soql query like this:

// Let's query all users and use the 3rd one in the list (zero-index 2)
User[] users = [select id from user];
Opportunity o = new Opportunity(My_User__c = users[2].id, Name='Testing', StageName = 'Prospecting', CloseDate = System.today());
insert o;

或者,通过电子邮件地址查找用户:

Or, look up a user by their email address:

User[] users = [select id from user where email = 'first.last@company.com' and IsActive = true];
if (users.size() > 0) {
    Opportunity o = new Opportunity(My_User__c = users[0].id, Name='Testing', StageName = 'Prospecting', CloseDate = System.today());
    insert o;
}

这只是一个非常好的获取当前用户 ID 的方法:

This is just a really good way to get the Id of the current user:

UserInfo.getUserId()

这篇关于在 SFDC 中创建新的自定义对象时如何填充查找字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆