Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Invoking Service and running a loop on Success Event on Mapping

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?

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Invoking Service and running a loop on Success Event on Mapping

Hello Jack,

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

Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Invoking Service and running a loop on Success Event on Mapping

Sure.

Mapping:
Image

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

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Invoking Service and running a loop on Success Event on Mapping

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

Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Invoking Service and running a loop on Success Event on Mapping

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?

Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Invoking Service and running a loop on Success Event on Mapping

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

Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Invoking Service and running a loop on Success Event on Mapping

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.

Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Invoking Service and running a loop on Success Event on Mapping

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

Image

Image

Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Invoking Service and running a loop on Success Event on Mapping

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?

asid
Posts: 0
Joined: Sun Aug 09, 2015 8:01 pm

Invoking Service and running a loop on Success Event on Mapping

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.

Return to “Issues”