0

I have a problem. I need to be able to return properties if they have opportunities in "test". There isn't a relationship from property down to opportunity. But there is a relationship from opportunity up to property.

(Hope im saying that right)

Anyways, is there a way to get properties if they have opportunities in a certain category?

@AuraEnabled   
        public static List <Opportunity> searchProperty(String searchString) 
        {
            //query opportunities. Put project name in a list. return the list.
                String searching = searchString + '%';
                List <Opportunity> returnList = new List <Opportunity> ();
                List <Opportunity> lstOfProperties = [select Property__r.name, name from opportunity
                                                      where recordtype.name in ('Test') 
                                    and Property__r.Name LIKE: searching and Probability not in (0,5,100)
                                    order by Property__r.Name].Property__r.Name;

               for (Opportunity acc: lstOfProperties) 
               {
                 returnList.add(acc);
               }
                 system.debug(returnList);
                 return returnList;     
       }

2
  • Opportunity is parent and property is child ? Commented Sep 11, 2019 at 19:07
  • Opportunity has a lookup relationship to property but property doesn't have a relationship to opportunity. This would make property the parent i think, right?
    – Tyler
    Commented Sep 11, 2019 at 19:10

1 Answer 1

1

There isn't a relationship from property down to opportunity. But there is a relationship from opportunity up to property.

This sentence does not make much sense. Instead you can say which object is Parent and which object is Child - And if necessary, you can also mention whether the relationship is lookup or master-detail. In your case, Property is parent.

Now coming to your problem, you can use below query to return properties which has related test opportunities:

List<Property__c> propertiesWithTestOpps = [
    SELECT Id, Name
    FROM Property__c 
    WHERE Id IN (SELECT Property__c 
                 FROM Opportunity 
                 WHERE recordtype.name in ('Test'))
];

Assuming lookup field name in opportunity is Property__c and also the object name of property is Property__c.

3
  • So both of those are correct, and i get what you mean. If theres a relationship between one theres a relationship between both. But using your line of code i get Invalid type: Schema.Property
    – Tyler
    Commented Sep 11, 2019 at 19:31
  • As i said assuming api name is Property__c. What is the object api name of property object? Commented Sep 11, 2019 at 19:33
  • I apologize i had a typo
    – Tyler
    Commented Sep 11, 2019 at 19:37

You must log in to answer this question.

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