function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sai Harshitha BandlamudiSai Harshitha Bandlamudi 

List has more than 1 row for assignment to SObject while autoforwarding objects using S2S

Hello All,
I am trying to implement S2S (Salesforce to salesforce) And I have written a code on Custom object called "ABC" to allow auto forwarding of records. After making the trigger active, I cannot manually create any records. Please help.

trigger SendABCToConnection on ABC__c(after insert,after update) {
        PartnerNetworkConnection conn = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection  where ConnectionStatus = 'Accepted' and ConnectionName = 'XYZ'];
        List<PartnerNetworkRecordConnection> recordConnectionToInsert  = new List<PartnerNetworkRecordConnection>  ();
        for (ABC__c acc : Trigger.new){
            PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();

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

The error I am getting:
Apex trigger SendABCToConnection caused an unexpected exception, contact your administrator: SendABCToConnection: execution of AfterInsert caused by: System.QueryException: List has more than 1 row for assignment to SObject: Trigger.SendABCToConnection: line 2, column 1
matt.ian.thomasmatt.ian.thomas
Setting the result of a query to a variable that isn't a list will throw a QueryException if the result set is anything but exactly 1 record. In this case, there are more than 1 "PartnerNetworkConnection" records returned in your query in the first line of your trigger, so you either need to change your variable declaration to be List<PartnerNetworkConnection> or modify your query such that it is ALWAYS returning 1 record. Alternatively, put it in a try-catch so that if and when it does return more than 1 result, you can do something about it rather than have it blow up entirely.
Deepak GulianDeepak Gulian
trigger SendABCToConnection on ABC__c(after insert,after update) {
        PartnerNetworkConnection conn = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection  where ConnectionStatus = 'Accepted' and ConnectionName = 'XYZ' Limit 1];
        List<PartnerNetworkRecordConnection> recordConnectionToInsert  = new List<PartnerNetworkRecordConnection>  ();
        for (ABC__c acc : Trigger.new){
            PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();

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