0

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: enter image description here

Error:(126, 82) Expecting '<' but was: '('

Error:(126, 74) Invalid type argument count for List: expected 1 but found 0

2
  • 1
    Seriously, don't use List<sobjects> results = new list<sobjects>([query]). It has no benefit other than bloating your code.
    – sfdcfox
    Jun 21, 2018 at 19:55
  • Plus it makes it just a tiny bit harder to read. Jun 22, 2018 at 6:34

1 Answer 1

2

Odds are, your problem lies with this line:

cir = [
    SELECT Successor__c
    FROM Interactions_Relation__c
    WHERE Predecessor__c = :nextInteraction.Id
];

If there's no record that matches the query, you'll get an exception. You can fix that up with:

Interactions_Relation__c[] cirSuccessors = [
    SELECT Successor__c
    FROM Interactions_Relation__c
    WHERE Predecessor__c = :nextInteraction.Id
];
if(!cirSucessors.isEmpty()) {
  cir = cirSuccessors[0];
} else {
  break;
}
2
  • Oh! Let me try it. Jun 21, 2018 at 19:57
  • @oobarbazanoo Sorry, had the wrong data type in the list. I just edited it.
    – sfdcfox
    Jun 21, 2018 at 19:58

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.