• daniel.knecht1.396874461719167E12
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 5
    Replies
Hi everyone,

I am currently working on a trigger to fire on insert or update and maintain the number of records with the same name in the respective field on all records with the same name. As I have been confronted with a "Too many SOQL" error, I have continuously tried to improve the efficiency of my trigger/class up to a point, where I cannot believe that the failed Tests are actually linked to this particular piece of code.

Question 1) Do you see anything which I should not do in the below code?
Question 2) Could the below code be part of a recursive trigger-merry-go-round? Would it be an option to check for trigger.old vs. trigger.new comparison on the fields in question? (if no change happened, do nothing)

I am pasting just the most important snippet of the code, as the rest is just part of trigger handling.
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Count number of contacts with same name for duplicate check
      
private static void IdenticalContacts(List<Contact> newContacts){

if(updatingContacts) { return; }
if(checkRecursive2.runOnce()){ 

String IPNames = [SELECT Name FROM Contact WHERE ID in :newContacts].Name;
Integer I = [select count() from Contact where Name = :IPNames]; 
List<Contact> AllIPs = new List <Contact>([SELECT ID, Number_of_contacts_with_same_name__c, Name FROM Contact WHERE Name = :IPNames]);
  
for (Contact b :AllIPs){

 //Sets number of counted records in existing and new records
 b.Number_of_contacts_with_same_name__c = i;
 }

updatingContacts = true;
update AllIPs;
updatingContacts = false;
}
}


 
Hi all,

Developing a few triggers in our sandbox, I was about to get them deployed into our live org (100% coverage, all clean and good in test). However, in the live org, I always run into a conflict of "Too many SOQL queries 101". I have tried to bulkify my triggers (e.g. on queries in for loops etc) and eventually realized, it all comes down to a test class that someone else previously developed. In said test class, two methods cause the exceptions. My intention now is to 1) either clean up this test class OR 2) temporarily "deactivate" it to upload my triggers (not the preferred way).

To stick to no. 1, do you know what I need to consider in the below test class?

Many thanks in advance and best regards,
Daniel

@isTest
private class IntegrationQueueTrigger_TEST {

  static testUtils util = new testUtils();

  ////////////////////////////////////////////////////////////////////////////////////////////////////
  @isTest static void Insert_IPs() {  
    Test.startTest();
    List<Contact> ips = new List<Contact>();
    ips = util.createTestIpProjectStack(1, 'Test');

    List<Integration_Queue__c> items = new List<Integration_Queue__c>();
    Integration_Queue__c item = new Integration_Queue__c();

    item.Type__c   = sapIntegrationUtils.IP_INTEGRATION_NAME;
    item.Item_Id__c = ips[0].id;
    items.add(item);
   
    insert items;
    Test.stopTest();
  }
  
  ////////////////////////////////////////////////////////////////////////////////////////////////////
  @isTest static  void Insert_CAs() {
    Test.startTest(); 
    // Implement test code
    List<Contact> ips = new List<Contact>();
    ips = util.createTestIpProjectStack(1, 'Test');

    List<Integration_Queue__c> items = new List<Integration_Queue__c>();
    Integration_Queue__c item = new Integration_Queue__c();

    // get the account back (via placements) for this IP
    Account a = new Account();
    a.id = [Select id, ts2__Project__r.ts2__Client__c FROM ts2__Placement__c WHERE ts2__Employee__c =: ips[0].id].ts2__Project__r.ts2__Client__c;

    item.Type__c   = sapIntegrationUtils.CA_INTEGRATION_NAME;
    item.Item_Id__c = a.id;
    items.add(item);
          insert items;
    Test.stopTest();
  }
  
}

Hi everyone,

I have been putting together a trigger to update all related child objects (ts2__Applications__c) if a change occurs on the parent (ts2__Project_Job__c). If a project is being set to "Cancelled" or "Lost", all related Applications should be set to "Not selected" status. The below trigger has worked flawlessly in our sandbox and got me 100% coverage. Yet, I realized that I need to bulkify it in order to get it into production. Being new to Apex programming, could anyone give me a hand to get the below trigger to work properly?

Many thanks and best regards,
Daniel

Trigger ProjectStatusChangeLostCancelled on ts2__Project_Job__c (after update){
List<ts2__Application__c> lstToUpdate = new List<ts2__Application__c>();
for(ts2__Application__c StatusChangePOT :[select Staffing_Status__c from ts2__Application__c  where  ts2__Project__c in : trigger.new AND ts2__Project__r.Status__c IN ('Cancelled', 'Lost')]){
if (
StatusChangePOT.Staffing_Status__c == 'IP proposed'
||
StatusChangePOT.Staffing_Status__c == 'Alternative IP proposed'
||
StatusChangePOT.Staffing_Status__c == 'IP contacted')
{
StatusChangePOT.Staffing_Status__c = 'IP not selected';
}
lstToUpdate.add(StatusChangePOT);
}
if(!lstToUpdate.isEmpty())
update lstToUpdate;
}

Dear all,

I have been playing around and looking for a solution to my problem on these forums for quite a bit now without success (it is worth mentioning I am new to the developer side of SF). Here is what I am trying to accomplish:

Users in our system create a record project__c (master object). The trigger then is meant to create a record for Selling_Team__c (child), using the project owner (or the running user's ID if the first does not work) to populate "User__c" (look-up field). My problem is that I cannot get the ID into the child record without error - "INVALID_FIELD_FOR_INSERT_UPDATE". I have tried several ways with map(), list(), etc. but have not yet managed to get this done. Without the ID insert for User__c, the trigger works fine (child record gets created with information for role__c).

Any help on this would be highly appreciated! (please see below for details of trigger)
Many thanks,

Daniel


-------
trigger CreateSellingTeam on ts2__Project_Job__c  (after insert)
{
//Creates set for Project records
   list<Selling_Team__c> STList = new list<Selling_Team__c>{};

//Loop for new record
  for (ts2__Project_Job__c Pro :trigger.new)
    {STList.add(new Selling_Team__c (
        Role__c = 'Lead CSP',
        User__c = UserInfo.getUserID()
    ));
    } {
    insert STList;
    try { insert STList; } catch (Exception Ex) { system.debug(Ex);
                                                }
                }
}

Hi all,

Developing a few triggers in our sandbox, I was about to get them deployed into our live org (100% coverage, all clean and good in test). However, in the live org, I always run into a conflict of "Too many SOQL queries 101". I have tried to bulkify my triggers (e.g. on queries in for loops etc) and eventually realized, it all comes down to a test class that someone else previously developed. In said test class, two methods cause the exceptions. My intention now is to 1) either clean up this test class OR 2) temporarily "deactivate" it to upload my triggers (not the preferred way).

To stick to no. 1, do you know what I need to consider in the below test class?

Many thanks in advance and best regards,
Daniel

@isTest
private class IntegrationQueueTrigger_TEST {

  static testUtils util = new testUtils();

  ////////////////////////////////////////////////////////////////////////////////////////////////////
  @isTest static void Insert_IPs() {  
    Test.startTest();
    List<Contact> ips = new List<Contact>();
    ips = util.createTestIpProjectStack(1, 'Test');

    List<Integration_Queue__c> items = new List<Integration_Queue__c>();
    Integration_Queue__c item = new Integration_Queue__c();

    item.Type__c   = sapIntegrationUtils.IP_INTEGRATION_NAME;
    item.Item_Id__c = ips[0].id;
    items.add(item);
   
    insert items;
    Test.stopTest();
  }
  
  ////////////////////////////////////////////////////////////////////////////////////////////////////
  @isTest static  void Insert_CAs() {
    Test.startTest(); 
    // Implement test code
    List<Contact> ips = new List<Contact>();
    ips = util.createTestIpProjectStack(1, 'Test');

    List<Integration_Queue__c> items = new List<Integration_Queue__c>();
    Integration_Queue__c item = new Integration_Queue__c();

    // get the account back (via placements) for this IP
    Account a = new Account();
    a.id = [Select id, ts2__Project__r.ts2__Client__c FROM ts2__Placement__c WHERE ts2__Employee__c =: ips[0].id].ts2__Project__r.ts2__Client__c;

    item.Type__c   = sapIntegrationUtils.CA_INTEGRATION_NAME;
    item.Item_Id__c = a.id;
    items.add(item);
          insert items;
    Test.stopTest();
  }
  
}

Hi everyone,

I have been putting together a trigger to update all related child objects (ts2__Applications__c) if a change occurs on the parent (ts2__Project_Job__c). If a project is being set to "Cancelled" or "Lost", all related Applications should be set to "Not selected" status. The below trigger has worked flawlessly in our sandbox and got me 100% coverage. Yet, I realized that I need to bulkify it in order to get it into production. Being new to Apex programming, could anyone give me a hand to get the below trigger to work properly?

Many thanks and best regards,
Daniel

Trigger ProjectStatusChangeLostCancelled on ts2__Project_Job__c (after update){
List<ts2__Application__c> lstToUpdate = new List<ts2__Application__c>();
for(ts2__Application__c StatusChangePOT :[select Staffing_Status__c from ts2__Application__c  where  ts2__Project__c in : trigger.new AND ts2__Project__r.Status__c IN ('Cancelled', 'Lost')]){
if (
StatusChangePOT.Staffing_Status__c == 'IP proposed'
||
StatusChangePOT.Staffing_Status__c == 'Alternative IP proposed'
||
StatusChangePOT.Staffing_Status__c == 'IP contacted')
{
StatusChangePOT.Staffing_Status__c = 'IP not selected';
}
lstToUpdate.add(StatusChangePOT);
}
if(!lstToUpdate.isEmpty())
update lstToUpdate;
}

Dear all,

I have been playing around and looking for a solution to my problem on these forums for quite a bit now without success (it is worth mentioning I am new to the developer side of SF). Here is what I am trying to accomplish:

Users in our system create a record project__c (master object). The trigger then is meant to create a record for Selling_Team__c (child), using the project owner (or the running user's ID if the first does not work) to populate "User__c" (look-up field). My problem is that I cannot get the ID into the child record without error - "INVALID_FIELD_FOR_INSERT_UPDATE". I have tried several ways with map(), list(), etc. but have not yet managed to get this done. Without the ID insert for User__c, the trigger works fine (child record gets created with information for role__c).

Any help on this would be highly appreciated! (please see below for details of trigger)
Many thanks,

Daniel


-------
trigger CreateSellingTeam on ts2__Project_Job__c  (after insert)
{
//Creates set for Project records
   list<Selling_Team__c> STList = new list<Selling_Team__c>{};

//Loop for new record
  for (ts2__Project_Job__c Pro :trigger.new)
    {STList.add(new Selling_Team__c (
        Role__c = 'Lead CSP',
        User__c = UserInfo.getUserID()
    ));
    } {
    insert STList;
    try { insert STList; } catch (Exception Ex) { system.debug(Ex);
                                                }
                }
}