Code inspection shows wrong illegal assignment for lookup fields

Issue #809 resolved
Andreas Epping created an issue

I am trying out Illuminated Cloud 2.0 and I find the code inspection extremely useful. However if I want to set the ID of a lookup record to a lookup field, code inspection suggests that this is an illegal assignment and recommends a type cast. If I use the type cast, I cannot deploy the apex file. I will get a compilation error. I searched the web, if my APEX knowledge is wrong but all examples do the same. Could you please give me an advice how to assign the ID to a lookup field so that IlluminatedCloud will not mark it as an illegal assignment? Or correct the issue. Thanks

2017-12-14_19-54-50.png

Comments (15)

  1. Scott Wells repo owner

    Thanks for filing, Andreas. Let me take a look and see why it's reporting that. I'll keep you posted on what I find.

  2. Scott Wells repo owner

    Andreas, a couple of questions here. What is the data type of order.OwnOrgUnit__c? What is the message that's being reported by the code inspection and what option is the intention providing for casting? I ask because I'm so far unable to reproduce this. And one other question...have you by chance regenerated your offline symbol table for IC2? My guess is yes, but if you haven't would you mind doing that and see if it clears up the issue?

    Anyway, assuming it doesn't, just let me know the data type of that field and the messages from the inspection and intention and hopefully that will give me some insight into what's going on.

  3. Andreas Epping reporter

    Hi Scott, thanks for your investigations so far. Order and OwnOrgUnit are custom objects. The field order.OwnOrgUnit__c is a lookup from Order to OwnOrgUnit and expresses, which org unit of a company is responsible for processing the order. And yes, I have regenerated the offline symbol table for IC2.

  4. Scott Wells repo owner

    Thanks, Andreas. So just to clarify, the data type of OwnOrgUnit__c is Id, is that correct? And do you mind using Ctrl+F1 / Cmd+F1 (Windows / Mac) to show the actual reported error and provide that here? Also, if you could use Alt+Enter / Opt+Enter to show what it's recommending in terms of the cast, that would help. I'm trying to see what it things the left-hand and right-hand sides of the assignment are to understand why it things it's an illegal assignment.

  5. Andreas Epping reporter

    Hi Scott, here is a complete Screenshot. IC2 recommends to do a cast to type OwnOrgUnit__c. If i follow the recommendation, the line will look like this:

    order.OwnOrgUnit__c = (OwnOrgUnit__c) standardOwnOrgUnit.Id;

    However, this cannot be deployed to Salesforce.

    2017-12-15_15-20-43.png

  6. Scott Wells repo owner

    That helps. And one more request...do you mind either using Cmd+B or Opt+Space/Cmd+Y to show the definition of OwnOrgUnit__c so I can see its data type? I'm sure it's Id, but I just want to see what might be happening here. Thanks!

  7. Andreas Epping reporter

    Hi Scott,

    I don't securely know what you want me to do. If I hit Cmd+B (using Mac) IntelliJ opens the definition of the SObject OwnOrgUnit__c. I doubt that you want that to be shown. I think you want to see the SObject Order__c and within that SObject the definition of the field OwnOrgUnit__c ... right? I am not so familiar with all the keystrokes and couldn't manage to open it by keystroke. However I navigated to the OfflilneSymbolTable.zip and copied this part out of the SObject Order__c:

    `global class Order__c extends SObject

    {

    ...

    global Id OwnOrgUnit__c;
    
    global OwnOrgUnit__c OwnOrgUnit__r;
    

    ...

    }`

  8. Scott Wells repo owner

    That's what I wanted, Andreas. Custom relationship fields are rendered into the offline symbol table as two member variables, the ID field which ends with __c and the relationship field which ends with __r. I wanted to verify that the former was in fact of type Id in the OST. You've verified that. Now let me see if I can reproduce with all of this info. Thanks!

  9. Scott Wells repo owner

    Oh, you know...I see the problem now. You have an SObject named OwnOrgUnit__c with a field named....OwnOrgUnit__c. I'm 99% sure that IC is evaluating order.OwnOrgUnit__c improperly and is thinking that reference to the field is instead a reference to the object. And if you said that previously in the this discussion, my apologies for now grokking it earlier! That should be much easier to reproduce, though the fix might be a little more tricky. I'll keep you posted here.

  10. Andreas Epping reporter

    The SObject is not OwnOrgUnit__c it is Order__c. So the assignment is of type Order__c.OwnOrgUnit__c = OwnOrgUnit__c.Id. However I found a second issue. I cannot change the Inspection settings in the way that IC2 shows this issue as a warning and not as an error. Should I file a second bug?

  11. Scott Wells repo owner

    On the first, from what I can tell the problem is that you have an SObject with the name OwnOrgUnit__c and a field with full name Order__c.OwnOrgUnit__c. IC is erroneously resolving the field reference order.OwnOrgUnit__c to the SObject.

    As for not being able to change the inspection settings, yes, please file another bug on that if it's not respecting the desired severity. You can also suppress the inspection on that one line without disabling/changing the inspection settings globally by typing Opt+Enter while on the error, then ArrowRight, then the bottom option is Suppress for Statement. It will add a //noinspection comment above the line to tell the inspection not to report the issue on that one line. I would recommend doing that until I issue a fix so as not to completely turn off that inspection.

  12. Scott Wells repo owner

    Andreas, sorry for the long delay getting back to you on this. I just modeled objects and fields matching the ones giving you an issue and entered the following code without any errors:

        public void fixIssue809(Order__c[] orders)
        {
            OwnOrgUnit__c standardOwnOrgUnit;
            for (Order__c order : orders)
            {
                order.OwnOrgUnit__c = standardOwnOrgUnit.Id;
            }
        }
    

    Can you make sure you're updated to the latest build, make sure that your OST is up-to-date, and let me know if you're still seeing this issue? Thanks!

  13. Andreas Epping reporter

    Hi Scott, thanks for your mail. It is working right now. Thanks. Beste Grüße / Kind Regards Andreas Epping

  14. Log in to comment