Why am I getting the error: List has no rows for assignment to SObject
?
Here is the function which produces the error:
@AuraEnabled
public static void setCharacterPose(
String interactionId,
Boolean isLeft,
String character,
String pose
) {
if(isLeft) {
UPDATE new zced__Interaction__c(
Id = interactionId,
zced__Left_Character__c = character,
zced__Left_Character_Pose__c = pose
);
List<Interactions_Relation__c> currentInteractionRelations = [
SELECT Successor__c
FROM Interactions_Relation__c
WHERE Predecessor__c = :interactionId
];
System.Debug('currentInteractionRelations' + currentInteractionRelations);
for(Integer i = 0; i < currentInteractionRelations.size(); ++i) {
Interactions_Relation__c cir = currentInteractionRelations[i];
System.Debug('sdsdvsHERE');
while(cir.Successor__c != null) {
Interaction__c nextInteraction = [
SELECT Id, Interaction_Type__c, zced__Left_Character__c, zced__Left_Character_Pose__c
FROM Interaction__c
WHERE Id = :cir.Successor__c
];
System.Debug('nextInteraction' + nextInteraction);
if(nextInteraction.Interaction_Type__c == 'Transition') {
break;
}
nextInteraction.zced__Left_Character__c = character;
nextInteraction.zced__Left_Character_Pose__c = null;
UPDATE nextInteraction;
cir = [
SELECT Successor__c
FROM Interactions_Relation__c
WHERE Predecessor__c = :nextInteraction.Id
];
System.Debug('cir' + cir);
}
}
} else {
UPDATE new zced__Interaction__c(
Id = interactionId,
zced__Right_Character__c = character,
zced__Right_Character_Pose__c = pose
);
}
}
Here is the log after the execution:
20:11:38.0 (295643387)|USER_DEBUG|>[131]|DEBUG|currentInteractionRelations(zced__Interactions_Relation__c:>{zced__Successor__c=a0A1r00002rAxEqEAK, Id=a0B1r000017Gwk2EAC})
I am really stuck because I can not understand what is causing the error. As you can see from the log the currentInteractionRelations
variable is initialized successfully. So, I would expect this code:
for(Integer i = 0; i < currentInteractionRelations.size(); ++i) {
Interactions_Relation__c cir = currentInteractionRelations[i];
System.Debug('sdsdvsHERE');
to run without any problems. And it is not actually the case because the sdsdvsHERE
does not get output to the log.
This answer does not help, because as you can tell from the log there is actually a returned value.
Here is what the function looks like after I made the changes suggested in the answer:
@AuraEnabled
public static void setCharacterPose(
String interactionId,
Boolean isLeft,
String character,
String pose
) {
if(isLeft) {
UPDATE new zced__Interaction__c(
Id = interactionId,
zced__Left_Character__c = character,
zced__Left_Character_Pose__c = pose
);
List<Interactions_Relation__c> currentInteractionRelations = new List([
SELECT Successor__c
FROM Interactions_Relation__c
WHERE Predecessor__c = :interactionId
]);
System.Debug('currentInteractionRelations' + currentInteractionRelations);
if(!currentInteractionRelations.isEmpty()) {
for (Integer i = 0; i < currentInteractionRelations.size(); ++i) {
Interactions_Relation__c cir = currentInteractionRelations[i];
System.Debug('sdsdvsHERE');
while (cir.Successor__c != null) {
Interaction__c nextInteraction = [
SELECT Id, Interaction_Type__c, zced__Left_Character__c, zced__Left_Character_Pose__c
FROM Interaction__c
WHERE Id = :cir.Successor__c
];
System.Debug('nextInteraction' + nextInteraction);
if (nextInteraction.Interaction_Type__c == 'Transition') {
break;
}
nextInteraction.zced__Left_Character__c = character;
nextInteraction.zced__Left_Character_Pose__c = null;
UPDATE nextInteraction;
cir = [
SELECT Successor__c
FROM Interactions_Relation__c
WHERE Predecessor__c = :nextInteraction.Id
];
System.Debug('cir' + cir);
}
}
}
} else {
UPDATE new zced__Interaction__c(
Id = interactionId,
zced__Right_Character__c = character,
zced__Right_Character_Pose__c = pose
);
}
}
Here is an error I am getting now:
Error:(126, 82) Expecting '<' but was: '('
Error:(126, 74) Invalid type argument count for List: expected 1 but found 0
List<sobjects> results = new list<sobjects>([query])
. It has no benefit other than bloating your code.