0

PFB Code. I want to validate few fields before save but not able to fetch field values from JSON string.

saveFunction:function(component, event, helper){ 

             //Production Year Details/validation
        var listOfPYRecords= JSON.parse(event.getParam('draftValues'));     
        alert(listOfPYRecords[0]);
        for(var i =0;i<listOfPYRecords.length;i++)
        {
            var temp=listOfPYRecords[i];         
        }        
        helper.onSave(component,event);

    }
  • Suggest you console.log the JSON to make sure it is what you think it is. – Keith C May 16 '19 at 10:11
  • With var listOfPYRecords= JSON.stringify(event.getParam('draftValues')); alert(listOfPYRecords); This is the output with alert [{"Quantity__c":"4","Id":"a0CJ000000U04sCMAR"}] now i want to fetch quantity field from it. – Priya May 16 '19 at 10:36
0

@Priya based on your above comment with alert message response, here is the solution to get the Quantity__c, value for validation, please try and let me know if you found any issue.

controller.js

event.getParam('draftValues').forEach(function(draftitem){
        if(draftitem.Quantity__c==null || draftitem.Quantity__c < 0)
        {
              var toastEvent=$A.get("e.force:showToast");
                               toastEvent.setParams({title:'Validation Error',
                                                     message:'Quantity Cannot be Blank or Less than Zero for Production Years',
                                                     key:'info_alt',
                                                     type:'error',
                                                     mode:'pester'
                                                    });
                               toastEvent.fire();
        }

    });


    helper.onSave(component,event);

}
| improve this answer | |
  • Mind explaining your code? – glls May 16 '19 at 11:42
  • based on alert message above @Priya comments, event.getParam('draftValues') return array of itemobject,and item structure is {"Quantity__c":"4","Id":"a0CJ000000U04sCMAR"}, so iterate the array using forEach function and get the "Quantity__c" value for validation, that's what -priya wants i think. – sdandamud1 May 16 '19 at 12:54
  • Please edit your answer – glls May 16 '19 at 12:55
1

From your comments, it looks like the data is already JavaScript objects so the fix is to remove the parse:

var listOfPYRecords= event.getParam('draftValues'); 
| improve this answer | |
  • But how to access field values from this list because when i try to iterate and access the field values it comes Undefined! – Priya May 16 '19 at 11:05
  • @Priya If the list item isn't undefined but the field is, it may be that you have the casing of the field name wrong. It should be listOfPYRecords[i].Quantity__c. – Keith C May 16 '19 at 11:12

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