Skip to main content
Solution was to make a copy of the object not to set a default
Source Link

You didn't definecannot set new properties on a record returned from the defaults for Account sobject type:

It should be likeserver or retrieved using cmp.get('v.[attributeName]'). The workaround is to make a copy of the object that: you want to set a new property on.

<aura:attribute 
                type="Account" 
                name="acc"
                default="{ 'sobjectType': 'Account',
                           'Name': '',
                           'Rating': 'Hot'
                         }"
 />

Your ControllerJS would be:

({
    init : function(cmp, event, helper) {
        console.log('init');
        var action = cmp.get('c.getAcc');
        action.setCallback(this, function(response) {
            // You cannot set a new property on an object returned from the server
            var acc = response.getReturnValue(); 

            var// accTYou =can JSON.stringify(acc);set a new property on a copy
            var accT = JSON.stringify(JSON.parse(accTacc)); 

            accT.Rating = 'Hot T';
            cmp.set('v.acc', accT);
        });
        $A.enqueueAction(action);
    }
})

You didn't define the defaults for Account sobject type:

It should be like that:

<aura:attribute 
                type="Account" 
                name="acc"
                default="{ 'sobjectType': 'Account',
                           'Name': '',
                           'Rating': 'Hot'
                         }"
 />

Your ControllerJS would be:

({
    init : function(cmp, event, helper) {
        console.log('init');
        var action = cmp.get('c.getAcc');
        action.setCallback(this, function(response) {
            var acc = response.getReturnValue();
            var accT = JSON.stringify(acc);
            accT = JSON.parse(accT);
            accT.Rating = 'Hot T';
            cmp.set('v.acc', accT);
        });
        $A.enqueueAction(action);
    }
})

You cannot set new properties on a record returned from the server or retrieved using cmp.get('v.[attributeName]'). The workaround is to make a copy of the object that you want to set a new property on.

<aura:attribute 
                type="Account" 
                name="acc"
 />

Your ControllerJS would be:

({
    init : function(cmp, event, helper) {
        console.log('init');
        var action = cmp.get('c.getAcc');
        action.setCallback(this, function(response) {
            // You cannot set a new property on an object returned from the server
            var acc = response.getReturnValue(); 

            // You can set a new property on a copy
            var accT = JSON.stringify(JSON.parse(acc)); 

            accT.Rating = 'Hot T';
            cmp.set('v.acc', accT);
        });
        $A.enqueueAction(action);
    }
})
added 530 characters in body
Source Link
Ashwani
  • 22.7k
  • 4
  • 41
  • 73

You didn't define the defaults for Account sobject type:

It should be like that:

<aura:attribute 
                type="Account" 
                name="acc"
                default="{ 'sobjectType': 'Account',
                           'Name': '',
                           'Rating': 'Hot'
                         }"
 />

Your ControllerJS would be:

({
    init : function(cmp, event, helper) {
        console.log('init');
        var action = cmp.get('c.getAcc');
        action.setCallback(this, function(response) {
            var acc = response.getReturnValue();
            var accT = JSON.stringify(acc);
            accT = JSON.parse(accT);
            accT.Rating = 'Hot T';
            cmp.set('v.acc', accT);
        });
        $A.enqueueAction(action);
    }
})

You didn't define the defaults for Account sobject type:

It should be like that:

<aura:attribute 
                type="Account" 
                name="acc"
                default="{ 'sobjectType': 'Account',
                           'Name': '',
                           'Rating': 'Hot'
                         }"
 />

You didn't define the defaults for Account sobject type:

It should be like that:

<aura:attribute 
                type="Account" 
                name="acc"
                default="{ 'sobjectType': 'Account',
                           'Name': '',
                           'Rating': 'Hot'
                         }"
 />

Your ControllerJS would be:

({
    init : function(cmp, event, helper) {
        console.log('init');
        var action = cmp.get('c.getAcc');
        action.setCallback(this, function(response) {
            var acc = response.getReturnValue();
            var accT = JSON.stringify(acc);
            accT = JSON.parse(accT);
            accT.Rating = 'Hot T';
            cmp.set('v.acc', accT);
        });
        $A.enqueueAction(action);
    }
})
Source Link
Ashwani
  • 22.7k
  • 4
  • 41
  • 73

You didn't define the defaults for Account sobject type:

It should be like that:

<aura:attribute 
                type="Account" 
                name="acc"
                default="{ 'sobjectType': 'Account',
                           'Name': '',
                           'Rating': 'Hot'
                         }"
 />