Page 1 of 2

Invoking Service and running a loop on Success Event on Mapping

Posted: Mon Jan 25, 2016 7:04 am
by Jack Bua

I originally posted about this here:

https://getsatisfaction.com/apperyio/...

The idea is the same; map a service response's $ to a label in a list item. On the mapping I invoke a separate service and trigger a loop in its success event that "sums" up a field.

What I cannot get to work is taking that "sum" and returning it as the desired value to be mapped to the list's label.

This is my code so far:

var token = Apperyio.storage.user_session_token.get();
var jli = value.id;
var whereObject = {job_lineitem_ptr: {"$inQuery": {id: jli } } };

var onSuccess = function(data)
{
var sum = 0;
for (var i=0; i 0)
{
total = Number(total);
}
else
{
total = 0;
}
console.log("Total from mapping: " + total);
return total.toFixed(2) + " hrs";

And this is what is written to the console log:

Image

The values seen in the console after "Total from Mapping: " (167) is also what is mapped to all of the labels in the list.
The summing logic appears to be correct. In this case, there are four list items, and this code is being ran once for each one. The loop code is correctly being ran a total of six times, (2 times for two list items, once for the other two).

The issue is getting the summed value back to the mapping and properly mapped to the list item's label. How can I do this?


Invoking Service and running a loop on Success Event on Mapping

Posted: Mon Jan 25, 2016 3:11 pm
by Serhii Kulibaba

Hello Jack,

Could you also show us you mapping and list item with that values?


Invoking Service and running a loop on Success Event on Mapping

Posted: Mon Jan 25, 2016 7:11 pm
by Jack Bua

Sure.

Mapping:
Image

List Item (it is actually a collapsible block header):
Image


Invoking Service and running a loop on Success Event on Mapping

Posted: Mon Jan 25, 2016 8:56 pm
by Serhii Kulibaba

Please provide us with a public app link (https://devcenter.appery.io/documenta...) and exact steps to reproduce this problem.


Invoking Service and running a loop on Success Event on Mapping

Posted: Mon Jan 25, 2016 9:09 pm
by Jack Bua

I just realized that the code I pasted into this post did not save correctly. Unfortunately I made changes to the code since then. I could get the original code if I can edit the original posting, but that ability seems to be removed for me now.Is there a way for me to be able to edit the first post on this page real quick so I can grab the original code and put it back into my app?


Invoking Service and running a loop on Success Event on Mapping

Posted: Mon Jan 25, 2016 9:14 pm
by Jack Bua

Or create an app from a publish? I think the original code is sitting, unused, on the published version of my app.


Invoking Service and running a loop on Success Event on Mapping

Posted: Mon Jan 25, 2016 9:26 pm
by Jack Bua

Never mind, I have successfully reproduced the code. I am sending you the login info and steps to reproduce the problem as a reply to your email for the related priority ticket.


Invoking Service and running a loop on Success Event on Mapping

Posted: Tue Jan 26, 2016 3:38 am
by Jack Bua

UPDATE: Issue still exists, but the structure of the collapsible set changed a little. Here are updated pictures of what you requested.

Image

Image


Invoking Service and running a loop on Success Event on Mapping

Posted: Tue Jan 26, 2016 6:56 pm
by Jack Bua

Ok, so now that figured out that I need to use the html code tags to insert code into this post, here is my code:

code
var token = Apperyio.storage.user_session_token.get();
var jli = value.id;
var whereObject = {job_lineitem_ptr: {"$inQuery": {id: jli } } };

var onSuccess = function(data)
{
var sum = 0;
for (var i=0; i<data&#46;length; i++)
{
if(data&#46;minutes === undefined)
{
sum +=+ 0;
}
else
{
sum +=+ data&#46;minutes;
}
}
Apperyio&#46;storage&#46;selected_time&#46;update("$['minutes']", sum);
&#47;&#47;I need the value sum from here
console&#46;log("Sum from Loop: " + sum);
};

view_lineitems_actual_hours_calc&#46;execute({
headers: { "X-Appery-Database-Id": "XXXXXXX" , "X-Appery-Session-Token": token } ,
data: {"where": JSON&#46;stringify(whereObject)} ,
success: onSuccess
});

var total = Apperyio&#46;storage&#46;selected_time&#46;get("$['minutes']");
&#47;&#47;I need to value sum to go here

console&#46;log("Total from mapping: " + total);
return total&#46;toFixed(2);
/code

My issue is arising from my usage of local storage in that function. Is there a better way I can do this?


Invoking Service and running a loop on Success Event on Mapping

Posted: Tue Jan 26, 2016 10:44 pm
by asid

Hi Jack,
I have not attempted to do what you are trying within the mapping, but I would normally do all this type of work server side and then just return the totals as part of the record.