Triggers 在SFDC中创建新自定义对象时如何填充查找字段
我正在与这种情况作斗争。。我在SFDC Opportunity中有一个标准对象,它有一个指向用户对象的自定义查找字段。我尝试用创建Opportunity布局中可用的自定义对象的用户的名称填充此字段 i、 e.新建GOP检查表,然后选择检查表的类型——然后填写所有必填字段并单击保存,这将指向Opportunity视图。首先,这是可行的吗?我知道查找字段可能很棘手。 我的第二个问题是,通过编程触发或使用工作流和字段更新功能来实现这一点的最佳方式是什么 谢谢Triggers 在SFDC中创建新自定义对象时如何填充查找字段,triggers,salesforce,apex-code,lookup,Triggers,Salesforce,Apex Code,Lookup,我正在与这种情况作斗争。。我在SFDC Opportunity中有一个标准对象,它有一个指向用户对象的自定义查找字段。我尝试用创建Opportunity布局中可用的自定义对象的用户的名称填充此字段 i、 e.新建GOP检查表,然后选择检查表的类型——然后填写所有必填字段并单击保存,这将指向Opportunity视图。首先,这是可行的吗?我知道查找字段可能很棘手。 我的第二个问题是,通过编程触发或使用工作流和字段更新功能来实现这一点的最佳方式是什么 谢谢 trigger TR_OrderCheck
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();
}
}
}
这就是他们想到的。在标准Opportunity对象中,我们有一个与用户关联的查找字段。。注意操作。。现在,触发器应该执行以下操作
1.-创建新的GOP检查表时,如果用户在名为COSU c的GOP对象中填充新的自定义查找字段,则保留该名称,
2.-如果用户未填写COSU Operations__c字段,但填写了Opp级别的CARE_Operations___c字段,则使用该名称。
3.-如果CARE_Operations_c或COSOperations_c均未填充用户输入,则COSOperations__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个条件工作正常!有什么想法吗?谢谢 我对这些要求有点困惑。 我们是说在创建自定义对象的记录时需要创建新的Opportunity记录吗
如果是的话,这是可能的。您可以在插入后在类型的customer对象上写入APEX触发器,并创建新的Opportunity记录,并根据需要填充其字段 我的第二个想法是修复你发布的触发代码:
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;
}
关闭fancy log视图并单击Open Raw log,您将看到一行如下所示:
16:42:37.077 (77645000)|USER_DEBUG|[7]|DEBUG|##### Opportunity.My_User__c = 00530000000grcbAAA, o.My_User__r.Name = John Doe
// 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;
请参阅,Salesforce将_____c查找字段视为Id字段。在本例中,它是与用户对象Id的外键关系。Salesforce让你认为Name字段是主键,但实际上它是Id字段好的,我还没有看到Salesforce的ERD,但我很确定这是正确的。请注意,您可以通过ur构造访问查找对象的字段
更新字段只需更改My_User_uC 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,如下所示:
16:42:37.077 (77645000)|USER_DEBUG|[7]|DEBUG|##### Opportunity.My_User__c = 00530000000grcbAAA, o.My_User__r.Name = John Doe
// 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()
嗨,理查德。。我想做的是尽快生成一个新的自定义对象在这种情况下GOP检查表我想在一个自定义字段中填充生成GOP检查表的人的姓名,该自定义字段属于与用户对象关联的Opportunity Object Lookup字段这可行吗??谢谢嗨,特温利。。在这种情况下,您正在将SOQL语句中的ID分配给o.My_User_u_uc,但如何将相同的ID值分配给查找字段??这可行吗?如果你能告诉我,我会非常感激的!谢谢,我想如果你打开引擎盖,你会发现查找字段实际上是Id字段。我提供了各种各样的教程来帮助说明这一点。很高兴能帮你,如果你还被困的话。嗨,特姆利。。。这是我写的触发器。。它不起作用!!我不知道为什么…:S@JeyJim,抱歉,您的更改被拒绝。请使用评论或更新原始问题添加您想说的额外内容。编辑其他人的答案来表达你的想法是不受欢迎的。@twamley请检查我原始问题中的触发器,并让我知道我在这里做错了什么!!谢谢@特恩利你好,特恩利。现在要求不同了。。请再次检查我的问题,感谢您的反馈!