How to insert data into a custom object using the search field from the vertex controller

I have a custom object MyCustomObj__cthat has a ContactData field.

I am trying to insert a record into a user object with the following apex method. This gives an error:

Invalid ID

The value Harialready exists in the contact list.

apex code:

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

How to insert a row?

+4
source share
3 answers

you should pass the identifier of the Hari contact record

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

Of course, you should try to avoid SOQL here, this is just an example.

+5
source

Apex code to insert data into an object:

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

.

0

We can use the data loader to move data to the database for the Guru search field

0
source

All Articles