4

I want to reset an attribute of type sObjectType in Lightning/Aura to the default value. I'm unable to reset even if I declare a dummy attribute of same sObjectType attaribute in component and assign it using the controller using

component.set("v.attribute1","v.dummyAttributetoReset");

<aura:attribute name="attribute1" type="l1.LightningObject__c" default="{ 'sobjectType': 'l1__LightningObject__c',
'Name': '',
'l1__Date__c': '',
'l1__Description__c': '',
'l1__Country__c': ''
}"/>

But this just resets the Date field on the form not other fields. When I assign the default value in JSON format(that we declare as default value in the component) it just thrown the error with JSON Parser exception.

Please help or any Suggestions!

  • When you say that you "When I assign the default value in JSON format" do you mean that you tried to set the value of attribute1 that has a type of l1.LightningObject__c to the string encoding of the default? Like GuaravK shows in his answer below once you are in javascript you want to work with javascript objects not strings. The default= in markup specifically knows to try and auto convert from a JSON string to a JSON object which is not the case when setting attributes via javascript (it certainly could be made to work that way but we've never seen the need to). – Doug Chasman Nov 16 '14 at 11:27
6

If there's a number field, set it to 0. If it's a String, set it to ''

I verified it in one of my lightning components that this snippet works (I've masked the true obj/ attr names) if I do this in my associated controller.

({
    myTask : function(component, problem) {
        var action = component.get("c.saveTask");
        action.setParams({
            "task": thisTask
        });
        action.setCallback(this, function(a){
            if(a.getState() === "SUCCESS"){
                 component.set("v.problems", null);
                component.set("v.Task", 
                {'sobjectType': 'obj_c',
                     'Name': '',
                     'obj__Count__c': 0,
                     'obj_String1_c': '',
                     'obj_String2__c': ''
                });

                var rSavedEvent = $A.get("e.obj:recordSaved");
                rSavedEvent.fire();
            }else{
              // Failure
            }
        });
        $A.enqueueAction(action);
    },
})
  • what if i had some 20 fields? – Venky Nov 28 '16 at 13:10
1

Here is an example specific to your question using only Account and a few standard fields (will work for custom objects and fields too of course).

clearingSObjectAttribute.app

<aura:application>
    <aura:attribute name="account" type="Account" 
        default="{ 'sobjectType': 'Account','Name': 'default name', 'AccountNumber': 'default account number' }"/>

    Account Name: <ui:inputText value="{!v.account.Name}"/><br/>
    Account Number: <ui:inputText value="{!v.account.AccountNumber}"/><br/> 

    <ui:button label="Reset" press="{!c.clearFields}"/>
</aura:application>

clearingSObjectAttributeController.js

({
    clearFields : function(component, event, helper) {
        component.set("v.account", { 
            "sobjectType": "Account",
            "Name": "cleared Account.Name",
            "AccountNumber": "cleared Account.AccountNumber"
            }
        );
    }
})

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.