+ Start a Discussion
VRKVRK 

Fetch Record from lighting:dataTable issue


Hi
How to Fetch only '"Contract Release' in below JSON Data :::

data->
        [{"Date_Reviewed":"1989-05-06","Date_Sent":"1989-05-05","editable":true,"Form":"Contract Revision 1","Id":"a0S3F000001bXAIUA2"},
        {"Date_Reviewed":"2019-02-01","Date_Sent":"2019-02-01","editable":true,"Form":"Contract Revision 2","Id":"a0S3F000001bXAJUA2"},
        {"Date_Reviewed":"2020-12-27","editable":true,"Form":"Contract Revision 3","Id":"a0S3F000001bXAKUA2"},
        {"Date_Reviewed":"2020-01-03","Date_Sent":"2020-01-03","editable":true,"Form":"Contract Release","Id":"a0S3F000001bXALUA2"},
        {"Date_Reviewed":"2020-12-29","editable":true,"Form":"Contract Revision 4","Id":"a0S3F000001bXAMUA2"},
        {"Date_Reviewed":"2020-12-30","editable":true,"Form":"Contract Revision 5","Id":"a0S3F000001bXANUA2"},
]        

My code :
saveDataTable :function(component, event) {
var data = component.get("v.data"); 
 var editedRecords1 =  component.find("riderStatusDataTable").get("v.draftValues");
for (var x in data)
            {
              var d = data[x];
 if(d.Form == "Contract Release" ){
  TemporaryArray1.Date_Sent__c = editedRecords1[0].Date_Sent;
           TemporaryArray1.Date_Reviewed__c = editedRecords1[0].Date_Reviewed;
break;
}
But , this is not Restrict for Contract Release ........its applied for all the Forms . I need this is for only 'Contract Release'...
can anyone help me...
Thanks
VRK
Best Answer chosen by VRK
Jitendra Singh ShahiJitendra Singh Shahi

No you can't use that as there is no form field in draft values.

here is code you have to do:

let data = [{"Date_Reviewed":"1989-05-06","Date_Sent":"1989-05-05","editable":true,"Form":"Contract Revision 1",
	"Id":"a0S3F000001bXAIUA2"}, 
    {"Date_Reviewed":"2019-02-01","Date_Sent":"2019-02-01","editable":true,"Form":"Contract Release","Id":"a0S3F000001bXAJUA2"}];
    
let editedRecord = component.find("riderStatusDataTable").get("v.draftValues");

let releaseData = data.filter(dataVal => dataVal.Form == "Contract Release");

let TemporaryArray1 = {'sobjectType':'Reader_Status__c'};

editedRecord.forEach((editObj) => {
	releaseData.forEach((elem, index) => {
    	if(elem.Id == editObj.Id){
        	TemporaryArray1.Date_Sent__c = editObj.Date_Sent;
        	TemporaryArray1.Date_Reviewed__c = editObj.Date_Reviewed;  
        }
    })
})

All Answers

Jitendra Singh ShahiJitendra Singh Shahi

Hi,

you can achieve this using filter:

let filteredData = data.filter(dataVal => data.Form  == "Contract Release");

in filteredData you have the result.

for more info please check this:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

and if it helps you please mark as the best answer.

Jitendra Singh ShahiJitendra Singh Shahi

Hi, 

let's see explanation below how the filter will work. (In above code there is typo I have fixed here)

let data = [{"Date_Reviewed":"1989-05-06","Date_Sent":"1989-05-05","editable":true,"Form":"Contract Revision 1","Id":"a0S3F000001bXAIUA2"},
        {"Date_Reviewed":"2019-02-01","Date_Sent":"2019-02-01","editable":true,"Form":"Contract Release","Id":"a0S3F000001bXAJUA2"},{"Date_Reviewed":"1989-05-06","Date_Sent":"1989-05-05","editable":true,"Form":"Contract Revision 1","Id":"a0S3F000001bXAIUA2"}, {"Date_Reviewed":"2019-02-01","Date_Sent":"2019-02-01","editable":true,"Form":"Contract Release","Id":"a0S3F000001bXAJUA2"}]


here data have an array of objects.  Now if you want to get only those objects which have Form = "Contract Release". you will do this.

 

let releaseData = data.filter(dataVal => dataVal.Form  == "Contract Release");


this releaseData variable also an array of object but it have only those object which have form = "Contract Release". Now you can work with releaseData. below is the result.

 

User-added image

hoe this helps you.

Jitendra Singh ShahiJitendra Singh Shahi
it will be really helpful if you explain what exactly you want to do. From your response what I understand is you are able to get the list of records where form = "contract release". Now, what is your next step or what you want to achieve after this?
Jitendra Singh ShahiJitendra Singh Shahi
do you want to update only those data which you just extracted from list ( where form = "Contract Release")?
Jitendra Singh ShahiJitendra Singh Shahi

Yes, it is a little bit clear. I added below code let me know if it helps you:

let data = [{"Date_Reviewed":"1989-05-06","Date_Sent":"1989-05-05","editable":true,"Form":"Contract Revision 1","Id":"a0S3F000001bXAIUA2"},
        {"Date_Reviewed":"2019-02-01","Date_Sent":"2019-02-01","editable":true,"Form":"Contract Release","Id":"a0S3F000001bXAJUA2"},{"Date_Reviewed":"1989-05-06","Date_Sent":"1989-05-05","editable":true,"Form":"Contract Revision 1","Id":"a0S3F000001bXAIUA2"}, {"Date_Reviewed":"2019-02-01","Date_Sent":"2019-02-01","editable":true,"Form":"Contract Release","Id":"a0S3F000001bXAJUA2"}]
        
        let releaseData = data.filter(dataVal => dataVal.Form == "Contract Release");
        
        let TemporaryArray1 = {'sobjectType':'Reader_Status__c'};
        releaseData.forEach((elem, index) => {
        	TemporaryArray1.Date_Sent__c = elem.Date_Sent;
           	TemporaryArray1.Date_Reviewed__c = elem.Date_Reviewed;  
        })
        console.log(TemporaryArray1);


this will make this output:

User-added image

let me know if you face any problem.

Jitendra Singh ShahiJitendra Singh Shahi
sure, can you send me the format of data you have in 
component.find("riderStatusDataTable").get("v.draftValues").
means what you get when you run the above code (JSON structure).
VRKVRK
[{"Date_Sent":"2020-06-05","Date_Reviewed":"2020-06-05","Id":"a0S3F000001bXALUA2"}]

The new Values of Date_Sent,Date_Reviewed values for Contract Release Form...
So , i need to pass these values ..
VRKVRK
 var editedRecords1 =  component.find("riderStatusDataTable").get("v.draftValues");
      var myData = JSON.stringify(editedRecords1);
 console.log(myData);

[{"Date_Sent":"2020-06-05","Date_Reviewed":"2020-06-05","Id":"a0S3F000001bXALUA2"}]

 
Jitendra Singh ShahiJitendra Singh Shahi

Hi,

try this: 
 

let data = [{"Date_Reviewed":"1989-05-06","Date_Sent":"1989-05-05","editable":true,"Form":"Contract Revision 1",
	"Id":"a0S3F000001bXAIUA2"}, 
    {"Date_Reviewed":"2019-02-01","Date_Sent":"2019-02-01","editable":true,"Form":"Contract Release","Id":"a0S3F000001bXAJUA2"}];
    
let editedRecord = [{"Date_Sent":"2020-06-05","Date_Reviewed":"2020-06-05","Id":"a0S3F000001bXALUA2"}];

let releaseData = data.filter(dataVal => dataVal.Form == "Contract Release");

let TemporaryArray1 = {'sobjectType':'Reader_Status__c'};
releaseData.forEach((elem, index) => {
	TemporaryArray1.Date_Sent__c = editedRecord[0].Date_Sent;
    TemporaryArray1.Date_Reviewed__c = editedRecord[0].Date_Reviewed;  
})
console.log(TemporaryArray1);

it will make this output with new dates.

User-added image

​​​​​​​If the above suggestion worked, let us know by marking the answer as "Best Answer" right under the comment which will help the rest of the community should they have a similar issue in the future.
Jitendra Singh ShahiJitendra Singh Shahi

yes as I am testing in a different systems where I do not have those objects that's why I chose it static. so for making it dynamic you can do this:

 

let data = [{"Date_Reviewed":"1989-05-06","Date_Sent":"1989-05-05","editable":true,"Form":"Contract Revision 1",
	"Id":"a0S3F000001bXAIUA2"}, 
    {"Date_Reviewed":"2019-02-01","Date_Sent":"2019-02-01","editable":true,"Form":"Contract Release","Id":"a0S3F000001bXAJUA2"}];
    
let editedRecord = component.find("riderStatusDataTable").get("v.draftValues");

let releaseData = data.filter(dataVal => dataVal.Form == "Contract Release");

let TemporaryArray1 = {'sobjectType':'Reader_Status__c'};
editedRecord.forEach((editObj) => {
	releaseData.forEach((elem, index) => {
    	TemporaryArray1.Date_Sent__c = editObj.Date_Sent;
    	TemporaryArray1.Date_Reviewed__c = editObj.Date_Reviewed;  
	})
})

 

VRKVRK
  let releaseData = data.filter(dataVal => dataVal.Form == "Contract Release");
can we apply similar filter on draft Values also for retrive Only 'Contract Release'.
 let editedRecord = component.find("riderStatusDataTable").get("v.draftValues");
i tried bleow line :
   let editedRecord1 = editedRecord.filter(dataVal => dataVal.Form == "Contract Release");   ( But display output as BLANK).

The problem of the Above code is :  No filter on draft Values, so its applicable for other Forms also .
So i am expecting :::
 i need to filter for Contract Release same as data.filter(dataVal => dataVal.Form == "Contract Release")  for draftValues 
Jitendra Singh ShahiJitendra Singh Shahi

No you can't use that as there is no form field in draft values.

here is code you have to do:

let data = [{"Date_Reviewed":"1989-05-06","Date_Sent":"1989-05-05","editable":true,"Form":"Contract Revision 1",
	"Id":"a0S3F000001bXAIUA2"}, 
    {"Date_Reviewed":"2019-02-01","Date_Sent":"2019-02-01","editable":true,"Form":"Contract Release","Id":"a0S3F000001bXAJUA2"}];
    
let editedRecord = component.find("riderStatusDataTable").get("v.draftValues");

let releaseData = data.filter(dataVal => dataVal.Form == "Contract Release");

let TemporaryArray1 = {'sobjectType':'Reader_Status__c'};

editedRecord.forEach((editObj) => {
	releaseData.forEach((elem, index) => {
    	if(elem.Id == editObj.Id){
        	TemporaryArray1.Date_Sent__c = editObj.Date_Sent;
        	TemporaryArray1.Date_Reviewed__c = editObj.Date_Reviewed;  
        }
    })
})
This was selected as the best answer
Jitendra Singh ShahiJitendra Singh Shahi

Can you please post this question in another thread as it becomes a very big thread. and if somebody came here he would not understand.

so if your previous query is resolved please mark the above answer as the best answer. And then post new question and share here a link so I can look into it.

VRKVRK
editedRecord.forEach((editObj) => {
        releaseData.forEach((elem, index) => {
            if(elem.Id == editObj.Id){
                valid = true;                                           // this line added now
                TemporaryArray1.Date_Sent__c = editObj.Date_Sent;
                TemporaryArray1.Date_Reviewed__c = editObj.Date_Reviewed;  
            }
       
        })
    })
Above code , i added valid = true, and write condition in other areas, when this is true, then only execute other part. 
So, now its working perfectly.
can you pls suggest me, is there any materials / documents for solve this type of scenarios ?
Thank you very much  your help on this.
 
Jitendra Singh ShahiJitendra Singh Shahi
Hi,
Nice to hear, It worked for you. Sure as this is a lightning problem but if I say technically it is a javascript problem. Like filtering an array and looping through the array. In your case, it is not a simple array it is an array of an object. You can learn more about javascript new ways of doing stuff from the Mozilla javascript guide. I have added the Mozilla link below you can check and learn. Happy Coding !!😊

https://developer.mozilla.org/en-US/docs/Web/JavaScript
VRKVRK
Hi Jitendra,
can u pls check below link and let me know any help on this
https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Lightning&criteria=OPENQUESTIONS&id=9062I000000QynlQAC