You need to sign in to do that
Don't have an account?
Minh Tran 26
Populate lookup field in custom object
One of the field in my custom object (Program__c) is a contact lookup field (Client__c). How do I insert a new custom object with an existing contact without getting the this exception:
com.sforce.ws.SoapFaultException: A duplicate value was specified for field 'Id' in object 'Contact'
Thanks,
com.sforce.ws.SoapFaultException: A duplicate value was specified for field 'Id' in object 'Contact'
Thanks,
SObject[] records = new SObject[1]; try { StringBuilder sb = new StringBuilder(); sb.append("SELECT Id, FirstName, LastName, AccountId, Account.Name "); sb.append("FROM Contact "); sb.append("WHERE AccountId != NULL "); sb.append(" AND Id = '003d000002w3njaAAA' "); QueryResult queryResults = connection.query(sb.toString()); SObject contact = null; if (queryResults.getSize() > 0) { contact = (SObject)queryResults.getRecords()[0]; } SObject so = new SObject(); so.setType("Program__c"); so.setField("RecordTypeId", "012d0000000hQlHAAU"); so.setField("Client__r", contact); so.setField("Marital_Status__c", "Separated"); so.setField("Level_of_Care__c", "Inpatient"); so.setField("Caller_Caregiver_Name__c", "Caregiver Name"); so.setField("Patient_Type__c", "Impatient Discharge"); so.setField("Admission_Date__c", new Date()); so.setField("Projected_Discharge_Date__c", new Date()); so.setField("Facility__c", "Grossmont"); so.setField("Notes__c", "Test record added by Minh..."); records[0] = so; // create the records in Salesforce.com SaveResult[] saveResults = connection.create(records); // check the returned results for any errors for (int i = 0; i < saveResults.length; i++) { if (saveResults[i].isSuccess()) { System.out.println(i+". Successfully created record - Id: " + saveResults[i].getId()); } else { Error[] errors = saveResults[i].getErrors(); for (int j = 0; j < errors.length; j++) { System.out.println("Error creating record: " + errors[j].getMessage()); } } } } catch (Exception e) { e.printStackTrace(); }
__r suffix is used to refer to relationships for the fields it is __c . As Client__c is a lookup field on Program__c object replace the
so.setField("Client__r", contact);
With
So your code will be
Let me know if helps you or still see face issue.
Thanks
Shashikant
All Answers
__r suffix is used to refer to relationships for the fields it is __c . As Client__c is a lookup field on Program__c object replace the
so.setField("Client__r", contact);
With
So your code will be
Let me know if helps you or still see face issue.
Thanks
Shashikant
BTW, since I'm using the Partner WS, it should be:
Please mark this post as solved so that others could be benifitted from it.
Thanks
Shashikant