3

Is it possible to upsert only a single object? And return the ID of the inserted/updated record?

I have an Custom Contact Object which i want to insert into the standard Object from Salesforce. The custom contact object is:

global class CContact 
{
    public String changed {get; set;}
    public String contactId {get; set;}
    public String customerNumber {get; set;}
    public String email {get; set;}
    public String firstName  {get; set;}
    public String lastName {get; set;}
    public String phoneCountryCode {get; set;}
    public String phoneNumber {get; set;}
    public String pointBalance {get; set;}
    public String contactUrl {get; set;}
}

What am i doing wrong below? My upsert logic:

Contact[] cts;
string searchQuery = 'SELECT id, firstName FROM contact WHERE Membership_number__c = 123456789';
cts = Database.query(searchQuery);
System.debug('Cts length: ' + cts.size()); (Returns 1)
cts[0] = new Contact(phone = '555', email = ''); //Just hardcoding a new value here!
Schema.SObjectField f = Contact.Fields.Membership_number__c;
Database.UpsertResult [] cr = Database.upsert(cts, f, false);
cr[0].getId(); //This returns the ID of inserted/updated record ((Returns null now)

My upsert logic 2:

Contact cts;
string searchQuery = 'SELECT id, firstName FROM contact WHERE Membership_number__c = 123456789 LIMIT 1';
try
{
    cts = Database.query(searchQuery);
}
catch (Exception e)
{

}
cts = new Contact(phone = '555', email = '', Membership_number__c = customerNumber);
Schema.SObjectField f = Contact.Fields.Membership_number__c;
Database.UpsertResult cr = Database.upsert(cts, f, false);
system.debug('is success: ' + cr.isSuccess()); //Returns false
system.debug('Id is: ' + cr.getId()); //This returns the ID of inserted/updated record
  • Have you tried checking errors from the upsert result? Also, have you considered using JSON serialization of the class to do this? – Caspar Harmer Jun 3 '15 at 18:34
  • I haven't considered JSON Serialization, it's getting into the system as a XML object which I convert to a Salesforce Contact Object, and afterwards searches the Salesforce Database with the customernumber in the search query. Can you elaborate how to check for errors on upsert result? – Claus Machholdt Jun 3 '15 at 19:36
  • 1
  • 1
    Had to include the "Unique Key field" in the New Contact() initialization. Thanks :-) – Claus Machholdt Jun 3 '15 at 20:12
  • Glad I could help :) – Caspar Harmer Jun 3 '15 at 21:14
3

Try Database.UpsertResult cr = Database.upsert(cts[0], f, false); since you're only inserting one record instead of an array.

Or recode to something like the following:

Contact cts;
string searchQuery = 'SELECT id, firstName FROM contact WHERE Membership_number__c = 123456789 limit 1';
cts = Database.query(searchQuery);
System.debug('Cts contains data: ' + cts.isEmpty()); (Returns False)
cts = new Contact(phone = '555', email = ''); //Just hardcoding a new value here!
Schema.SObjectField f = Contact.Fields.Membership_number__c;
Database.UpsertResult cr = Database.upsert(cts, f, false);  
cr.getId(); 

Also, following your query, you should already have the Id which is cts.Id

| improve this answer | |
  • cts.size() is not valid for the above code – Rao Jun 3 '15 at 18:46
  • 1
    You're right, I was just copying and pasting without paying attention to his debug statements! Good catch! Will make an edit. – crmprogdev Jun 3 '15 at 18:47
  • 1
    also one other thing, he still may see a null Id since there is no explicit database.upsertresult checker. I see that when the contact cts is being inserted the op is not providing an external Id ( membership_number__c) so the system would complain external Id not found. Unless there is an explicit check to see if database,upsertresult.issucess() the id would still return null – Rao Jun 3 '15 at 18:51
  • Understand, but then that wasn't part of his question, nor did he state he was using it as an external ID. I can't read minds. :) – crmprogdev Jun 3 '15 at 18:54
  • 1
    I'm aware of firstName etc. requirements. And I actually just added try/catch logic for it to work, if query didnt return any record. – Claus Machholdt Jun 4 '15 at 5:52
-1

Your search should be like this : string searchQuery = 'SELECT id, firstName,Membership_number__c FROM contact WHERE Membership_number__c = 123456789'; and instead of "cts[0] = new Contact(phone = '555', email = '');" use : cts[0].phone = '555'; cts[0].email = '';

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.