0

I am receiving apex errors with the following code:

public with sharing class AddExistingSpecsExt {

    public Specifications__c loadedSpec { get; private set; }

public AddExistingSpecsExt() {
    String specId = ApexPages.currentPage().getParameters().get('id');
    if (String.isBlank(specId)) return;

    List<Specifications__c> candidates = [
        SELECT Id, Name, Product__c FROM Specifications__c WHERE Id = :specId LIMIT 1
    ];
    if (!candidates.isEmpty()) loadedSpec = candidates[0];
}
    public PageReference associate () {

        Specifications__c candidates = new Specifications__c ( Id = Specifications__c.Product__c, Name = Specifications__c.Id);

        try {
            Database.update(candidates);
        } catch (Exception error) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error while associating.' + error.getMessage()));
        }

        PageReference page = new PageReference('/' + candidates);

        return page.setRedirect(true);
    }

    public PageReference cancel () {
        PageReference page = new PageReference('/' + Null);
        return page.setRedirect(true);        
    }

}

Attempting to add records to a related list through a custom button and small vf page.

The Error is Line 16

Invalid initial expression type for field Specifications__c.Id, expecting: Id

Any help?

1
  • Name = Specifications__c.Id instead Name=loadedSpec.Id also Specifications__c.Product__c should be loadedSpec.Product__c – Ratan Paul Feb 26 '16 at 17:59
2

You are confusing data and metadata. So instead of getting a value, you are getting SObjectField.

Id data = loadedSpec.Id;
SObjectField metadata = Spefications__c.Id;

You need to use the former (data) in your code:

Schema.SObjectField idMetadata = Specifications__c.Id; // you don't need this
Id idData = loadedSpec.Id;

SObjectField productMetadata = Specifications__c.Product__c; // you don't need this
String productData = loadedSpec.Product__c;

Specifications__c record = new Specifications__c(Id=idData, Name=productData);

Also, you don't even need to instantiate a new Specifications__c record at all. Just do:

loadedSpec.Name = loadedSpec.Product__c;
//update logic

One last note, you should return null in your catch block after calling ApexPages.addMessage.

7
  • Whoa, going over my head now. Where would I put the former and fix the SObject vs SObjectType issue? – Jason Feb 26 '16 at 18:03
  • @Jason Check the updated answer. – Adrian Larson Feb 26 '16 at 18:07
  • 1
    You want the data and not the metadata of the object. That is what he is trying to say; – karthikselva Feb 26 '16 at 18:15
  • 1
    Thanks @karthikselva, that distinction is much better than the one I had. – Adrian Larson Feb 26 '16 at 18:17
  • 1
    I haven't seen any specific use of retrieving the metadata like this, but its really good one to know. Your answer has great depth on the code, Thank you :) – karthikselva Feb 26 '16 at 18:30
0

I wonder why you are assigning the ID into a Name field and a lookup (I assume it's a lookup) into the ID field.... but that could be the correct logic...

Specifications__c candidates = new Specifications__c ( Id = Specifications__c.Product__c, Name = Specifications__c.Id);

If Product__c is not an ID field, you will get an error.

1
  • 1
    see Adrian's answer for an explanation on how to use a variable vs the actual sObject – Sebastian Kessel Feb 26 '16 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.