2013-02-14 13 views
2

我在爲我的控制器寫一個測試。爲此,我必須在測試數據庫中插入一個事件。第一個錯誤:INVALID_CROSS_REFERENCE_KEY,分配給ID:所有者不能爲空:[OwnerId]

我的測試方法是:

static TestMethod void Test1_TestInsertWithValue() 
{ 
    Meeting_Master__c master = new Meeting_Master__c(); 
    Event event = new Event(); 
    Profile p = [SELECT Id From Profile WHERE Name='Standard User']; 
    User u2 = new User(Alias = 'newUser', Email = '[email protected]', EmailEncodingKey = 'UTF-8', LastName = 'Testing', 
    LanguageLocaleKey = 'en_US', LocaleSidKey='America/Los_Angeles', UserName='[email protected]', ProfileId=p.Id); 

    event.OwnerId = u2.Id; 
    event.StartDateTime = datetime.newInstance(2008, 12, 1); 
    event.EndDateTime = datetime.newInstance(2008, 12, 30); 
    event.subject = 'call'; 
    event.WhatId = master.Id; 
    insert master; 
    insert event; 
    ........... 
} 

當插入事件發生時,我對着這個錯誤:

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Assigned To ID: owner cannot be blank: [OwnerId]

如何糾正這個錯誤?

回答

2

正如你可以插入測試用戶的第一個選項:

@isTest 
private class test{ 

    static TestMethod void Test1_TestInsertWithValue() { 
     Meeting_Master__c master=new Meeting_Master__c(); 
     Event event =new Event(); 
     Profile p=[SELECT Id From Profile WHERE Name='Standard User']; 
     User u2 =new User(Alias = 'newUser1' , 
          Email ='[email protected]', 
          EmailEncodingKey = 'UTF-8', 
          LastName = 'Testing', 
          LanguageLocaleKey='en_US', 
          LocaleSidKey='en_US', // changed for to avoid: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, Locale: bad value for restricted picklist field: America/Los_Angeles 
          UserName='[email protected]', 
          ProfileId=p.Id, 
          TimeZoneSidKey = 'America/Los_Angeles'); 
     insert u2; 

     event.OwnerId = u2.Id; 
     event.StartDateTime = datetime.newInstance(2008, 12, 1); 
     event.EndDateTime = datetime.newInstance(2008,12,10); // changed to 10-12-2008 for to avoid: FIELD_INTEGRITY_EXCEPTION, Event duration can not be longer than 14 days 
     event.subject='call'; 
     event.WhatId=master.Id; 
     Insert master; 
     insert event; 
    } 

} 

第二個選項是試圖使用System.runAs() http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm

@isTest 
private class test{ 

    static TestMethod void Test1_TestInsertWithValue() { 
     Meeting_Master__c master=new Meeting_Master__c(); 
     Event event =new Event(); 
     Profile p=[SELECT Id From Profile WHERE Name='Standard User']; 
     User u2 =new User(Alias = 'newUser1' , 
          Email ='[email protected]', 
          EmailEncodingKey = 'UTF-8', 
          LastName = 'Testing', 
          LanguageLocaleKey='en_US', 
          LocaleSidKey='en_US', 
          UserName='[email protected]', 
          ProfileId=p.Id, 
          TimeZoneSidKey = 'America/Los_Angeles'); 

     Insert master; 

     System.runAs(u2) { 
      event.StartDateTime = datetime.newInstance(2008, 12, 1); 
      event.EndDateTime = datetime.newInstance(2008,12,10); 
      event.subject='call'; 
      event.WhatId=master.Id; 

      insert event; 
     } 
    } 

} 
+0

請回復此查詢http://stackoverflow.com /問題/ 14912599 /解析JSON的對象功能於銷售人員,先端 – 2013-02-17 13:04:50

4

您忘記了在行event.OwnerId =u2.Id;之前插入u2

相關問題