0

I have a custom relationship to campaign object from opportunity object. When I am doing this:

List<sObject> opportunities = [SELECT Name, Campaign__r.Name FROM Opportunity];
for (Integer i = 0; i < opportunities.size(); i++) {
    System.debug(opportunities.get(i));
}

I am getting campaign object's id instead of campaign's name.

USER_DEBUG [4]|DEBUG|Opportunity:{Name=Test, Campaign__c=70163000000HcFbAAK, Id=00663000008E7JuAAK}

How can I get campaign's name (which in my case is campaigntest) instead of campaign__c id?

2

You need to do the following to get the field value see the sobject methods here

List<sObject> opportunities = [SELECT Name, Campaign__r.Name FROM Opportunity];

for (Integer i = 0; i < opportunities.size(); i++) {
    opportunit o = (opportunity) opportunities[i];
    String cName = o.Campaign__r.Name;
}
0

If you debug an SObject entire, you have little control over what is displayed. If you instead wish to display the value from a particular field path, debug specifically that field path.

for (Opportunity record : [SELECT Campaign__r.Name FROM Opportunity])
{
    system.debug(opportunities[i].Campaign__r.Name);
}

There is no reason here to assign your query to List<SObject>. Rather, assign it to the more specific List<Opportunity> type and your code will be much easier to work with.

0

I presume your aim isn't simply to debug, but if it was, it is worth being aware of this approach (for temporary debugging only as this is circumventing measures the platform takes to keep debug output costs limited):

List<sObject> opportunities = [SELECT Name, Campaign__r.Name FROM Opportunity];

System.debug('opportunities=\n' + JSON.serializePretty(opportunities));

that outputs all the objects and all the fields into the debug log.

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.