• Emo514
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies

I'm following the Apex example and have created a Line Item object. I am trying to get the names and values for each of the fields in the object. I've reduced the example to the following fields: Created By, Unit Price and Merchandise (name of the product sold)

 

I have tied a visual force page to a button that when clicked activates an Apex class.

<apex:page standardController="Line_Item__c" extensions="VFController" action="{!autoRun}">
  <apex:messages />
</apex:page>

 

 

In my Apex class I tried to use getRecord() on the controller but there was an exception "SObject row was retrieved via SOQL without querying the requested field:" whenever I called the get('unit_price__c') method for example.

 

So I dynamically created a query, but now the Created By and Merchandise values are what appear to be keys and not the actual values (A4223400034 instead of 'Wii Jet' for example. Does anyone know how to get the actual token values from an object? I see them on the page so I'm sure there is a way to get the actual token value and not the ID.

 

Expected: createdbyid|foo                      unit_price__c|20  merchandise__c|Wii Jet

Actual:       createdbyid|005U0Irv0IAC  unit_price__c|20  merchandise__c|a01U010x4KIAQ

 

Apex class code:

public class VFController {

    private final SObject s;
    private final String id;
   
    public VFController(ApexPages.StandardController stdController) {
        s = stdController.getRecord();
        id = stdController.getId();
    }
 
    // Code we will invoke on page load.
    public PageReference autoRun() {

        //get the object details
        Schema.Describesobjectresult details = s.getSObjectType().getDescribe();
        Map<String, Schema.SObjectField> fieldInfo = details.fields.getMap();

        //create the select statement
        String query = 'SELECT ';
        for(String name : fieldInfo.keySet()) {
            query += name + ', ';
        }
       
        //remove extra ,
        query = query.substring(0, query.length()-2);

        //query and output messages
        query += ' FROM ' + details.getName() + ' WHERE id=\'' + id + '\'';
        sObject dataFields = Database.query(query);

        ApexPages.Severity error = ApexPages.Severity.Error;
        for (String name : fieldInfo.keySet()){
            ApexPages.Message myMsg = new ApexPages.Message(error,' Field:' + name +
             '|' + String.valueOf(dataFields.get(name)));
            ApexPages.addMessage(myMsg);
        }
       
        return null;
    }
}

 

  • November 14, 2011
  • Like
  • 0

I'm following the Apex example and have created a Line Item object. I am trying to get the names and values for each of the fields in the object. I've reduced the example to the following fields: Created By, Unit Price and Merchandise (name of the product sold)

 

I have tied a visual force page to a button that when clicked activates an Apex class.

<apex:page standardController="Line_Item__c" extensions="VFController" action="{!autoRun}">
  <apex:messages />
</apex:page>

 

 

In my Apex class I tried to use getRecord() on the controller but there was an exception "SObject row was retrieved via SOQL without querying the requested field:" whenever I called the get('unit_price__c') method for example.

 

So I dynamically created a query, but now the Created By and Merchandise values are what appear to be keys and not the actual values (A4223400034 instead of 'Wii Jet' for example. Does anyone know how to get the actual token values from an object? I see them on the page so I'm sure there is a way to get the actual token value and not the ID.

 

Expected: createdbyid|foo                      unit_price__c|20  merchandise__c|Wii Jet

Actual:       createdbyid|005U0Irv0IAC  unit_price__c|20  merchandise__c|a01U010x4KIAQ

 

Apex class code:

public class VFController {

    private final SObject s;
    private final String id;
   
    public VFController(ApexPages.StandardController stdController) {
        s = stdController.getRecord();
        id = stdController.getId();
    }
 
    // Code we will invoke on page load.
    public PageReference autoRun() {

        //get the object details
        Schema.Describesobjectresult details = s.getSObjectType().getDescribe();
        Map<String, Schema.SObjectField> fieldInfo = details.fields.getMap();

        //create the select statement
        String query = 'SELECT ';
        for(String name : fieldInfo.keySet()) {
            query += name + ', ';
        }
       
        //remove extra ,
        query = query.substring(0, query.length()-2);

        //query and output messages
        query += ' FROM ' + details.getName() + ' WHERE id=\'' + id + '\'';
        sObject dataFields = Database.query(query);

        ApexPages.Severity error = ApexPages.Severity.Error;
        for (String name : fieldInfo.keySet()){
            ApexPages.Message myMsg = new ApexPages.Message(error,' Field:' + name +
             '|' + String.valueOf(dataFields.get(name)));
            ApexPages.addMessage(myMsg);
        }
       
        return null;
    }
}

 

  • November 14, 2011
  • Like
  • 0
Hello,
I am trying to retrieve the detail records at the same time as the master record. I added a custom field lookup on Opportunity to Account (Contact__c).
 I get this error in Eclipse for the last line of code: "The configuration of your org has changed, please reload the page. Missing dependent object: Field: Opportunity.ContactId".
Could anybody help?
The business scenario that I have is totry to get is the latest Membership_End_Date__c for all Opportunities related to a given Contact.
 Thanks!

trigger UpdateIndividualMemberEndDate on Opportunity (after insert) { set<ID> contIDs = new Set<ID>(); if(Trigger.isInsert) { for(Opportunity o : System.Trigger.new){ contIDs.add(o.Contact__c); } Contact[] conts = new List<Contact>(); conts = [select Id, (select Membership_End_Date__c from Opportunities Order by Membership_End_Date__c desc) from Contact where ID in :contIDs]; Map<Id, Opportunity[]> contopp = new Map<Id, Opportunity[]>(); for (Contact eachCont: conts) { contopp.put(eachCont.Id, eachCont.Opportunities);} } }