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
megan.burchmegan.burch 

Error in Trigger

Hi,

 

I'm trying to write a trigger that takes a custom object record and if a certain field is checked, creates a new record with a different owner. Everything is working except for the owner field. When I try to set the owner field equal to a field from the account object I get this error. Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Account> at line 70 column 32. Any help would be much appreciated. Trigger is below.

 

trigger SharedAccountTrigger on RM_Requests__c (after insert) { 


List<RM_Requests__c> RM = new List<RM_Requests__c>();

List<Account> a = [SELECT Name, Market_Manager_Assignment__c, Market_Manager_Shared1__c, Market_Manager_Shared2__c, Market_Manager_Shared_3__c FROM Account];

    for (RM_Requests__c newRM: Trigger.New)
         if (newRM.RecordTypeID == '01260000000JAaNAAW')
         if (newRM.Market_Manager_Overide__c == True)
         if (newRM.Shared_Market_Account__c == 'YES'){
                 
                 RM.add (new RM_Requests__c(
                     RecordTypeID = '01260000000JBr4AAG',
                     Name = newRM.name,
                     Submitted_to_Promo_Team__c = newRM.Submitted_to_Promo_Team__c,
                     Unexclusive_Date_send_notification__c = newRM.Unexclusive_Date_send_notification__c,
                     Account__c = newRM.Account__c,
                     Reason_Code__c = newRM.Reason_Code__c,
                     Possible_Points__c = newRM.Possible_Points__c,
                     Location_ID__c = newRM.Location_ID__c,
                     Chosen_Placement__c = newRM.Chosen_Placement__c,
                     Market_Manager_Overide__c = True,
                     Current_Stage__c = newRM.Current_Stage__c,
                     MM_Email__c = newRM.MM_Email__c,
                     SMM_Email__c = newRM.SMM_Email__c,
                     RD_Email__c = newRM.RD_Email__c,
                     Duration_Days__c = newRM.Duration_Days__c,
                     Point_of_Sale__c = newRM.Point_of_Sale__c,
                     Point_of_Sale_Other_Notes__c = newRM.Point_of_Sale_Other_Notes__c,
                     Booking_Path__c = newRM.Booking_Path__c,
                     TO_BE_BUILT__c = newRM.TO_BE_BUILT__c,
                     Deal_Type__c = newRM.Deal_Type__c,
                     Percent_off__c = newRM.Percent_off__c,
                     Free_Nights__c = newRM.Free_Nights__c,
                     Dollar_Off__c = newRM.Dollar_Off__c,
                     Promotion_Description__c = newRM.Promotion_Description__c,
                     Minimum_Length_of_Stay__c = newRM.Minimum_Length_of_Stay__c,
                     AP_Band__c = newRM.AP_Band__c,
                     Booking_Start_Date2__c = newRM.Booking_Start_Date2__c,
                     Booking_End_Date__c = newRM.Booking_End_Date__c,
                     MEXICO_ONLY_apply_promo_to_all_nights__c = newRM.MEXICO_ONLY_apply_promo_to_all_nights__c,
                     Stay_Start_Date__c = newRM.Stay_Start_Date__c,
                     Stay_End_Date__c = newRM.Stay_End_Date__c,
                     Blackout_Dates_Restrictions__c = newRM.Blackout_Dates_Restrictions__c,
                     Booking_Start_Date_Time__c = newRm.Booking_Start_Date_Time__c,
                     Booking_End_Date_Time__c = newRM.Booking_End_Date_Time__c,
                     Cumulative_Free_Nights_FN_Only__c = newRM.Cumulative_Free_Nights_FN_Only__c,
                     Select_Nights_to_Apply__c = newRM.Select_Nights_to_Apply__c,
                     Guest_must_arrive_on__c = newRM.Guest_must_arrive_on__c,
                     Rate_Type__c = newRM.Rate_Type__c,
                     Room_Type__c = newRM.Room_Type__c,
                     POS_Channels__c = newRM.POS_Channels__c,
                     Deal_Details__c = newRM.Deal_Details__c,
                     All_POS_Test__c = newRM.All_POS_Test__c,
                     RM_Actioned_Date__c= newRM.RM_Actioned_Date__c,
                     SLA__c = newRM.SLA__c,
                     Submission_Date__c = newRM.Submission_Date__c,
                     GMT_Offset_created_by__c = newRM.GMT_Offset_created_by__c,
                     Adjusted_Location_ID__c = newRM.Adjusted_Location_ID__c,
                     Adjusted_POS__c = newRM.Adjusted_POS__c,
                     Adjusted_Position__c = newRM.Adjusted_Position__c,
                     Adjusted_Start_Time__c = newRM.Adjusted_Start_Time__c,
                     Adjusted_End_Time__c = newRM.Adjusted_End_Time__c,
                     Adjustment_Reason_Code__c = newRM.Adjustment_Reason_Code__c,
                     Account_Owner_Time_of_Creation__c = newRM.Account_Owner_Time_of_Creation__c,
                     ownerid = a.Market_Manager_Assignment__c
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     ));   
         }
   insert RM;
 }

 

 

ForcepowerForcepower

Megan, You're trying to use a List<Account> as a single account.

ownerid = a.Market_Manager_Assignment__c

You probably need:
ownerid = a[0].Market_Manager_Assignment__c

 

Make sure you do have at least one element in that array since you're accessing a[0].

 

Best,

Ram

megan.burchmegan.burch

Thank you! I'm getting this error when I try to run it.

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger SharedAccountTrigger caused an unexpected exception, contact your administrator: SharedAccountTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]: Trigger.SharedAccountTrigger: line 89, column 1

 

I'm assuming it's becuase it's not matching the name of the account against the account on the RM request. I tried modifiying the list line to, but it's saying rm.account__c is an unexpected token.

 

List<Account> a = [SELECT Market_Manager_Assignment__c, Market_Manager_Shared1__c, Market_Manager_Shared2__c, Market_Manager_Shared_3__c FROM Account where name = rm.account__c];

 

 

asish1989asish1989

Hi 

Just before Insert statement add some coditions.Always try to avoid null pointer exception

If(rm !=null && rm.size()>0){

    insert rm;

}

 

If this post answers your question please mark it asl solved and give kudos for this post

 

Thanks

 

 

ForcepowerForcepower

Megan,

Need a colon before rm.account__c

Ram

megan.burchmegan.burch

Thanks Ram. I got this error when I added the colon.

 

  Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<RM_Requests__c> at line 9 column 165

megan.burchmegan.burch

Thanks Ashish. I tried that and got the same error

asish1989asish1989

can you please share that line where exactly you are facing that error?

megan.burchmegan.burch

Hi Anish,

 

This is the line I'm getting the error on.

List<Account> a = [SELECT Market_Manager_Assignment__c, Market_Manager_Shared1__c, Market_Manager_Shared2__c, Market_Manager_Shared_3__c FROM Account where name = :rm.account__c];

  and this is the error

 

  Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<RM_Requests__c> at line 9 column 165

ForcepowerForcepower

Hi Megan,

 

You're trying to use rm as an individual element but it's defined as a list. So you need to index it correctly - may be rm[0]?

 

Just for clarity, I'd suggest naming your variables with a naming convention that makes it easier to understand their purpose. So, rather than using 'a' for an account list, make it accountList; rather than rm, use rmList or something even more readable. 

 

Ram

megan.burchmegan.burch

Is there a way to update this so every time the parents record is updated the fields on the child that was created are also updated?