I am trying to insert record into a child object, it gives me StringException:Invalid ID error. Here I am populating the cityStats list with the records to insert and then final Database.SaveResult[] to save to the city_stat object. In the fields section below I am putting the theCity variable value into two fields, one is to the master-detail relationship field, other is to the Name column in the city_stat object. Is this the right way of inserting into a child object.
List<sumchans__City_Stat__c> cityStats = new List<sumchans__City_Stat__c>();
sumchans__City_Stat__c stat = new sumchans__City_Stat__c();
stat.Name = theCity;// City name column in the City Stat table
stat.sumchans__Name__c = theCity;**// Master-Detail relationship field - referenced to City_Master, CITY_NAME Column**
stat.sumchans__ON_BILLINGS__c = onBillings;
stat.sumchans__OFF_BILLINGS__c = offBillings;
stat.sumchans__TOTAL_INTERNET_OFFERINGS__c = internetOfferings;
stat.sumchans__TOTAL_VIDEO_OFFERINGS__c = videoOfferings;
stat.sumchans__TOTAL_PHONE_OFFERINGS__c = phoneOfferings;
cityStats.add(stat);
}
Database.SaveResult[] saveCityStats = Database.Insert(cityStats, false);
The only valid value to place in a Salesforce relationship field is an Id
: a 15- or 18-character Salesforce record Id. It is not valid to place any other value in a relationship field, including a parent record's Name field or an External Id reference. You will always receive a StringException
if you place any value other than a valid Salesforce Id in a relationship field.
Populating the Name
field of an object has no effect on any relationship.
You may populate a relationship field using the value of an External Id field on the parent object only by populating the sObject version of the relationship field, which is postfixed with __r
. The syntax for doing so is described in the Apex Developer Guide.