【问题标题】:How to populate lookup field when creating a new custom object in SFDC在 SFDC 中创建新的自定义对象时如何填充查找字段
【发布时间】:2012-08-29 17:41:35
【问题描述】:

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

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

谢谢!!

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.. 现在触发器应该做的事情如下..

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

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

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 个条件正常工作.. !有任何想法吗 ?谢谢!!!

【问题讨论】:

  • @Twanley 嗨,Twanley。现在要求不同了..请再次检查我的问题,感谢您的所有反馈..!

标签: triggers salesforce apex-code lookup


【解决方案1】:

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

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),这个触发器是一个好的开始:

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 字段,试试这个练习。在 Opportunity 对象上创建一个名为“我的用户”的新 Lookup(User) 字段。

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

从 Salesforce 网页中,选择现有机会记录并将我的用户字段设置为某个随机用户并保存记录。现在从开发者控制台执行这个匿名 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);
}

关闭花哨的日志视图并点击“Open Raw Log”,你会看到这样一行:

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 构造访问查找对象的字段。

更新字段只需更改 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:

// 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;

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

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 的一个非常好的方法:

UserInfo.getUserId()

【讨论】:

  • 嗨 twanley.. 在这种情况下,您将 SOQL 语句中的 ID 分配给 o.My_User__c 但我如何将相同的 id 值分配给查找字段?这是可行的吗?如果你能告诉我我将如何真正感激。 !谢谢
  • 我想如果你打开引擎盖,你会发现 Lookup 字段确实是 Id 字段。我提供了一个教程来帮助说明这一点。如果您仍然遇到困难,很乐意提供帮助。
  • 嗨 Twamley... 这是我写的触发器.. 它不起作用.. !!我不知道为什么..! :S
  • @JeyJim,抱歉,您的更改被拒绝了。请使用 cmets 或更新您的原始问题添加您想说的额外内容。编辑其他人的答案来表达你的想法并不受欢迎。
  • @twamley 请检查我原来问题中的触发器,让我知道我在这里做错了什么.. !!谢谢!!
【解决方案2】:

我对这里的要求有点困惑。 我们是说在创建自定义对象的记录时需要创建新的商机记录吗?

如果是的话,这是可能的。您可以在“插入后”类型的客户对象上编写 APEX 触发器,并创建一个新的机会记录并根据需要填充其字段。

【讨论】:

  • 嗨,Richard.. 我要做的是尽快在这种情况下生成一个新的自定义对象(GOP 清单),我想填充生成 GOP 清单的人的姓名属于商机对象的一个​​自定义字段(与用户对象相关的查找字段)是否可行?谢谢!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
相关资源
最近更新 更多