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
Kris WebsterKris Webster 

Populate Lookup Field Upon Record Creation

I am trying to create a trigger that will populate a lookup field on a custom object called Levvel Forecat.

The basic criteria here is that when a new Levvel Forecast record is created with a specific record type I need to update Assignment__c (Lookup field to assignment (pse__assignment__c) object) with the current active assignment that the contact (lookup field to Contact record) on the levvel Forecast record is on.

So what I have done is trying to perform a select statement to store the contact id (Employee__c) on the Levvel Forecast record so we can use that in the second Select statement as a comparison.

I have then created a second SELECT statement to select the pse__Assignment__c WHERE the Contact Id on the assignment is EQUAL to the contact Id on the levvel Forecast.

Then I have a simple IF statement, and if it passed I am trying to insert that assignment ID into the Assignment__c field on the Levvel Forecast record.

CURENTLY in my test class I am getting the error saying that there is “Too many SOQL queries 101”

Any help would be greatly appreciated!

I have this methos inserted within my LevvelForecastTriggerHandler so i am sorry if it doesnt look like a conventional Trigger..

**** The method that is running all this is the "loadAssignment" method, and is also looking at the BULKBEFORE after the line that reads "if(trigger.isInsert)"....
 
/************************************************************************************************************************
// Name         LevvelForecastTriggerHandler
// Description  Standard trigger handler for LevvelForecastTrigger
// INITIAL    2019-Aug-16  KWEBB     Initial version
************************************************************************************************************************/
public without sharing class LevvelForecastTriggerHandler extends TriggerHandlerBase 
{
    //List for querying forecast periods for DELETE
    private List<Forecast_Periods__c> forecastPeriodList = new List<Forecast_Periods__c>();

    //list for storing all active assignemnts that are realted to the new forecast creations
    private List<pse__Assignment__c> assignmentList = new List<pse__Assignment__c>();

    /************************************************************************************************************
    // Name         bulkBefore
    // Description  Standard implementation of the TriggerHandler.bulkBefore() interface method.
    *************************************************************************************************************/
    public override void bulkBefore()
    {
        if (trigger.isDelete)
        {
            //store parent Levvel Forecast records that are about to be deleted 
            list<Id> forecastIds = new list<Id>();
            for(SObject so :trigger.old)
            {
                Levvel_Forecast__c forecast = (Levvel_Forecast__c)so;
                forecastIds.add(forecast.id);
            }

                //Now we need to query all child Forecast Period records related to the Levvel Forecast we are about to delete
                forecastPeriodList = [SELECT Id, Levvel_Forecast__c FROM Forecast_Periods__c WHERE Levvel_Forecast__c IN: forecastIds];
                system.debug('List of Forecast Periods ' + forecastPeriodList);
            }

        if(trigger.isInsert)
        {
            //store the LevvelForecast record for update 
            list<Id> forecastsToUpdate = new List<Id>();
            for(SObject so :trigger.new)
            {
               
                Levvel_Forecast__c newForecast = (Levvel_Forecast__c)so;
                if(newForecast.RecordTypeId == '0121N000000U6GYQA0' )
                {
                    forecastsToUpdate.add(newForecast.employee__c);
                }

            }

                //Now we need to query all the assignments 
                assignmentList = [SELECT id, pse__Resource__c FROM pse__Assignment__c WHERE pse__Resource__c IN: forecastsToUpdate];

            }
    }

    

    /************************************************************************************************************
    // Name         beforeInsert
    // Description  Standard implementation of the TriggerHandler.beforeInsert() interface method.
    *************************************************************************************************************/
    public override void beforeInsert (SObject so)
    {
        Levvel_Forecast__c newForecast = (Levvel_Forecast__c)so;

        loadAssignment(newForecast);
    }


    /************************************************************************************************************
    // Name         beforeDelete
    // Description  Standard implementation of the TriggerHandler.beforeInsert() interface method.
    *************************************************************************************************************/
    public override void beforeDelete(SObject so)
    {
        Levvel_Forecast__c oldForecast = (Levvel_Forecast__c)so;

        deleteForecastPeriods(oldForecast);
    }


     /************************************************************************************************************
    // Name         loadAssignment
    // Description  Helper method to validate resource requests on an opportunity
    *************************************************************************************************************/
    private void loadAssignment (Levvel_Forecast__c newForecast)
    {

        for (pse__Assignment__c ass :assignmentList )
        if(ass.pse__End_Date__c > newForecast.Today__c && ass.pse__Is_Billable__c == TRUE)
        {
            newForecast.employee__c = ass.pse__resource__c;
        }
    }

    /************************************************************************************************************
    // Name         deleteForecastPeriods
    // Description  Helper method to validate resource requests on an opportunity
    *************************************************************************************************************/
    private void deleteForecastPeriods (Levvel_Forecast__c oldForecast)
        {
            //Now we delete 
            delete forecastPeriodList;
        }
    }