0

私はこの状況に苦しんでいます。SFDC(Opportunity)に標準オブジェクトがあり、ユーザーオブジェクトを指すカスタムルックアップフィールドがあります。このフィールドに、作成するユーザーの名前を入力します。 Opportunityレイアウトで使用可能なカスタムオブジェクト...

つまり、新しいGOPチェックリスト---次に、チェックリストのタイプを選択します---次に、すべての必須フィールドに入力して[保存]をクリックします。これは、オポチュニティビューに戻ります。そもそもこれは実行可能なことですか?フィールドの検索には注意が必要です。そして私の2番目の質問は、これをプログラムで(トリガー)またはワークフローとフィールド更新機能を使用して行うための最良の方法は何ですか?

ありがとう !!

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();
  }
}

}

これが彼らが思いついたものです。Standard Opportunity Objectには、ユーザーに関連付けられたルックアップフィールドがあります。CARE_Operations__c..これで、トリガーが実行することになっているのは次のとおりです。

1.-新しいGOPチェックリストを作成するときに、ユーザーがCOSOperations_ cという名前のGOPオブジェクトに新しいカスタムルックアップフィールドを入力した場合は、その名前を保持します。2.-ユーザーがCOSOperations _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();}
}       
} 

私の問題は2番目のifステートメントにあります..他の2つの条件は正しく機能しています..!何か案は ?ありがとう !!!

4

2 に答える 2

0

私はここで要件と少し混乱しています。カスタムオブジェクトのレコードを作成するときに、新しいオポチュニティレコードを作成する必要があると言っていますか?

もしそうなら、それは可能です。「挿入後」タイプの得意先オブジェクトにAPEXトリガーを記述し、新しい商談レコードを作成して、必要に応じてそのフィールドに入力することができます。

于 2012-08-29T19:37:55.100 に答える
0

私の(2番目の)あなたが投稿したトリガーコードの修正に取り組みます:

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

Lookup(User)としてOpportunity.My_User__cがあり、lookup(Opportunity)としてMy_Object__c.Opportunity__cがあるとすると、このトリガーは良いスタートです。

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 Webページから、既存の商談レコードを選択し、[マイユーザー]フィールドをランダムユーザーに設定して、レコードを保存します。開発者コンソールから、次の匿名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);
}

ファンシーログビューを閉じて[未加工ログを開く]をクリックすると、次のような行が表示されます。

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では、[名前]フィールドが主キーであると思われますが、実際には[ID]フィールドです(SalesforceのERDは実際には見ていませんが、正しいと確信しています)。__r構造を介してルックアップオブジェクトのフィールドにアクセスできることに注意してください。

フィールドの更新は、My_User__cidを変更するだけです。

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()
于 2012-08-30T04:08:41.290 に答える