4

我有一个自定义对象MyCustomObj__c,它有一个名为“ContactData”的字段。

我正在尝试使用以下顶点方法将记录插入到自定义对象中。它给出了一个错误:

Invalid ID

该值Hari已存在于联系人列表中。

顶点代码:

public static String saveData(){
    MyCustomObj__c newObj = new MyCustomObj__c(); 
    newObj.contactData__c = 'Hari';
    insert newObj;
    return "success";
}

如何插入一行?

4

3 回答 3

5

你应该通过联系人记录'Hari'的ID

 public static String saveData(){
       MyCustomObj__c newObj = new MyCustomObj__c(); 
       newObj.contactData__c = [SELECT Id 
                                FROM Contact 
                                WHERE Name ='Hari' LIMIT 1].Id;
       insert newObj;
       return "success";
 }

当然,你应该尽量避免使用 SOQL,这只是一个例子。

于 2013-11-24T07:52:51.500 回答
0

将数据插入对象的 Apex 代码:

static void testInsert(){
    User testUser = new User(username = 'joebieber@hotmail.com', 
    firstname = 'joe', lastname = 'bieber', 
    email = 'joebieber@hotmail.com', Alias = 'jo', 
    CommunityNickname = 'jo', 
    TimeZoneSidKey = 'America/New_York', LocaleSidKey = 'en_US', 
    EmailEncodingKey = 'ISO-8859-1', 
    ProfileId = '00e6000000167RPAAY', 
    LanguageLocaleKey = 'en_US');
    insert testUser;

    List<SObject> sos = Database.query('select username from User');

    List<User> users= new List<User>();
    if( sos != null ){
       for(SObject s : sos){
          if (((User)s).get('username') == 'joebieber@hotmail.com')
            Utils.log('' + ((User)s).get('username') );
       }
    }
}

应该打印您刚刚添加的用户的用户名。

于 2014-12-11T21:39:13.023 回答
0

我们可以使用数据加载器将数据移动到数据库中查找字段 Guru

于 2015-06-11T15:18:00.607 回答