0

I have the following trigger that automate the case obj. sync from one Org to another via salesforce to salesforce connection.

trigger OrgSync on Case (after insert, after update) {

PartnerNetworkConnection conn = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection  where ConnectionStatus = 'Accepted'];

        List<PartnerNetworkRecordConnection> recordConnectionToInsert  = new List<PartnerNetworkRecordConnection>  ();

        for (Case cc : Trigger.new){
            PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();

           newrecord.ConnectionId = conn.Id;
           newrecord.LocalRecordId = cc.id;  
            newrecord.SendClosedTasks = true;
            newrecord.SendOpenTasks = true;
            newrecord.SendEmails = true;
            recordConnectionToInsert.add(newrecord);
        }
        if (recordConnectionToInsert.size() > 0){
            System.debug('>>> Sharing ' + recordConnectionToInsert.size() + ' records');
            insert recordConnectionToInsert;
        }
}

I am getting the following error when try to save a case record in the sorce org.

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger trCase caused an unexpected exception, contact your administrator: trCase: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Class.trCaseHandler.AddEmailSenderToCaseTeam: line 203, column 1

Upon inspection I see that its conflicting with handler class of another trigger. Here is the code snippet of the method where the conflict is happening in the handler class. Am I missing something ?

public void AddEmailSenderToCaseTeam( case[] newCases )
    {
        CaseTeamMember[] team = new CaseTeamMember[]{};

        CaseTeamRole createRole = [SELECT Id FROM CaseTeamRole WHERE Name = 'Created By' LIMIT 1];

        MAP <String, Id> emailParentMap = new MAP <String, Id>{};
        for( case c: newCases )
        {
            emailParentMap.put( c.SuppliedEmail, null );
        }

        //check if case supplied email is in users
        User[] emailUsers = [select Id, Email from User where Email =: emailParentMap.keySet() order by Email, LastLoginDate desc];
        for( User u: emailUsers )
        {
            Id x = emailParentMap.get( u.Email );
            if( x == null )
                emailParentMap.put( u.Email, u.Id );
        }

        for( case c: newCases )
        {
            Id x = emailParentMap.get( c.SuppliedEmail );
            if( x != null )
            {
                team.add(new CaseTeamMember(ParentId = c.Id, MemberId = x,TeamRoleId = createRole.Id) );
            }
            else
            {
                String msg = 'Unable to add email-to-case sender ' + c.SuppliedEmail + ' to case team. No matching user found.';
                if(m_isExecuting)
                    c.addError( msg );
                ErrorMsgs.add(msg + ' [Case ID ' + c.ID + ']');
            }
        }
        insert team;
    }
0

You have multiple triggers configured on case. The issue occurs because of a different trigger 'trCase'. Both your trigger (OrgSync) and this trigger(trCase) execute on same event. So both these triggers will fire in any order (one after another). Both triggers are not interlinked but they will execute sequentially.

The real issue here is -

CaseTeamRole createRole = [SELECT Id FROM CaseTeamRole WHERE Name = 'Created By' LIMIT 1];

This query is not returning any rows and the exception is not handled here. So all you have to do is understand if this query should always return some row as per business functionality. If not - enclose this query in a try catch block.

public void AddEmailSenderToCaseTeam( case[] newCases )
    {
        CaseTeamMember[] team = new CaseTeamMember[]{};
        try {
            CaseTeamRole createRole = [SELECT Id FROM CaseTeamRole WHERE Name = 'Created By' LIMIT 1];

            MAP <String, Id> emailParentMap = new MAP <String, Id>{};
            for( case c: newCases )
            {
                emailParentMap.put( c.SuppliedEmail, null );
            }

            //check if case supplied email is in users
            User[] emailUsers = [select Id, Email from User where Email =: emailParentMap.keySet() order by Email, LastLoginDate desc];
            for( User u: emailUsers )
            {
                Id x = emailParentMap.get( u.Email );
                if( x == null )
                    emailParentMap.put( u.Email, u.Id );
            }

            for( case c: newCases )
            {
                Id x = emailParentMap.get( c.SuppliedEmail );
                if( x != null )
                {
                    team.add(new CaseTeamMember(ParentId = c.Id, MemberId = x,TeamRoleId = createRole.Id) );
                }
                else
                {
                    String msg = 'Unable to add email-to-case sender ' + c.SuppliedEmail + ' to case team. No matching user found.';
                    if(m_isExecuting)
                        c.addError( msg );
                    ErrorMsgs.add(msg + ' [Case ID ' + c.ID + ']');
                }
            }
            insert team;
        } catch (Exception ex){
            system.debug('Following Exceptions Occured'+ex.getmessage());
        }
    }

I would also suggest the same for the Query on PartnerNetworkConnection in your trigger.

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.