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
Big developersBig developers 

Problem getting SObject

So, I have a Rules object. Within the Rules object is an array of Condition objects. Each Condition Object has an associated Attribute_Definition object.

 

I'm trying to get the name of the Attribute_Definition object associated with each condition object.

 

                Rule_Condition__c[] condList = [select Attribute_Definition__c, Operator__c,
                                            Text_Value__c, Number_Value__c, IndexVar__c  
                                     from Rule_Condition__c where Configuration_Rule__c = :bmRule.Id];
                for (Integer j = 0; j<condList.size(); j++) {
                    if (('' +j).equals(s)) {
                        Attribute_Definition__c attrDef = new Attribute_Definition__c(condList[j].Attribute_Definition__c);
                        return attrDef.Name + ' OPERATOR val '; // RETURN THE ENGLISH
                        //return condList[j].Attribute_Definition__c + ' OPERATOR val '; // RETURN THE ENGLISH
                    }
                }
                
                return '!]error03 - ' + s;

 

 

Based on some brief testing, I know that s and j are firing true. If I remove the .Name tag, it'll pring out a01A0000001aR1vIAE

 

Error is this:

Error: Compile Error: Illegal assignment from Id to SOBJECT:Attribute_Definition__c at line 84 column 26

 

 

Thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
Jeremy.NottinghJeremy.Nottingh

It looks like you're sort of mixing up object names, field names, and relationships here. Let me explain what I mean.

 

You have two objects, Rule_Condition__c and Attribute Definition__c. Rule_Condition__c has a field called Attribute_Definition__c, which stores the ID of the associated Attribute_Definition__c object. This field defines the relationship between these two objects, which is then referred to as Attribute_Definition__r, because you take the "c" off the field name and replace it with an "r" to denote a relationship. If your field were called Attribute__c, it would point to the Attribute_Definition__c object, and would define the relationship Attribute__r. Your case is somewhat confusing because the field and the object have the same name.

 

So when you query Rule_Condition__c with

Rule_Condition__c rc = [Select Attribute_Definition__c from Rule_Condition__c limit 1];

you will receive an object with a field that holds an ID value. rc.Attribute_Definition__c = '0a30...0348aCe897' or something. Which isn't what you want.

 

This is why you got the error, because

 

Attribute_Definition__c attrDef = new Attribute_Definition__c(condList[j].Attribute_Definition__c);

 is trying to create an Attribute_Definition__c object, and you're handing it the value of the Attribute_Definition__c field, which is just an ID string, not an object.

 

Since you are really looking for the Name of the associated Attribute Definition, you can use the relationship name to query the other object.

Rule_Condition__c rc = [Select Attribute_Definition__r.Name from Rule_Condition__c limit 1];
system.debug('The name of the Attribute Definition object is ' + rc.Attribute_Definition__r.Name);

In this case, rc.Attribute_Definition__r.Name is the Name of the object you're looking for. You don't have to do any more looping or checking stuff, it's right there in your query result. See if that answers your questions.

 

Jeremy

All Answers

WesNolte__cWesNolte__c

Hey

 

So it fails if you uncomment the last line (and comment out the other return statement) ?

 

What's the return type of your method?

 

Wes

Jeremy.NottinghJeremy.Nottingh

It looks like you're sort of mixing up object names, field names, and relationships here. Let me explain what I mean.

 

You have two objects, Rule_Condition__c and Attribute Definition__c. Rule_Condition__c has a field called Attribute_Definition__c, which stores the ID of the associated Attribute_Definition__c object. This field defines the relationship between these two objects, which is then referred to as Attribute_Definition__r, because you take the "c" off the field name and replace it with an "r" to denote a relationship. If your field were called Attribute__c, it would point to the Attribute_Definition__c object, and would define the relationship Attribute__r. Your case is somewhat confusing because the field and the object have the same name.

 

So when you query Rule_Condition__c with

Rule_Condition__c rc = [Select Attribute_Definition__c from Rule_Condition__c limit 1];

you will receive an object with a field that holds an ID value. rc.Attribute_Definition__c = '0a30...0348aCe897' or something. Which isn't what you want.

 

This is why you got the error, because

 

Attribute_Definition__c attrDef = new Attribute_Definition__c(condList[j].Attribute_Definition__c);

 is trying to create an Attribute_Definition__c object, and you're handing it the value of the Attribute_Definition__c field, which is just an ID string, not an object.

 

Since you are really looking for the Name of the associated Attribute Definition, you can use the relationship name to query the other object.

Rule_Condition__c rc = [Select Attribute_Definition__r.Name from Rule_Condition__c limit 1];
system.debug('The name of the Attribute Definition object is ' + rc.Attribute_Definition__r.Name);

In this case, rc.Attribute_Definition__r.Name is the Name of the object you're looking for. You don't have to do any more looping or checking stuff, it's right there in your query result. See if that answers your questions.

 

Jeremy

This was selected as the best answer
Big developersBig developers

 That worked perfectly, Jeremy. Thanks a lot!