+ Start a Discussion

Problem Dealing with Custom Relationships

I am having a (hopefully easy) problem creating new child objects.  I have a custom object called Product Interest that has a master-detail relationship to Contact.  In my trigger I simply want to create several new Product Interests and add them to the Contact's related list.  However, when I call add on contact.Product_Interests__r, nothing is added (i.e. contact.Product_Interests__r.size() == 0).  Can someone help me understand what is going on?


Thanks.  Here is the code for the "test" case I started to write (currenlty it makes no assertions).

static testMethod void CreateContact() {
        Contact c = new Contact();
		c.FirstName = 'Cameron';
		c.LastName = 'Woodmansee';
		c.MailingCity = 'Provo';
		c.MailingCountry = 'USA';
		c.MailingPostalCode = '84606';
		c.MailingState = 'UT';
		c.MailingStreet = '541 E 500 N';
		c.Lead_Product_Interest__c = 'ELx50; ELx808; Eon; Epoch';
		insert c;

And here's the trigger code.

trigger CreateProductInterests on Contact (after insert) {

	Contact[] contacts = [
		SELECT Id, Name, Account.Id, Lead_Campaign_Source__c, LeadSource, Lead_Product_Interest__c,
			   (SELECT Id, Name FROM Product_Interests__r)
		FROM Contact
		WHERE Id IN :Trigger.new
	for(Contact c : contacts) {
		if(c.Product_Interests__r.size() == 0) {
			string[] interests = c.Lead_Product_Interest__c.split(';',0);
			if(interests.size() == 0)
			for(string interest : interests) {
				Product_Interest__c p = new Product_Interest__c();
				p.Account__c = c.Account.Id;
				p.Campaign__c = (Id) c.Lead_Campaign_Source__c;
				p.Contact__c = c.Id;
				p.Product_Interest_Notes__c = c.Product_Interest_Notes__c;
				p.Product_Interest_Source__c = c.LeadSource;
				p.Contact__c = c.Id;
				try {
					insert p;
				catch(DMLException e) {
			//c.Product_Interests__r = pi;
			update c;

 Thanks again.


Ritesh AswaneyRitesh Aswaney

You will need to explicitly insert the records, rather than adding them to the 'related list' on Contact and updating the Contacts. (Also, never a good idea to query and / or update/insert stuff inside loops - you will exhaust your limits very quickly)



Product_Interest__c[] prodInterests = new Product_Interest[]{};

for(Contact c : ...


for (string interest : interests)


Product_Interest__c p = new Product_Interest();





}//end product interests

}//end contacts


if(prodInterests != null && !prodInterests.isEmpty())


Shashikant SharmaShashikant Sharma

Could you please do one thing



Update this catch block like this 

catch(DMLException e) {
                                        trigger.new[0].addError('Error : '+e.getmessage());

 Please let me know 


One more thing why are you updateing c


 This will not add anything in related list, size will always come as 0.


Thanks for your help, but why won't c.Product_Interests__r.add(p); add anything? Why allow that operation when it does nothing?


Shashikant SharmaShashikant Sharma

When you use relationship to fetch the related list , you can not add any item to this list. It is similar to that like if I fetch a related field of any object say


Contact c = [Select Account.Name from Contact where lastName = 'Test' limit 1];

now i can read the c.Account.name but can not update it like


c.Account.name = 'New Account';

update c;