SOQL joindre requête renvoie sObject mais pas les champs. Comment utiliser l'ID obtenu?
-
08-10-2019 - |
Question
J'ai SOQL ci-dessous et je reçois le résultat qui contient l'ID de sObject.
Mon hypothèse était la requête renvoie les champs de SObject ainsi.
Par exemple ma requête essayer d'obtenir « startDay__c
» (La date) qui est comme domaine de ShigotoShousai sobject. Mais le résultat de la requête est juste ID de l'instance sObject.
( parent : ShigotoShousai
enfant : ShigotoAssign
)
sObject[] result = [
SELECT
ShigotoShousai__r.id,
ShigotoShousai__r.startDay__c
FROM ShigotoAssign__c
];
system.debug (suite) Rendement
shigotoAssign_ c: {Id = a06500000067aNjAAI, ShigotoShousai _c = a055000000DlHnOAAV}, shigotoAssign_ c: {Id = a06500000067aNoAAI, ShigotoShousai _c = a055000000DlHnTAAV})
Je suis ID de ShigotoShousai__c sObject au lieu de sa propriété "startDay__c
".
Je pensais que la sortie serait quelque chose comme:
shigotoAssign__c:{ShigotoShousai__c=a055000000DlHnOAAV, startDay__c=2010-10-10},
shigotoAssign__c:{ShigotoShousai__c=a055000000DlHnTAAV, startDay__c=2010-10-13})
Mais résultat de la requête me retourne ID de ShigotoShousai__c sobject: (
Maintenant, je sais que la valeur d'identification de ShigotoShousai__c
et que vous souhaitez accéder à son champ, donc je ne suit.
ShigotoShousai__c foo = (ShigotoShousai__c)result[0].get('ShigotoShousai__c');
//now I assume I can access to some fields like below
system.debug(foo.workDate);
Et cela me donne l'erreur:
System.TypeException: Invalid conversion from runtime
type Id to SOBJECT:shigotoShousai__c
Alors je me suis dit que ID ne peut pas être utilisé pour désigner SObject (à savoir ShigotoShousai__c).
Mais j'ai son id .. Comment puis-je accéder, par exemple startDay__c
? Est-il possible d'utiliser cet ID?
La solution
Le problème est que vous assignez le résultat de la requête SOQL au Sobject générique [], qui n'a pas de champs de béton de son propre, à l'exception Id. Tant que vous n'êtes pas essayer de faire quoi que ce soit avec fantaisie SOQL dynamique, essayer quelque chose comme ceci:
ShigotoAssign__c[] result = [
SELECT
ShigotoShousai__r.id,
ShigotoShousai__r.startDay__c
FROM ShigotoAssign__c
];
for(ShigotoAssign__c s : result) {
System.debug(s.ShigotoShousai__r.startDay__c);
}
Autres conseils
Aussi veux juste partager mes informations, car il est lié à la question. Espérons que quelqu'un trouve cela utile.
Ci-dessous la requête implique parent, enfant, petit-enfant et j'utilisé le sommet pour atteindre chaque valeur. Maintenant, je peux afficher les valeurs à la page de Visualforce:)
//WORK
public String workName { get; set; }
public String workContent { get; set; }
public String workCategory { get; set; }
public String workSponsor { get; set; }
//WORK DETAIL
public String workDetailStartDay { get; set; }
public String workDetailStartHour { get; set; }
public String workDetailStartMin { get; set; }
public String workDetailEndDay { get; set; }
public String workDetailEndHour { get; set; }
public String workDetailEndMin { get; set; }
public String workDetailCategory { get; set; }
public String workDetailDivision { get; set; }
//WORK ASSIGN
ShigotoAssign__c[] result = [
SELECT
ShigotoShousai__r.Shigoto__r.name,
ShigotoShousai__r.Shigoto__r.workContent__c,
ShigotoShousai__r.Shigoto__r.category__c,
ShigotoShousai__r.Shigoto__r.sponsor__c,
ShigotoShousai__r.id, ShigotoShousai__r.startDay__c, ShigotoShousai__r.startHour__c,
ShigotoShousai__r.startMin__c,
ShigotoShousai__r.endDay__c, ShigotoShousai__r.endHour__c, ShigotoShousai__r.endMin__c,
ShigotoShousai__r.workCategory__c, ShigotoShousai__r.workDivision__c,
ShigotoShousai__r.address__c,
id, contactStat__c
FROM ShigotoAssign__c
WHERE id = :workAssigned.id
];
//get WORK info to show on email template page
workName = result[0].ShigotoShousai__r.Shigoto__r.name;
workContent = result[0].ShigotoShousai__r.Shigoto__r.workContent__c;
workSponsor = result[0].ShigotoShousai__r.Shigoto__r.sponsor__c;
//get WORK DETAIL info to show on email template page
workDetailStartDay = String.valueOf(result[0].ShigotoShousai__r.startDay__c);