Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Salesforce 第一个错误:分配给ID:owner的交叉引用密钥无效:[OwnerId]不能为空_Salesforce_Apex Code_Apex_Soql - Fatal编程技术网

Salesforce 第一个错误:分配给ID:owner的交叉引用密钥无效:[OwnerId]不能为空

Salesforce 第一个错误:分配给ID:owner的交叉引用密钥无效:[OwnerId]不能为空,salesforce,apex-code,apex,soql,Salesforce,Apex Code,Apex,Soql,我正在为我的控制器编写一个测试。为此,我必须在测试数据库中插入一个事件 我的测试方法是: 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

我正在为我的控制器编写一个测试。为此,我必须在测试数据库中插入一个事件

我的测试方法是:

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 = 'newuser@testorg.com', EmailEncodingKey = 'UTF-8', LastName = 'Testing',
    LanguageLocaleKey = 'en_US', LocaleSidKey='America/Los_Angeles', UserName='newuser@testorg.com', 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;
    ...........
}
发生insert事件时,我面临以下错误:

System.DmlException:插入失败。第0行的第一个异常;第一个错误:分配给ID:owner的交叉引用密钥无效:[OwnerId]不能为空


如何更正此错误?

您忘记在
event.OwnerId=u2.Id行之前插入
u2

作为第一个选项,您可以插入测试用户:

 @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 ='newuser123@testorg.com',
                            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='newuser123@testorg.com',
                            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()

请回答这个问题
 @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 ='newuser123@testorg.com',
                            EmailEncodingKey = 'UTF-8',
                            LastName = 'Testing',
                            LanguageLocaleKey='en_US',
                            LocaleSidKey='en_US', 
                            UserName='newuser123@testorg.com',
                            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;
         }
     }

 }