function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
navaneetha.cm1.3887470963815178E12navaneetha.cm1.3887470963815178E12 

How the list gets updated with additional values while returning back to a main method from a submethod

I have 2 methods, main and a submethod. In main method I will have a list having repeated items. In the submethod I have written a logic to remove duplicate. But when the list is returned back it will have extra values.

For example: 
Main method() {
         list<object> sampleList = {{color: red, qty:1}, {color : blue, qty: 1},{color: red, qty:1}, {color : blue, qty: 1}};
         submethod(sampleList);
         Here the List will have {{color: red, qty:2}, {color : blue, qty: 2},{color: red, qty:1}}; extra value added
         Instead of  {{color: red, qty:2}, {color : blue, qty: 2}};

}

submethod (list<object> sampleList) {
                        logic to remove duplicate;
                        updated list will now have  {{color: red, qty:2}, {color : blue, qty: 2}}; Only 2 values
       return;     
}

Can anyone suggest how that additional value gets added and solution to avoid it?
Gil GourévitchGil Gourévitch
Hi,
Did you try something like this :
Main method : 
List<object> result = submethod(sampleList);

Submethod : 
List<object> result = new list<object>();
logic to remove duplicate;
result = updated list;
return result;

Hope this helps
Gil

Question Solved ? Please mark as the best answer to help other users !
navaneetha.cm1.3887470963815178E12navaneetha.cm1.3887470963815178E12
yes. In submethod I am again initializing that result list once again and assigning values.

Gil GourévitchGil Gourévitch
The key points here are :
- the variable assignment from the submethod :
List<object> result = submethod(sampleList);
- the return statement in the submethod
return result;
Hope this helps
Gil

Question Solved ? Please mark as the best answer to help other users !