0

I have a user input search bar where the user will input their "City", "Address", and "State". Then the user will click the search button, which will change to a Boolean value to equal 'true'. This Boolean value is then past to my function which will go though a If/Else condition. If the Boolean = 'true': then it includes the 'City' and 'State' (So I can get the 9 (or less) recent "Signed" opportunities from the 'city' & 'State' to the array). Else the Boolean will just set to the 9 most recent "Signed" opportunities to the array (without narrowing the array down via 'city' & 'state').

The error message: enter image description here

mapping.js Code (with SearchClick button):

(UpdateOpportunities is the Boolean value)

// Method for when the search button is clicked in the "mappingBox" LWC.
    SearchClick(message) {

        if(message.operator == true){

            // The address entered into the search bar.
            const ArrStreet = SearchString.split(',');
            SearchStreet = ArrStreet[0];

            // The town or city entered into the search bar.
            const ArrCity = SearchString.split(',');
            SearchCity = ArrCity[1];

            UpdateOpportunities = true;

            // Updates and locates the location searched.
            this.mapMarkers = MapMarkers(SearchCity, SearchState, SearchStreet, ClickData, UpdateOpportunities);

        }

    }

MapMarkers.js Code:

export function MapMarkers(SearchCity, SearchState, SearchStreet, Opportunities, UpdateOpportunities){
   
   let NineRecentOpportunities;
   let validStages;
   let validCity;
   let validState;

   if(UpdateOpportunities == true){

       // Sets the 9 most recent "Signed" opportunities in the searched: 'City & State' to the array.
       validStages = ['Signed', 'Signed LOI'];
       validCity = [SearchCity];
       validState = [SearchState];
       NineRecentOpportunities = Opportunities.filter(
       (record) => validCity.indexOf(record.Service_City__c) > -1, validState.indexOf(record.Service_State__c) > -1, validStages.indexOf(record.StageName) > -1 ).slice(-9);


   } else {

       // Sets the 9 most recent "Signed" opportunities to the array.
       validStages = ['Signed', 'Signed LOI'];
       NineRecentOpportunities = Opportunities.filter(
       (record) => validStages.indexOf(record.StageName) > -1 ).slice(-9);

   }

Refined Question:

The Else-statement seems to be working for me giving me all the 9 most recent "Signed" opportunities to the array. But the If-statement isn't narrowing the array down by 'city' & 'state' of the 9 or less recent "Signed" opportunities. Is it because It will fail if their are less-then 9 "Signed" opportunities in that city, or is it the Boolean value that's not pasting correctly?

1 Answer 1

1

You need to use the && (and) operator to check more than one condition.

NineRecentOpportunities = Opportunities.filter(
    (record) => (
       validCity.indexOf(record.Service_City__c) > -1 && 
       validState.indexOf(record.Service_State__c) > -1 && 
       validStages.indexOf(record.StageName) > -1
    )
).slice(-9);

Comma has some special behavior based on context. Inside of a function call parameter list, the comma separates the individual parameters. Therefore, it was as if you were only filtering by one condition, the city. When the second parameter tried to be evaluated, it was undefined in any scope, because you were now outside the arrow function.

The outer parentheses after => avoids potentially ambiguous code reading, and the && operator evaluates the left-hand operand first, and only if that operand is truthy, it then evaluates the right-hand operand. This means that the code runs very slightly faster, since it knows that the right-hand operand can't make the result true.

8
  • I get a error: "[Cannot read properties of undefined (reading 'Service_Street__c')]" when I try searching my location. I this because I have less then 9 recent "Signed" opportunities?
    – Pinto
    Commented Apr 24, 2023 at 17:29
  • @Pinto The error isn't in this code, as there's no Service_Street__c in the code on this page. My guess is that you have a magic number in your code somewhere, for example, for(let i = 0; i < 9; i++) { or some variant, where you run off the end of the Array into undefined records.
    – sfdcfox
    Commented Apr 24, 2023 at 17:34
  • Yes, I have a for-loop exactly like the one you shown. It "creates a address string, and stores them to an array." So I can display it to the marker-box. can I do? for(let i = 0; i < NineRecentOpportunities.length; i++) {
    – Pinto
    Commented Apr 24, 2023 at 17:42
  • 1
    @Pinto You could, but I'd really suggest you make use of the Array.map function: let markers = NineRecentOpoortunities.map((record)=>({ location: { Street: record.Service_Street__c, City: record.Service_City__c, ... }})); This is a fairly common style of code to write in JS. Let a code formatter pretty this up for you, I can't include newlines in comments.
    – sfdcfox
    Commented Apr 24, 2023 at 17:50
  • 1
    @Pinto Probably. We're pretty deep in comment territory.
    – sfdcfox
    Commented Apr 24, 2023 at 18:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .