function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
woodmanzeewoodmanzee 

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,
			   Product_Interest_Notes__c, 
			   (SELECT Id, Name FROM Product_Interests__r)
		FROM Contact
		WHERE Id IN :Trigger.new
	];
	
	for(Contact c : contacts) {
		system.debug(c.Product_Interests__r);
		
		if(c.Product_Interests__r.size() == 0) {
			string[] interests = c.Lead_Product_Interest__c.split(';',0);
			
			if(interests.size() == 0)
				break;
				
			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;
					c.Product_Interests__r.add(p);
					system.debug(c.Product_Interests__r.size());
					
				}
				catch(DMLException e) {
					system.debug(e);
				}
			}
			//c.Product_Interests__r = pi;
			
			update c;
		}
		system.debug(c.Product_Interests__r.size());
	}

 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)

 

So 

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

for(Contact c : ...

{

for (string interest : interests)

{

Product_Interest__c p = new Product_Interest();

......

prodInterests.add(p);

 

 

}//end product interests

}//end contacts

 

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

Database.insert(prodInterests);

Shashikant SharmaShashikant Sharma

Could you please do one thing

 

 

Update this catch block like this 

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

 Please let me know 

 

One more thing why are you updateing c

c.Product_Interests__r.add(p);
system.debug(c.Product_Interests__r.size());

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

woodmanzeewoodmanzee

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;