tag:blogger.com,1999:blog-6140199022860280334.post7373712002223777307..comments2024-03-29T06:37:56.706+00:00Comments on Bob Buzzard Blog: Create Parent and Child Records in One Insert CallBob Buzzardhttp://www.blogger.com/profile/07815472992351343395noreply@blogger.comBlogger35125tag:blogger.com,1999:blog-6140199022860280334.post-25538799709793837482020-11-16T17:35:54.147+00:002020-11-16T17:35:54.147+00:00I know it's been years, but I want this to be ...I know it's been years, but I want this to be answered.<br />You can solve this assigning it to Target_Settings__r (the reference to the object) instead of Target_Setting__c (the Id).Shonen Batnoreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-70955914335631320722020-02-08T12:27:59.064+00:002020-02-08T12:27:59.064+00:00That's an interesting question - it should be ...That's an interesting question - it should be 1 as there is only one statement - that's certainly the case for other scenarios where I've inserted different types in a single statement.Bob Buzzardhttps://www.blogger.com/profile/07815472992351343395noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-31532888535099488802020-02-05T21:35:51.432+00:002020-02-05T21:35:51.432+00:00Thank you Bob. I have a question on governor limit...Thank you Bob. I have a question on governor limits on DML statement count, what would be the count, is it 2 or 1 since you are inserting two different objects in one go.Praveen Krishnahttps://www.blogger.com/profile/02479910035954619470noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-25336168019123102712019-01-15T08:21:48.063+00:002019-01-15T08:21:48.063+00:00Is there a way for this solution to work on a list...Is there a way for this solution to work on a list of account records and a list of contact records where external ID value can be set dynamically versus having to define it up front on each record?Docta Lovehttps://www.blogger.com/profile/16023484395539125507noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-88314799705481435312018-08-09T23:28:12.944+01:002018-08-09T23:28:12.944+01:00will it work for custom objects?will it work for custom objects?Anonymoushttps://www.blogger.com/profile/17787261946884628915noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-64319742357133691452018-01-18T18:22:05.151+00:002018-01-18T18:22:05.151+00:00is the same possile while trying to link custom ob...is the same possile while trying to link custom objects? <br />say a lookup on child? Anonymoushttps://www.blogger.com/profile/05158717257454274512noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-60788516938505882222017-10-10T19:15:05.237+01:002017-10-10T19:15:05.237+01:00Hi...I am able to use this create a junction objec...Hi...I am able to use this create a junction object and the two Master records but I need to populate an ExternalID field on the junction object which is a combination of Master1 objectID + Master2 object ID. Any thoughts of how this can be acheived? I am looking for a way to upsert data into the two Master objects as well as the Junction objectAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-53455820217997877872017-03-24T07:36:36.717+00:002017-03-24T07:36:36.717+00:00Hi
How can I do this for upserting list of two cus...Hi<br />How can I do this for upserting list of two custom objects. I followed approach that you have shared but I am not able to do it for the list of two related custom objects(parent, Child). NIKHILnoreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-3370544382142542422017-03-06T04:43:40.125+00:002017-03-06T04:43:40.125+00:00It's years after the fact and not sure if you&...It's years after the fact and not sure if you're still monitoring comments on this post, but I can't figure out the syntax to achieve a single insert statement for 2 custom objects. Help?<br /><br />This line:<br /><br />custom_object_a__c=new custom_object__a(external_id__c='abc')<br /><br />Gives me this error:<br /><br />Invalid initial expression type for field custom_object_b__c.custom_object_a__c, expecting: IdAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-20722244185665652132017-01-06T07:50:35.259+00:002017-01-06T07:50:35.259+00:00I am doing the same thing in custom objects. But I...I am doing the same thing in custom objects. But I am getting compilation error as 'Illegal assignment from Target_Setting__c to Id'<br />where Target_Setting__c is my parent custom objectGorai Souravmoyhttps://www.blogger.com/profile/09468973649042336445noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-6690171509312036332016-11-11T09:26:18.620+00:002016-11-11T09:26:18.620+00:00Hi Bob, taking a leap I am working on a VF page to...Hi Bob, taking a leap I am working on a VF page to create any parent and child record!!<br /><br />The prototype is ready. Can you review the requirements and help me by giving your thoughts on what would be useful to have in such an app?<br /><br />Refer to: https://salesforcemann.wordpress.com/2016/11/11/pet-project-create-parent-and-child-records-from-visualforce-page-for-any-salesforce-object/humanhttps://www.blogger.com/profile/07764688345723000000noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-76959424780497781972016-06-13T08:01:43.925+01:002016-06-13T08:01:43.925+01:00Hello Bob,
Thank you for the post, indeed its ver...Hello Bob,<br /><br />Thank you for the post, indeed its very good post with perfect example, I was trying to do the same in one of my Org, but somehow it is not working as per expectation. I have followed all the steps as you mentioned but still am getting an error : <br /><br />"System.DmlException: Insert failed. First exception on row 1; first error: REQUIRED_FIELD_MISSING, Required fields are missing"<br /><br />Is there has been any change during the API releases ? As this is not working at all (I am using API version 37). Could you please check this with latest API version if it is working.<br /><br />Thank you again.Anonymoushttps://www.blogger.com/profile/03183024069561956552noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-2195015769612138002015-07-30T17:06:29.027+01:002015-07-30T17:06:29.027+01:00I was stuck on that. Thanks Bob!I was stuck on that. Thanks Bob!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-16925672710388616302015-07-24T12:55:42.795+01:002015-07-24T12:55:42.795+01:00Thanks man , nice way to do it. :)
Thanks man , nice way to do it. :)<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-50380628081669291112014-12-05T20:26:56.497+00:002014-12-05T20:26:56.497+00:00Hi,
How would I go about it if I wanted to get the...Hi,<br />How would I go about it if I wanted to get the Master_Id__c value from an input field, instead of hardcoding it as 'Blog Acc 8'?<br />I'm assuming we first need a Public string xxxx which we set as the value of the input field first, but I don't know how to proceed from there.<br /><br />Contact cont=new Contact(FirstName='Bob', LastName='Buzzard', Account=new Account(Master_Id__c='Blog Acc 8'));<br /><br /> Thanks!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-16491893089113398522014-06-02T08:38:14.637+01:002014-06-02T08:38:14.637+01:00I don't have any ideas to make it work I'm...I don't have any ideas to make it work I'm afraid. I suspect its to do with the trigger semantics, because the upsert has already taken place (at least partly), There may be something around the fact that account/contact (and opportunity/line item, order/line item) are system relationships rather than master/detail or lookup, which confers some additional capability on them.Bob Buzzardhttps://www.blogger.com/profile/07815472992351343395noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-56251977391216555382014-06-01T21:01:42.749+01:002014-06-01T21:01:42.749+01:00
Hi Keir,
This blog post has always been a refere...<br />Hi Keir,<br /><br />This blog post has always been a reference for me.<br />I come back to it regularly to check for the syntax of case number 8 while upserting lookup references to objects via a foreign key relationship. This has always worked for me : for Contact+Account, OrderLine+Order, OpportunityLineItem+Opportunity, etc.<br /><br />Up to today :(<br /><br />I am trying to insert/update contacts with a reference to other contacts.<br />The use case ?<br />I have a list of contacts, all with an email address as an external ID in IDEXT_Email__c. Some are endorsed by others, so Contact will have an EndorsedBy__c lookup to a Contact. As this stuff is coming from another system, we are using upsert with foreign keys : Contact has an EndorsedBy_Email__c field with the email address of the contact who endorsed it; this serves as the key in the foreign relationship to look up IDEXT_Email__c on Contact.<br />This is great in practically all cases ... except the following.<br /><br />I have a list of 200 accounts, some being endorsed by others ... in the same list.<br /><br />We are handling the foreign key wizardry in a Contact trigger.<br />I have tried all the possible solutions I can think of, but our problem boils down to the following : from within a trigger (on a list of contacts), you cannot upsert a contact which is already in the list. Unfortunately, the syntax suggested by you in case number 8 does not work if the related-to Contact is not upserted previously, in another transaction : it will not work if the Contact is in the same transaction, event if it is before in the list.<br /><br />Any thoughts ?<br /><br />Have a good day,<br />Rupert (@altius_rup)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-49926698925122730322014-05-01T13:35:31.117+01:002014-05-01T13:35:31.117+01:00Great Stuff, thank you Bob :-)!Great Stuff, thank you Bob :-)!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-14506481031334218302013-09-24T21:11:15.478+01:002013-09-24T21:11:15.478+01:00Very good post Bob. I am facing a situation simila...Very good post Bob. I am facing a situation similar to it but with a twist. I need to be able to insert the parent, its children and then grandchildren. <br /><br />Something like if parent name is A then A has three children<br /><br />A1<br />A2 and <br />A3<br /><br />And then grand children<br /><br />A1-Child1<br />A1-Child2<br /><br />A2-Child1<br />A2-Child2<br /><br />A3-Child1<br />A3-Child2<br />A3-Child3.<br /><br />How would I resolve this.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-16673032174756529992013-09-14T17:45:41.141+01:002013-09-14T17:45:41.141+01:00Hey Bob,
I am trying to insert multiple child rec...Hey Bob,<br /> I am trying to insert multiple child records to the junction object. <br />I have snippet works fine for single but when i try multiple it throws me error field writable.I tried to use your solution but is giving error. can you help me to fix my issue. <br /><br />Anonymoushttps://www.blogger.com/profile/01980081692239060142noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-25910448758451347302013-07-02T18:02:54.160+01:002013-07-02T18:02:54.160+01:00Great Post Bob, this really helped. However when I...Great Post Bob, this really helped. However when I am trying to add Opportunity Team Members using the same concept, it errors out. <br /><br />First of all I create an instance of Opportunity and User using their respective External Id's<br /><br />User teamUser = new User(User_External_ID__c =reader.getText());<br /><br />Opportunity req = new Opportunity(Opportunity_External_ID__c = reqNumber);<br /><br />I then create an instance of Opportunity Team Member<br /><br />OpportunityTeamMember teamMember = new OpportunityTeamMember(Opportunity = req, User = teamUser, TeamMemberRole = 'FM');<br /><br />Once I do an upsert of for teamMember, it gives me an error saying<br /><br />VF_PAGE_MESSAGE OpportunityTeamMember: bad field names on insert/update call: User, Opportunity<br /><br />System.DmlException: Upsert failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, OpportunityTeamMember: bad field names on insert/update call: User, Opportunity: [User, Opportunity]<br /><br />VARIABLE_ASSIGNMENT [1675]|e|"common.apex.runtime.impl.DmlExecutionException: Upsert failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, OpportunityTeamMember: bad field names on insert/update call: User, Opportunity: [User, Opportunity]"|0x19dcd463<br /><br /><br />Not sure what am I missing. I tried all the examples described above. Thank you for your helpJoehttps://www.blogger.com/profile/08395002839514693523noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-8702705917330922602013-07-02T18:01:06.199+01:002013-07-02T18:01:06.199+01:00This comment has been removed by the author.Joehttps://www.blogger.com/profile/08395002839514693523noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-70099444133813421862013-01-29T20:07:55.548+00:002013-01-29T20:07:55.548+00:00Great post...Great post...DarkJoyhttps://www.blogger.com/profile/17341028734201776084noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-15986317684358340492012-11-20T11:11:53.830+00:002012-11-20T11:11:53.830+00:00@Tom - That's exactly what I put in the blog p...@Tom - That's exactly what I put in the blog post as the solution - not sure why you are posting the same thing here.Bob Buzzardhttps://www.blogger.com/profile/07815472992351343395noreply@blogger.comtag:blogger.com,1999:blog-6140199022860280334.post-66933024055793959732012-11-20T10:50:17.969+00:002012-11-20T10:50:17.969+00:00A workaround to the exeception "System.DmlExc...A workaround to the exeception "System.DmlException: Insert failed. First exception on row 0; first error: INVALID_FIELD, More than 1 field provided in an external foreign key reference in entity" <br /><br />is to create a temp Account with just the external id field and then linking the contact to it :<br /><br />Account acc = new Account(...);<br />Contact cont = new Contact(...);<br /><br />Account tempAccount = new Account(<br /> ExternalId__c = 'external_id'<br /> );<br /> <br />cont.Account = tempAccount;<br /><br />insert acc;<br />insert cont;<br />Tomnoreply@blogger.com