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
raj161raj161 

Error in Apex Trigger when sharing and unsharing the records

Hi,

I am working on Salesforce to Salesforce automation and developed few triggers as part of this automation.

My scenario is when a account is created or updated in one org with a customfield "Global Account" = YES, it need to be forwared to other org. Similarly when an account is changed from YES to NO on "Global Account" field, it need to unshare the account and its related records.(Contacts,Opportunities and Tasks).

 

I have used the below trigger for the above functionality and I am getting an error of "Too many SOQL querie: 21". I am not sure how I am getting 21 queries. 

 

Please let me know if you can figure out this issue..

 

Thanks in advance.

 

*************************************************************

 AccountAutoSharePartner: execution of AfterUpdate caused by:
System.Exception: Too many SOQL queries: 21
Trigger.AccountAutoSharePartner: line 63, column 59

Apex script unhandled trigger exception by user/organization:
00520000000us1b/00D200000000KT5

 

AccountAutoSharePartner: execution of AfterUpdate

caused by: System.Exception: Too many SOQL queries: 21

Trigger.AccountAutoSharePartner: line 63, column 59

*************************************************************

 

 

 

**************************

Trigger Code...........................

**************************

trigger AccountAutoSharePartner on Account (after insert,after update)
{
List<PartnerNetworkConnection> connMap = new List<PartnerNetworkConnection>([select id,ConnectionStatus,ConnectionName from PartnerNetworkConnection where id = '04P2000000096FPEAY']);
List<PartnerNetworkRecordConnection> prncList = new List<PartnerNetworkRecordConnection>();
Set<Id> listings = new Set<Id>();
Set<Id> prnclocalrecords = new Set<Id>();

for(Integer i=0; i < Trigger.size; i++)
{
Account li = Trigger.new[i];
String uId = li.Id;

System.debug('Sharing ' + li.id);
System.debug('connMap ' + connMap);
System.debug('Step 1');

if(li.Global_Account__c == 'YES' && li.ConnectionReceivedId == NULL)
{
for(PartnerNetworkConnection network : connMap)
{
System.debug('Step 2');
String cid = network.Id;
String status = network.ConnectionStatus;
String connName = network.ConnectionName;

PartnerNetworkRecordConnection prnc = new PartnerNetworkRecordConnection();

prnc.ConnectionId = cid;
prnc.localRecordId = uId;
prnc.RelatedRecords = 'Contact';
prnc.SendClosedTasks = true;
prnc.SendOpenTasks = true;
prnc.SendEmails = false;
prncList.add(prnc);
}
}
else if(li.Global_Account__c != 'YES' && li.ConnectionReceivedId == NULL)
{
System.debug('Step 3');
listings.add(UId);
}

}

//Remove all the records associated to the accounts.
if(listings.size() > 0)
{

//Opportunities
List<Opportunity> OppList = new List<Opportunity>([Select Id, AccountId From Opportunity Where AccountId in :listings]);
for(Opportunity objopp : OppList)
{
prnclocalrecords.add(objopp.Id);
}

//Accounts
prnclocalrecords.addAll(listings);
}

List<PartnerNetworkRecordConnection> filterMap = new List<PartnerNetworkRecordConnection>([select Id, Status, ConnectionId, LocalRecordId from PartnerNetworkRecordConnection where LocalRecordId in :prnclocalrecords OR ParentRecordId in :listings]);
List<PartnerNetworkRecordConnection> deleteNets = new List<PartnerNetworkRecordConnection>();
for(PartnerNetworkRecordConnection nets : filterMap)
{

String lclId = nets.LocalRecordId;
String status = nets.Status;
System.debug('Checking Status: '+status);
if(status.equalsIgnoreCase('Sent') || status.equalsIgnoreCase('Pending') || status.equalsIgnoreCase('Invite'))
{
if(!lclId.startsWith('00T'))//ignore the tasks as the tasks will be unshared automatically.
{
deleteNets.add(nets);
}
}
System.debug('PNRC ID: '+nets.Id +' ; LOCAL RECORD ID: '+nets.LocalRecordId);
}
System.debug('Size of list: '+deleteNets.size());

//Start Auto share
if (prncList.size() > 0)
{
insert prncList;
}

//Stop Auto share
if (deleteNets.size() > 0)
{
if(deleteNets.size() > 50)
BulkSharePartner.RemoveAccountSharing(prnclocalrecords);
else
delete deleteNets;
}
}

 

jkucerajkucera

Given there's an indeterminant amount of related records, a trigger will be problematic given governor limits.  Instead, you might want to use asynchrnous or batch apex, which have less strict limits.

 

 

raj161raj161

Thanks for the such a quick response.

 

I am handling the asynchronous apex when the number of records are more than 50. If the record count is less than 50 I am updating in the trigger itself.

 

But I don't think the error is coming from number of record count. Its coming from the number of SOQL queries.

I have tested it with single record update and also bulk record update using the data loader. I didn't receive any error.

 

I got a vague information of cause of error from the user as, when a Lead is converted to contact on Account or Opportunity its  causing an error. But I did tried the same scenario in sandbox, but it went well with out an error.

 

 

jkucerajkucera

That's about the limit of my knowledge so hopefully someone else can help out here :)  By my eye it looks like you used good form using sets and not including SOQL in the loops.

 

raj161raj161

Hi John,

 

Thanks for your support.

 

I have solved this issue by checking the debug log of the user. When checking the exception message I was able to only trace the trigger where I am getting an issue. But in the debug log it specifies all the information in that particular transaction.

 

In this case, when a lead is converted into a contact on an Opportunity or Account, there are other triggers that are getting fired before this trigger is getting fired. But the limit was reached on this trigger which was raising the exception.

 

once again thanks for the support.

 

 

jkucerajkucera
That makes sense given the limits are shared across that save/session.  Thanks for the update!