0

i have two classes. in the first class account object having two lookup fields like "ship from" and "ship to". but these lookup object is one that is Address__c object. these lookup fields have different records.

totalrecords.totalamount(Address__C.Ship_To__c, Address__C.Ship_From__c, contact.type);

enter image description here my requirement is how to pass these two object values to second class in this first set object parameter testcal(Set<Address__c>AddressSet, string type). like below method

public static Decimal testcal(Set<Address__c>AddressSet, string type){
  }
1
  • Why do you want to pass the Set when you are just updating/using one field based on the type? I would say that we can optimize the solution if you can provide more details about what you have done and what you want to achieve. Commented Dec 27, 2018 at 17:56

1 Answer 1

2

First, you need to create a Set object, typed to match whatever object type you're working with, in this case, Address__c.

You can add a value to a list or set using the add method. This will add its value to the collection, increasing its size. Worth reading the documentation for the set class.

The values stored in your fields are Id's and not the actual records - so we need to run a query (or just pass the ids and modify the caller to query the records) before calling your method.

To do that, we need to get all our ids in a list, and then create a second set based on the ids from our first set. I've written a demo below.

// Get our specific address
Addresss__c someAddress = [SELECT ID, Ship_To__c, Ship_From__c FROM Address__c WHERE Id = :someID]; // get some id or marker for query to make it more selective 

Set<Id> addressIds = new Set<Id>(); // create a new variable 

addressIds.add(someAddress.Ship_To__c); // add this variables field value to our set 
addressIds.add(someAddress.Ship_From__c); // add another field 

// Create another set of records based on the query results, filtered by our id list 
Set<Addresss__c> addresses = new Set<Addresss__c>([SELECT Id, Other_Fields__c FROM Addresss__c WHERE ID IN :addressIds]);

TestCal(addresses, Contact.Type); // call your method, passing along our set & type 

One thing I'm not totally sure about is your use of Address__c.Ship_To__c. The __c implies the object type, and not an instance of that type. You'll get a field describe back instead of whatever record's value you want. You'll need to identify your record (probably by an id or passing it from another context), and query directly for the field values you need. Best to ask another question if you have trouble with it.

2
  • Thanks for your reply. for single record it will fine but how to pass list of address values in for loop.
    – Goldberg
    Commented Dec 28, 2018 at 6:24
  • Gonna have to see some more code to provide anything useful there. Not even sure how you're managing the record you have, let alone the list you're using. If you just need a bunch of addresses in a list, you can iterate over your list of records and just add their fields to the same address list. Otherwise you might need to call this method per record or use a Map<ParentId, List<Address__c>> to keep the records related to their parents. Commented Dec 28, 2018 at 14:35

You must log in to answer this question.

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