Hello,
I have a local variable which has data type Employee Record List . and this variable contains some list of records and now I wanted to delete one record
and here I first used listIndexOf method which should return position which going to delete and then using ListRemove method but positon from first method returning -1 everytime ,
I have checked condition many time but it still return -1,
conditon I used there is ( EmployeeList.Current.Employee.Id = JavaScript1.RandomIndex)
and finally passing input for listRemove method output of listIndexof method.
Let me know anything you wants to know from myside
Regards
Vish Holge
Exactly, but that is not what he is doing.
So his problem is not the ability to generate a random number, but the fact that he uses that against is instead of against position in list
Hi Vish,
Please make sure that Javascript returns correct Id that is existing into Employee List.
You can debug your code and check value returned from JavaScript code.
I debuged it, value is coming from javascript but when I put that value in condition of listIndexOf method positon is recturning -1,
javascripts return integer value and I am comparing it with list id which has data type of list Identetifier
Can you please show me screenshot of properties of ListIndex action?
showing list you are searching inside and condition.
Also while you are debugging, can you check if list of Employees is empty or not
Here is properties of ListIndexOf method
and in condition I have done with converting integer to Identifire ,
talking about Employee list while debugging list is not empty has records showing while degbug
If you can share this sample into OML this may help to discover issue here
You have two issues into your OML:
First one you are using ListIndex for any empty list which is Employees. you are never filling it will data before calling your action "GeneratenameOnClick".
you should use DataAction1.out1 as below or add OnAfterFetch action and assign Output of FetchData action to your List Employees
Second issue in your JavaScript code you are using length of list which may be for example 10 that's not meaning or grantee that if you will generate number from 1 to 10 it will be equal to one of ids of employee table
(Please note that 0 is never used as an Id into autogenerate table identifier as 0 is null value into integer data type into OutSystems)
Hello Vish,
You should check that the returned index is valid ( exists in your list ) to avoid index out-of-bounds errors and ensure that the index is found.
One way to accomplish this is to use a component like this one which allows you to set a range of max and min values for your random numbers. This can give you some control over the value generated.
If you wish to use JS then I would suggest this code :
function getRandomIntInclusive(min, max) {
min = Math.ceil(min); max = Math.floor(max);
return Math.floor(Math.random() * (max-min + 1) + min);
// The maximum is inclusive and the minimum is inclusive
}
This allows you to set max and min values.
Hope it helps !
Paulo Rosário
Thanks Paulo,
I have used exactly same code in javascripts but there is no error in javascript it is return random number but when I appying it in condition it position is returning -1
It is a silly idea to use a random number on a limited list of identifiers.
How are you determining the min and max ?
Do all numbers between min and max exist in your database ?
Even with an autonumber starting at 1, there is no guarantee, after you start deleting records.
What subset of your full set of existing id's is present in your local list (i.e. MaxRecords on retrieval)
What on earth do you need this for ? If it is just for the sake of exercise, use length of list and currentRowNumber, maybe.
If it is for the sake of picking a random record, count total, than have an aggregate with MaxRecord of 1 and as StartIndex a random number between 1 and count, for example.
Dorine
And here's another id (see answer by Nuno) to pick a random one if you are willing to use SQL.
I understood that he wanted to remove a random position of the list. The main and max in that case would be 0 , the starting position of the list , and list.lenght-1 for the max, this would restringe the possible values to the existing index's.
Except that he is looking at entity identifier, not position in list
Exactly, as he currently using list length and returning random index but he trying to filter his list based on condition Entity Id = Returned index from JS.
He should use this index returned from JS to remove element directly without ListIndex
But if the objective is to remove a random employee that comparison is not needed . He can simply remove the element from the list using the list remove and the returned index.
In that case , add an aggregate to get all the employees , group by id ( that will give you a list of the available employee IDs ) then use a component like This one to get a random id from that list. Then you will be able to compare the random Id with the ones on your employee list
Hello , paulo
thanks a lot , as you I did it and its working now , the reason I was using ListIndexOf , intially I was using removeList inside for each then someone on this forum told me we have to always use removList next to IndexOf method ,
Now its working ( deleting as I remove ListIndexOf method)
but I have one more doubt is now I am deleting record thas ( id, Name, Email) but can I store name in local variable before deleting it (record) cause I need to show on screen that name ,,
I have tried to used foreach loop and in that applied condition if current id = javascripts return no then I was removeing element but it was showing error that list is being iterated remove is not allowed .
Hello Vish ,
Of course, just add a local variable of type employe and assign the selected employee that you are going to delete to that variable before you delete them. This will give you a local copy of that record.