2

Schema.DescribeSObjectResult r = t.getDescribe();

Schema.DescribeFieldResult f =r.fields.getMap().get(fieldName).getDescribe();

return f.getType(); // this here returns the field type

Is there a way to create a attribute based on the type returned or dynamically assign it to the existing attribute?

3
  • 1
    There are several posts addressing this questions on the forum. Can you provide more context. Please take some time to review How to Ask and narrow down the scope of your question. thanks!
    – glls
    May 20, 2017 at 16:39
  • @glls I searched the forum before posting the question and didn't find anything quiet related to what I'm looking for. I would really appreciate if you can point me towards one of those several posts that I might have missed.
    – cartman
    May 20, 2017 at 16:51
  • Hi @Cartman, as I mentioned, it is important you mention THIS when you open a question, please refer to How to Ask. It is you, that must include resources that you read, and point out why its not what you are looking for, Otherwise, people will assume that it is a duplicate of an existing question. Provide minimal working code samples of what you are trying to achieve, explain what is the expected behavior and the actual one. Include any errors and information on your debugging attempts. We need this information to be able to help you.
    – glls
    May 20, 2017 at 16:58

2 Answers 2

1

Yeah you can declare an aura attribute of type="Object" which can take any object either an sObject or an object instatiated and created in JavaScript controller.

But serialisation at client side that is in your JavaScript controller and deserialisation into a custom wrapper at Apex controller should be done if you are sending custom JavaScript objects back to apex

Note: variables in the wrapper class must be @auraEnabled

0

First, I'm going to assume you're running the above code in a Server side controller and passing the results to your component. It's not clear if you're wanting to define a Type attribute for an Object's variable, but it sounds as though you do. I think there are two references that will answer all of your questions and point you in the right direction.

The way to do this is via a server side controller since the code you've posted won't run client side. See Working with Salesforce Records in the Lightning Component Developer Guide and I think you'll also find some of the links at the bottom of that page helpful to you too.

You'll notice the below code uses the aura:attribute to set an object type.

<aura:component controller="MyObjController"/>
<aura:attribute name="myObjects" type="namespace.MyObj__c[]"/>
<aura:iteration items="{!v.myObjects}" var="obj">
    {!obj.Name}, {!obj.namespace__myField__c}
</aura:iteration>

You'd need to pass a different type into your code and use set:attribute to then provide the attribute's name and type to change it dynamically. See aura:set in the documentation for more on how to do this, including with arrays that contain an object and it's fields (object fields being what seems to be more relevant to your question).

You'll still need to have a server side controller that contains the results of your Apex schema describe calls to pass the type in an @AuraEnabled method to your component if you're doing this dynamically as implied by the code you've provided. You could also use typeof rather than complete schema calls where appropriate. I wouldn't want to rely on JS for this.

1
  • 1
    I still do not understand how to use <aura:set> for setting the type. Say the field type returned from apex is Date. I have an attribute defined in my component <aura:attribute name="fieldname" type="Boolean">, how would I be able to change the type of fieldname to date?
    – cartman
    May 21, 2017 at 0:56

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.