UPDATE - I have found that the trigger I was trying to deploy is probably the cause of the other issues. This trigger is to count all opps associated to an account and update the account field "Number of Opportunities". Is there a better way to write this so I don't get the error of too many SOQL queries?
trigger CountOpps on Opportunity ( after insert, after update) {
Opportunity [] Opps;
Opps = Trigger.new;
// get list of accounts
Set<ID> acctIds = new Set<ID>();
for (Opportunity opp : Opps) {
if (opp.Stage_Calc__c != null)
acctIds.add(opp.AccountId);
}
Map<ID, Opportunity> OppsForAccounts = new Map<ID, Opportunity>([select Id
,AccountId
from Opportunity
]);
Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id
,Number_of_Opps__c
from Account
where Id in :acctIds]);
for (Account acct : acctsToUpdate.values()) {
Set<ID> OppIds = new Set<ID>();
for (Opportunity opp : OppsForAccounts.values()) {
if (opp.AccountId == acct.Id)
OppIds.add(opp.Id);
}
if (acct.Number_of_Opps__c != OppIds.size())
acct.Number_of_Opps__c = OppIds.size();
}
update acctsToUpdate.values();
}
I recently had this test class start to fail, I did not write this class it was written by a partner who integrated SF and NetSuite. The test is far more advanced than I can write at this time. The error I am getting is:
System.DmlException: Delete failed. First exception on row 0 with id a10f0000000QhRhAAK; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, celigoUpdateSalesOrder: execution of BeforeDelete caused by: System.Exception: Too many SOQL queries: 101 Trigger.fulfillmentcontactcreatedopp: line 22, column 1 Stacktrace: Class.CeligoTestTriggers.testTrigger: line 280, column 1
Here are both the test class that is failing and the referenced trigger (bolded lines)
Test Class
@isTest
private class CeligoTestTriggers {
static testMethod void testAccountsAndContacts (){
Test.startTest();
List<Account> accounts = new List<Account>();
Account a = new Account();
a.Name = 'testAccA';
//fill fields defined in the setting
CeligoTestHelper.fillSobject(a);
accounts.add(a);
Account a4 = new Account();
a4.Name = 'testAccB';
a4.NetSuite_Pull__c = true;
//fill fields defined in the setting
CeligoTestHelper.fillSobject(a4);
accounts.add(a4);
Account a3 = new Account();
a3.Name = 'testAccD';
a3.NetSuite_Id__c = '-1';
a3.NetSuite_Locked__c = false;
//fill fields defined in the setting
CeligoTestHelper.fillSobject(a3);
accounts.add(a3);
Account a5 = new Account();
a5.Name = 'testAccE';
a5.NetSuite_Id__c = '-1';
a5.Celigo_Update__c = true;
//fill fields defined in the setting
CeligoTestHelper.fillSobject(a5);
accounts.add(a5);
insert accounts;
Account actual = [SELECT id,Celigo_Update__c, NetSuite_Id__c, NetSuite_Locked__c, NS_Sync__c
FROM Account where id = :a.id];
System.assert(actual.Celigo_Update__c == false);
System.assert(actual.NetSuite_Id__c == null);
System.assert(actual.NetSuite_Locked__c == false);
System.assert(actual.NS_Sync__c == null);
actual = [SELECT id, Celigo_Update__c, NetSuite_Id__c, NetSuite_Locked__c, NS_Sync__c
FROM Account where id = :a3.id];
System.assert(actual.Celigo_Update__c == false);
System.assert(actual.NetSuite_Id__c == null);
System.assert(actual.NetSuite_Locked__c == false);
System.assert(actual.NS_Sync__c == null);
actual = [SELECT id, Celigo_Update__c, NetSuite_Id__c, NetSuite_Locked__c, NS_Sync__c
FROM Account where id = :a4.id];
System.assert(actual.Celigo_Update__c == false);
System.assert(actual.NetSuite_Id__c == null);
System.assert(actual.NetSuite_Locked__c == false);
System.assert(actual.NS_Sync__c == null);
//Test contacts
List<Contact> contacts = new List<Contact>();
Contact c = new Contact();
c.LastName = 'testConA';
//fill contact fields defined in the setting
CeligoTestHelper.fillSobject(c);
contacts.add(c);
Contact c2 = new Contact();
c2.LastName = 'testConB';
c2.Celigo_Update__c = true;
//fill fields defined in the setting
CeligoTestHelper.fillSobject(c2);
contacts.add(c2);
Contact c3 = new Contact();
c3.LastName = 'testConC';
//fill fields defined in the setting
CeligoTestHelper.fillSobject(c3);
c3.AccountId = actual.Id;
contacts.add(c3);
insert contacts;
Contact cActual = [SELECT id, Celigo_Update__c, NetSuite_Id__c, NetSuite_Locked__c, NS_Sync__c
FROM Contact where id = :c.id];
System.assert(actual.Celigo_Update__c == false);
System.assert(actual.NetSuite_Id__c == null);
System.assert(actual.NetSuite_Locked__c == false);
System.assert(actual.NS_Sync__c == null);
cActual = [SELECT id, Celigo_Update__c, NetSuite_Id__c, NetSuite_Locked__c, NS_Sync__c
FROM Contact where id = :c2.id];
System.assert(actual.Celigo_Update__c == false);
System.assert(actual.NetSuite_Id__c == null);
System.assert(actual.NetSuite_Locked__c == false);
System.assert(actual.NS_Sync__c == null);
Contact c3Actual = [SELECT id, Celigo_Update__c, NetSuite_Id__c, NetSuite_Locked__c, NS_Sync__c
FROM Contact where id = :c3.id];
System.assert(c3Actual.Celigo_Update__c == false);
System.assert(c3Actual.NetSuite_Id__c == null);
System.assert(c3Actual.NetSuite_Locked__c == false);
System.assert(c3Actual.NS_Sync__c == null);
List<Contact> updateContact = new List<Contact>();
c2.LastName = 'testConC';
updateContact.add(c2);
c3.Celigo_Update__c = true;
updateContact.add(c3);
update updateContact;
cActual = [SELECT id, Celigo_Update__c, NetSuite_Id__c, NetSuite_Locked__c, NS_Sync__c
FROM Contact where id = :c2.id];
System.assert(actual.Celigo_Update__c == false);
System.assert(actual.NetSuite_Id__c == null);
System.assert(actual.NetSuite_Locked__c == false);
System.assert(actual.NS_Sync__c == null);
c3Actual = [SELECT id, Celigo_Update__c, NetSuite_Id__c, NetSuite_Locked__c, NS_Sync__c
FROM Contact where id = :c3.id];
System.assert(c3Actual.Celigo_Update__c == false);
System.assert(c3Actual.NetSuite_Id__c == null);
System.assert(c3Actual.NetSuite_Locked__c == false);
System.assert(c3Actual.NS_Sync__c == null);
c3.Celigo_Update__c = false;
c3.AccountId = null;
c3.NetSuite_Id__c = '-1';
update c3;
Test.stopTest();
}
static testMethod void testTrigger () {
Test.startTest();
Account a = new Account();
a.Name = 'testAccA';
//fill fields defined in the setting
insert a;
List<Product2> products = new List<Product2>();
Product2 p = new product2(name='Test Product');
//fill fields defined in the setting
CeligoTestHelper.fillSobject(p);
products.add(p);
Product2 p2 = new product2(name='Test Product2');
//fill fields defined in the setting
CeligoTestHelper.fillSobject(p2);
products.add(p2);
insert products;
System.debug('Success p: ' + p.ID);
Pricebook2 pb = new pricebook2(name='test pricebook');
//fill fields defined in the setting
CeligoTestHelper.fillSobject(pb);
insert pb;
System.debug('Success pb: ' + pb.ID);
Pricebook2 stdPb = [select Id from Pricebook2 where isStandard=true limit 1];
System.debug('Success stdPb: ' + stdPb.ID);
List<PricebookEntry> entryList = new List<PricebookEntry>();
PricebookEntry pbeS = new PricebookEntry(pricebook2id = stdPb.id, product2id = p.id, unitprice=100.0, isActive=true);
//fill fields defined in the setting
CeligoTestHelper.fillSobject(pbeS);
entryList.add(pbeS);
PricebookEntry pbeS2 = new PricebookEntry(pricebook2id = stdPb.id, product2id = p2.id, unitprice=100.0, isActive=true);
//fill fields defined in the setting
CeligoTestHelper.fillSobject(pbeS2);
entryList.add(pbeS2);
PricebookEntry pbe = new PricebookEntry(pricebook2id=pb.id, product2id=p.id, unitprice=100.0, isActive=true);
//fill fields defined in the setting
CeligoTestHelper.fillSobject(pbe);
entryList.add(pbe);
PricebookEntry pbe2 = new PricebookEntry(pricebook2id=pb.id, product2id=p2.id, unitprice=100.0, isActive=true);
//fill fields defined in the setting
CeligoTestHelper.fillSobject(pbe2);
entryList.add(pbe2);
insert entryList;
System.debug('Success pbeS: ' + pbeS.ID);
Opportunity o = new Opportunity();
o.Name = 'test';
o.AccountId = a.Id;
o.StageName = 'Prospecting';
o.CloseDate = Date.today();
o.A_vs_B__c = 'A Lead';
//fill fields defined in the setting
CeligoTestHelper.fillSobject(o);
insert o;
Opportunity oActual = [SELECT id, StageName, Send_to_NetSuite__c, Generate_Estimate__c, Generate_Sales_Order__c, NS_Sync__c
FROM Opportunity where id = :o.id];
System.assert(oActual.Send_to_NetSuite__c == true);
System.assert(oActual.Generate_Estimate__c == false);
System.assert(oActual.Generate_Sales_Order__c == false);
System.assert(oActual.NS_Sync__c == null);
System.assert(oActual.StageName == 'Prospecting');
OpportunityLineItem oli = new OpportunityLineItem(opportunityid=o.id, UnitPrice=100, quantity=1, pricebookentryid=pbe.Id);
//fill fields defined in the setting
CeligoTestHelper.fillSobject(oli);
insert oli;
OpportunityLineItem oliActual = [SELECT id, UnitPrice, quantity FROM OpportunityLineItem where id = :oli.id];
oli.Quantity = 2;
update oliActual;
NetSuite_Estimate__c estimate = new NetSuite_Estimate__c();
estimate.Name = 'test';
estimate.Opportunity__c = o.Id;
//fill fields defined in the setting
CeligoTestHelper.fillSobject(estimate);
insert estimate;
oActual = [SELECT id, Celigo_Sync_Helper__c
FROM Opportunity where id = :o.id];
System.assert(oActual.Celigo_Sync_Helper__c == true);
NetSuite_Estimate__c estimate2 = new NetSuite_Estimate__c();
estimate2.Name = 'test2';
estimate2.Opportunity__c = null;
//fill fields defined in the setting
CeligoTestHelper.fillSobject(estimate2);
insert estimate2;
NetSuite_Sales_Order__c order = new NetSuite_Sales_Order__c();
order.Name = 'test';
order.Opportunity__c = o.Id;
//update Opportunity and fill its fields
CeligoTestHelper.fillSobject(o, true);
update o;
//fill fields defined in the setting
CeligoTestHelper.fillSobject(order);
insert order;
oActual = [SELECT id, StageName, StageNameState__c
FROM Opportunity where id = :o.id];
//get the setting for the closed stage
celigo_connector__c cs = celigo_connector__c.getInstance('Opportunity Closed Stage');
String closedStage = (cs!=null)?cs.value__c:'Closed Won';
System.assert(oActual.StageName == closedStage);
System.assert(oActual.StageNameState__c == 'Prospecting');
List<NetSuite_Sales_Order__c> orderList = new List<NetSuite_Sales_Order__c>();
NetSuite_Sales_Order__c so = new NetSuite_Sales_Order__c();
so.Name = 'testSO';
//fill fields defined in the setting
CeligoTestHelper.fillSobject(so);
orderList.add(so);
NetSuite_Sales_Order__c so2 = new NetSuite_Sales_Order__c();
so2.Name = 'test';
so2.Opportunity__c = o.Id;
//fill fields defined in the setting
CeligoTestHelper.fillSobject(so2);
orderList.add(so2);
insert orderList;
oActual = [SELECT id, StageName, StageNameState__c
FROM Opportunity where id = :o.id];
System.assert(oActual.StageName == closedStage );
System.assert(oActual.StageNameState__c == 'Prospecting');
delete so2;
***oActual = [SELECT id, StageName, StageNameState__c
FROM Opportunity where id = :o.id];
System.assert(oActual.StageName == closedStage);
System.assert(oActual.StageNameState__c == 'Prospecting');***
delete order;
oActual = [SELECT id, StageName, StageNameState__c
FROM Opportunity where id = :o.id];
System.assert(oActual.StageName == 'Prospecting');
System.assert(oActual.StageNameState__c == null);
CeligoAccountSyncPoller casp = new CeligoAccountSyncPoller();
casp.getMessage();
casp.setShowLoader(true);
casp.getShowLoader();
casp.getOpportunityById(o.Id);
casp.getAccountById(a.Id);
casp.redirect('home/home.jsp');
System.currentPageReference().getParameters().put('whence', a.Id);
casp.checkSyncStatus();
CeligoAccountSyncPoller casp3 = new CeligoAccountSyncPoller();
System.currentPageReference().getParameters().put('whence', a.Id);
casp3.checkSyncStatus();
CeligoAccountSyncPoller casp2 = new CeligoAccountSyncPoller();
casp2.checkSyncStatus();
a.NetSuite_Pull__c = false;
update a;
casp2.checkSyncStatus();
CeligoQuoteSyncPoller cqsp = new CeligoQuoteSyncPoller();
System.currentPageReference().getParameters().put('whence', o.Id);
cqsp.getMessage();
cqsp.setShowLoader(true);
cqsp.getShowLoader();
cqsp.getOpportunityById(o.Id);
cqsp.getAccountById(a.Id);
cqsp.redirect('home/home.jsp');
cqsp.checkSyncStatus();
o.Send_to_NetSuite__c = false;
o.NS_Sync__c = 'test';
update o;
cqsp.checkSyncStatus();
CeligoSalesOrderSyncPoller csosp = new CeligoSalesOrderSyncPoller();
System.currentPageReference().getParameters().put('whence', o.Id);
csosp.checkSyncStatus();
System.currentPageReference().getParameters().put('whence', null);
csosp.checkSyncStatus();
System.currentPageReference().getParameters().put('whence', o.Id);
System.currentPageReference().getParameters().put('opp_id', o.Id);
o.Send_to_NetSuite__c = false;
o.Celigo_Sync_Helper__c = true;
o.NS_Sync__c = null;
update o;
System.currentPageReference().getParameters().put('whence', o.Id);
csosp.checkSyncStatus();
cqsp.checkSyncStatus();
System.currentPageReference().getParameters().put('whence', 'test_xyz');
csosp.checkSyncStatus();
csosp.getErrorMessage();
csosp.getMessage();
Test.stopTest();
}
}
Part that fails: oActual = [SELECT id, StageName, StageNameState__c FROM Opportunity where id = :o.id]; System.assert(oActual.StageName == closedStage); System.assert(oActual.StageNameState__c == 'Prospecting');
Trigger
trigger fulfillmentcontactcreatedopp on Opportunity (before insert, before update) {
Map<Id, Opportunity> oppsToCheckMap = new Map<Id, Opportunity>();
for(Opportunity opp : trigger.new) {
if(opp.Stage_Calc__c >= 5 ){
if(trigger.isInsert ||
trigger.isUpdate && trigger.oldMap.get(opp.Id).StageName != opp.StageName)
oppsToCheckMap.put(opp.Id, opp);
}
***List<AggregateResult> result = [
select OpportunityId, count(Id)
from OpportunityContactRole
where (Role = 'Fulfillment') and
OpportunityId in :oppsToCheckMap.keySet() group by OpportunityId];***
for(AggregateResult aggCount : result) {
if(aggCount.get('expr0') == 0) {
Id oppId = (Id)aggCount.get('OpportunityId');
Opportunity oppo = oppsToCheckMap.get(oppId);
}
Integer fulcount = Integer.valueOf(aggCount.get('expr0'));
opp.Number_of_Fulfillment_Contacts__c = fulcount;
}
}
}
Line that is referenced: List< AggregateResult> result = [ select OpportunityId, count(Id) from OpportunityContactRole where (Role = 'Fulfillment') and OpportunityId in :oppsToCheckMap.keySet() group by OpportunityId];
Netsuite_Sales_Order__c Trigger
trigger celigoUpdateSalesOrder on NetSuite_Sales_Order__c (before insert, before update, before delete) {
if (System.Trigger.new != null) {
List<Id> opIds = new List<Id>();
for(NetSuite_Sales_Order__c order: System.Trigger.new) {
if (order.Opportunity__c == null)
continue;
opIds.add(order.Opportunity__c);
}
List<Opportunity> ops = [select id, StageName, StageNameState__c from Opportunity where Id IN :opIds];
celigo_connector__c cs = celigo_connector__c.getInstance('Opportunity Closed Stage');
String closedStage = (cs!=null)?cs.value__c:'Closed Won';
for (Opportunity o : ops){
if (Trigger.isInsert) {
if (o.StageNameState__c == null) {
o.StageNameState__c = o.StageName;
o.StageName = closedStage;
}
}
o.Celigo_Sync_Helper__c = true;
update o;
}
}
if (Trigger.isDelete && System.Trigger.old != null) {
List<Id> opIds = new List<Id>();
for (NetSuite_Sales_Order__c order: System.Trigger.old) {
if (order.Opportunity__c == null)
continue;
opIds.add(order.Opportunity__c);
}
List<Opportunity> ops = [select id, StageName, StageNameState__c from Opportunity where Id IN :opIds];
List<NetSuite_Sales_Order__c> tOrders = [select id, Opportunity__c from NetSuite_Sales_Order__c where Opportunity__c IN :opIds];
Map<Id,List<NetSuite_Sales_Order__c>> m = new Map<Id,List<NetSuite_Sales_Order__c>>();
for(NetSuite_Sales_Order__c order : tOrders){
if(m.get(order.Opportunity__c) == null)
m.put(order.Opportunity__c, new NetSuite_Sales_Order__c[]{order});
else{
List<NetSuite_Sales_Order__c> temp = m.get(order.Opportunity__c);
temp.add(order);
}
}
Set<Id> keys = m.keySet();
for(Id i : keys){
List<NetSuite_Sales_Order__c> temp = m.get(i);
Opportunity op;
for(Opportunity tempOp : ops){
if(tempOp.Id == i){
op = tempOp;
break;
}
}
if(temp.size() > 1)
continue;
if(op.StageNameState__c == null)
continue;
op.StageName = op.StageNameState__c;
op.StageNameState__c = null;
update op;
}
}
}
delete order;
operation happening on line 280 and points to thebefore delete
behavior. Can you edit this question and post thebefore delete
trigger code on theNetSuite_Sales_Order__c
object? I suspect that there is anupdate
statement in a loop on each of the items in the delete trigger.