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 

Trigger error:List has no rows for assignment to SObject

When I try to create a new account, It throws me an error saying:Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger SendAccountsToConnection caused an unexpected exception, contact your administrator: SendAccountsToConnection: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.SendAccountsToConnection: line 2, column 1
trigger SendAccountsToConnection on Account(after insert,after update) { PartnerNetworkConnection conn = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionStatus = 'Accepted' and ConnectionName = 'Johnson Controls']; List<PartnerNetworkRecordConnection> recordConnectionToInsert = new List<PartnerNetworkRecordConnection> (); for (Account 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; } }
​

trigger SendAccountsToConnection on Account(after insert,after update) {
        PartnerNetworkConnection conn = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection  where ConnectionStatus = 'Accepted' and ConnectionName = 'abc'];
        List<PartnerNetworkRecordConnection> recordConnectionToInsert  = new List<PartnerNetworkRecordConnection>  ();
        
        for (Account 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;
        }
     }

 
Best Answer chosen by Sai Harshitha Bandlamudi
Shashi PatowaryShashi Patowary
Hi,

What is there in the line no. 2 ? If this is the code, then this query is returning no data. Please try to execute it from dataloader or qorkbench to see if there is data and then correct in the trigger.
PartnerNetworkConnection conn = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionStatus = 'Accepted' and ConnectionName = 'Johnson Controls'];

Please let me know if this is helpful.

regards,
Shashi

All Answers

Shashi PatowaryShashi Patowary
Hi,

What is there in the line no. 2 ? If this is the code, then this query is returning no data. Please try to execute it from dataloader or qorkbench to see if there is data and then correct in the trigger.
PartnerNetworkConnection conn = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionStatus = 'Accepted' and ConnectionName = 'Johnson Controls'];

Please let me know if this is helpful.

regards,
Shashi
This was selected as the best answer
Sai Harshitha BandlamudiSai Harshitha Bandlamudi
Hi shashi, Thank you for reply. It worked. There was an error in Connection name. Thank you