0

I've created a component which accepts as an attribute an SObject. When I simply pass a JSON SObject to it, like:

 <c:ChildCreatorComponent obj="{
                                    sobjectType : 'Contact',
                                    LastName : 'Rossi',
                                    FirstName : 'Mario'
                                }" />

Everything is fine. But right now, I need to pass a dynamic value (like an ID):

<c:ChildCreatorComponent obj="{
                                    sobjectType : 'Contact',
                                    AccountId : MY_DYNAMIC_ID,
                                    LastName : 'Rossi',
                                    FirstName : 'Mario'
                                }" />

If I try to insert my ID with an expression, as I would do in Visualforce, I obtain an error. Example:

<c:ChildCreatorComponent obj="{
                                        sobjectType : 'Contact',
                                        AccountId : {!v.recordId},
                                        LastName : 'Rossi',
                                        FirstName : 'Mario'
                                    }" />

"Cannot mix expression and literal string in attribute value, try rewriting like {!'foo' + v.bar}: Source"

But if I do that, I am passing a String instead of an SObject to the component. Any idea? I want to keep the simplest as possible the component attribute structure.. So I can not create other attribute for the dynamic field values.

  • the obj is an attribute in your case and not a HTML markup and hence you get an error .What is your use case here ? – Mohith Shrivastava Oct 25 '16 at 15:59
  • What is your code when you change to {!'foo' + v.bar}? maybe you can pass another variable? – Itai Shmida Oct 26 '16 at 5:40
  • @ItaiShmida My code is more or less this: <c:ChildCreatorComponent obj="{!' \'sobjectType\' : \'Contact\','+ '\'AccountId\' : '+v.recordId+','+ '\'LastName\' : \'Rossi\','+ '\'FirstName\' : \'Mario\' ' }" /> Main problem: that's a string and not an object, so the system return me a generic "Unexpected Error"... – F. Carmagnola Oct 26 '16 at 7:10
0

I used a workaround in the end. I added a String attribute to my component, called "lookups", format like this: "fieldName1:value1;fieldName2:value2...". I use some code to read the values and write it in the object.

You can check the entire code here:

https://github.com/fracarma/Lightning-Object-Creator-Component

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.