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
rickynrickyn 

Need help creating a Map

I am seeking to develop a Map which can hold selected values from a user on a viusal force page

 

I have three variables in context:

 

1) Service_Area__c - SObject

2) Postal Code - String

3) Product2 - SObject

 

Each Service Area selection has a Postal Code and each Service Area/Postal Code combination has multiple Product selections.

 

My logic is to combine Service_Area__c and Postal Code in one virtual object called eServiceArea.

 

I am attempting to create a map like the following:

 

Map<eServiceArea, List<Product2>> Map_ServiceAreaList_ServiceTasksList = new Map<eServiceArea, List<Product2>>();

 

Using some code I found in a wrapper class, assuming its relevant:

public class eServiceArea {
        public Service_Area__c SA {get; set;}
        public string PostalZipCode {get; set;}

        public eServiceArea (Service_Area__c eSA, String ePostalZipCode) {
            SA = eSA;
            PostalZipCode = ePostalZipCode;
        }
    }

 The map will not allow me to save with eServiceArea.  I am not sure if creating a eServiceArea Class is what I need, please provide some direction.

 

Thanks,

 

RN

Mike@COLMike@COL

I want to make sure I understand this correctly:

1) There is a 1 to 1 relationship between Service_Area__c and Postal Code.

2) There is a 1 to many relationship between Service_Area__c and Product2.

 

Assume I've got that right, I woudl recommend a class like this:

 

public class eServiceArea {
        public Service_Area__c SA {get; set;}
        public string PostalZipCode {get; set;}
	public List<Product2> Products { get; set;} 

        public eServiceArea (Service_Area__c eSA, String ePostalZipCode) {
            SA = eSA;
            PostalZipCode = ePostalZipCode;
	    Products = <get your list of products however you want>
        }
    }

 

Depending on how you need to access the data, you could use a list of eServiceArea objects, or a map based on Service_Area__c.Id or PostalZipCode. Examples are below:

List<eServiceArea> myList = new List<eServiceArea>();
Map<Id,eServiceArea> myMapById = new Map<Id,eServiceArea>();
Map<String,eServiceArea> myMapByPostalCode = new Map<String,eServiceArea>();