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
Michael Hedrick 2Michael Hedrick 2 

Associate existing Record to many newly created records.

Hello All,
I am trying to associate an existing related record(sales rebate__c) to another custom object when they are created(Sales program__c)
Below is what I have so far but I am receiving an error where I am comparing the year on the parent child record.  
There is only a single child(sales_rebate) record that will be associated to many parent accounts.  
Error Message "Initial term of field expression must be a concrete SObject: List<Sales_Rebate__c>"

Thanks for your time and help.
M


trigger AddSalesRebateToSalesProgram on Sales_Program__c (After insert) {
    
 List<Sales_Rebate__c> salesrebate = new List<Sales_Rebate__c>();
 List<Sales_Program__c> salesprogram = new List<Sales_Program__c>();
    
    for(Sales_Program__c sp : Trigger.new)
    {
      *if(salesrebate.Early_Buy_Rebate_Year__c == salesprogram.Current_SBF_Year__c )*
      {
           Sales_Rebate__c rebate = new Sales_Rebate__c();
            rebate.Sales_Program__c = sp.id;  
                  salesrebate.add(rebate);
      }
    }
   
    insert salesrebate;
}
 
Best Answer chosen by Michael Hedrick 2
Zaid DarbarZaid Darbar
Sorry i made a mistake

trigger AddSalesRebateToSalesProgram on Sales_Program__c (After insert, after update) {

List<Sales_Rebate__c> salesrebate = new List<Sales_Rebate__c>([select id, name, Early_Buy_Rebate_Year__c from Sales_Rebate__c ]);
List<Sales_Rebate__c> lstsalesProgram = new List<Sales_Rebate__c>(); // added ;()
List<Sales_Program__c> salesprogram = new List<Sales_Program__c>();
    
    for(Sales_Program__c sp : Trigger.new)
    {
     for (Sales_Rebate__c SR : salesrebate ) 
    {
        if(SR.Early_Buy_Rebate_Year__c == sp.Current_SBF_Year__c ) // I MADE CORRECTION HERE
      {
            //REPLACE NAME OF SalesRebate__c look up field on the Sales Program__c WITH APINAME_OF_FIELD__c
            SP.Sales_Rebate__c = SR.id;
            lstsalesProgram.add(Sp)   ;
      }
    }
}
}

This should work let me know if it doesn't.

All Answers

sridharbsridharb

The error being thrown is because of the IF Condition. In IF condition you are referening List to access the filed, Which is impractical. Access the Current_SBF_Year__c from it s object not from List.

For quick run Just comment IF condition line properly and give try. I am sure you would not get the error.

Regards
Sri

Michael Hedrick 2Michael Hedrick 2
Sri,
You are correct. Removing the enite "If" condition removed the error.  But how can I make sure that the correct Sales Reabte rcord is being associated to the new Sales Program records?  The year fields need to match from both records.
Cheers,
​M
SalesFORCE_enFORCErSalesFORCE_enFORCEr
How will you do this "There is only a single child(sales_rebate) record that will be associated to many parent accounts." I see you have a lookup on Sales Rebate to Sales Program then how will you achieve this statement?
Michael Hedrick 2Michael Hedrick 2
The more I edit the code the more confusing its getting.  SalesFORCE_enFORCEr yes there is a lookup to both objects.  I am now seeing if I could just populate the Sales_Rebate__c field on the Sales Program object.  But I still need to make sure I am grabbing the latest Sales Rebate record.  That is why I had the "If" statement. 
Thanks
M
Zaid DarbarZaid Darbar
First of all you need to query Sales_Rebate__c records before you start comparing them in IF condition.

so it will go something like this : 


trigger AddSalesRebateToSalesProgram on Sales_Program__c (After insert) {
 
// you need to add a where cluase in this query according to your logic i am just writing it to associate 1 record to every Sales Program record   
 List<Sales_Rebate__c> salesrebate = new List<Sales_Rebate__c>([select id, name, Early_Buy_Rebate_Year__c from Sales_Rebate__c limit 1 ]);
 List<Sales_Program__c> salesprogram = new List<Sales_Program__c>();
    
    for(Sales_Program__c sp : Trigger.new)
    {
      
      *if(salesrebate[0].Early_Buy_Rebate_Year__c == salesprogram.Current_SBF_Year__c )*
      {
           Sales_Rebate__c rebate = new Sales_Rebate__c();
            rebate.Sales_Program__c = sp.id;  
                  salesrebate.add(rebate);
      }
    }
   
    insert salesrebate;
}

mark this answer as true if helpful or let me know if you have any issues.
Zaid DarbarZaid Darbar
You need to understand following things

1) 1 sales rebate__c record can be tied to 1 Sales_Program__c only.

so,

you cannot do this
*if(salesrebate.Early_Buy_Rebate_Year__c == salesprogram.Current_SBF_Year__c )*

because of 2 reasons
1) it does not know which salesrebate record to compare with
2) If it is already there then it must be linked with salesprogram already.

let me know the requirement here i can help you with the code.
Michael Hedrick 2Michael Hedrick 2
Thanks Zaid.  Now that I understand the restriction listed in point1.  Can ​a single Sales Rebate__C record be assigned to the  lookup field on many Sales Program__ records?

The requirements are this:
A single Sales Rebate__C record is created every year.  This record wil be used by all "new" Sales Program__c records that will be created in the following months in a calendar year.  I need to tie the Latest Sales Rebate__C to the newly created Sales Program__c records.   There is a field on both object that houses a year value: salesrebate.Early_Buy_Rebate_Year__c = salesprogram.Current_SBF_Year__c 
That is how I thought I could marry these records together assuming they both had the same Year.
Thanks agian for you time,
M

 
sridharbsridharb
Hi Michael,

No Need of If condition, Just write SOQL query to get Sales_Rebate__c record for the current year and use that record as parent reference for all new sales_program__c records.

Regards
Sri
Michael Hedrick 2Michael Hedrick 2
Sri,
I am not sure I could use --- SELECT Id FROM Sales_Rebate__c WHERE Early_Buy_Rebate_Year__c = 'THIS YEAR'
The 'Early_Buy_Rebate_Year__c' field is a text field that is being populated from a workflow.  The field is text so that it could be made unique.    
Thanks
M
Zaid DarbarZaid Darbar
trigger AddSalesRebateToSalesProgram on Sales_Program__c (After insert) {
    
 List<Sales_Rebate__c> salesrebate = new List<Sales_Rebate__c>([select id, name, Early_Buy_Rebate_Year__c from Sales_Rebate__c ]);
List<Sales_Rebate__c> salesrebateToUpdate = new List<Sales_Rebate__c>
 List<Sales_Program__c> salesprogram = new List<Sales_Program__c>();
    
    for(Sales_Program__c sp : Trigger.new)
    {
     for (Sales_Rebate__c SR : salesrebate ) 
    {
        if(SR.Early_Buy_Rebate_Year__c == salesprogram.Current_SBF_Year__c )
      {
           Sales_Rebate__c rebate = new Sales_Rebate__c();
            SR.Sales_Program__c = sp.id;  
                  salesrebateToUpdate.add(SR);
      }
    }
   
    Update salesrebateToUpdate;
}

But Remember this will tie Sales Program to Sales Rebate record.

Let me know if this is what you need or you need to Sales Rebate to Sales program.
sridharbsridharb
I think this should work
SELECT Id FROM Sales_Rebate__c WHERE Early_Buy_Rebate_Year__c =:THIS_YEAR

If not Convert the  Year to String

Sting year = THIS_YEAR;

Use this  SELECT Id FROM Sales_Rebate__c WHERE Early_Buy_Rebate_Year__c =:year

Your almost there. Good Luck

Regards
Sri
Michael Hedrick 2Michael Hedrick 2
Zaid, When you say "But Remember this will tie Sales Program to Sales Rebate record." do you mean the Sales Program__c object will show as a related records on the Sales Rebate__c this is fine.  But the Sales Rebate look up field on the Sales Program__c record will be populated with the correct Sales Rebate record.
.
In regard to the trigger I am receiving the error: Initial term of field expression must be a concrete SObject: List<Sales_Program__c>
This is related to this line of code.   if(SR.Early_Buy_Rebate_Year__c == salesprogram.Current_SBF_Year__c )

So I tried Sri option:
String year = THIS_YEAR;
List<Sales_Rebate__c> salesrebate = new List<Sales_Rebate__c>([SELECT Id FROM Sales_Rebate__c WHERE Early_Buy_Rebate_Year__c =:year]);

But I recieved this Error: Variable does not exist: THIS_YEAR

 
Zaid DarbarZaid Darbar
Please replace if confition with this line

 if(SR.Early_Buy_Rebate_Year__c == sp.Current_SBF_Year__c )

sorry i forgot to add that...
Zaid DarbarZaid Darbar
trigger AddSalesRebateToSalesProgram on Sales_Program__c (After insert) {
    
 List<Sales_Rebate__c> salesrebate = new List<Sales_Rebate__c>([select id, name, Early_Buy_Rebate_Year__c from Sales_Rebate__c ]);
List<Sales_Rebate__c> salesrebateToUpdate = new List<Sales_Rebate__c>
 List<Sales_Program__c> salesprogram = new List<Sales_Program__c>();
    
    for(Sales_Program__c sp : Trigger.new)
    {
     for (Sales_Rebate__c SR : salesrebate ) 
    {
        if(SR.Early_Buy_Rebate_Year__c == sp.Current_SBF_Year__c ) // I MADE CORRECTION HERE
      {
            SR.Sales_Program__c = sp.id;  
                  salesrebateToUpdate.add(SR);
      }
    }
   
    Update salesrebateToUpdate;
}
sridharbsridharb
String year = System.Today().year();
List<Sales_Rebate__c> salesrebate = new List<Sales_Rebate__c>([SELECT Id FROM Sales_Rebate__c WHERE Early_Buy_Rebate_Year__c =:year]);

Try this Michael, Cheers
Michael Hedrick 2Michael Hedrick 2
Zaid,
The code worked without error. Awesome.  For curiousity sake and for learning purposes. How can the current trigger be manipulate to have the Sales Rebate__c look up field on the Sales Program__c record be populated?
The trigger above has the Sales Rebate__c object as a related record on the Sales Program object.
Thanks to you both for your time and insightful approach to solving problems....
Regards,
M
Zaid DarbarZaid Darbar
I don not know the name of Sales Rebate__c look up field on the Sales Program__c.

trigger AddSalesRebateToSalesProgram on Sales_Program__c (Before insert) {
    
 List<Sales_Rebate__c> salesrebate = new List<Sales_Rebate__c>([select id, name, Early_Buy_Rebate_Year__c from Sales_Rebate__c ]);
List<Sales_Rebate__c> salesrebateToUpdate = new List<Sales_Rebate__c>
 List<Sales_Program__c> salesprogram = new List<Sales_Program__c>();
    
    for(Sales_Program__c sp : Trigger.new)
    {
     for (Sales_Rebate__c SR : salesrebate ) 
    {
        if(SR.Early_Buy_Rebate_Year__c == sp.Current_SBF_Year__c ) // I MADE CORRECTION HERE
      {
            //REPLACE NAME OF SalesRebate__c look up field on the Sales Program__c WITH APINAME_OF_FIELD__c
            SP.APINAME_OF_FIELD__c = SR.id;
            salesrebateToUpdate.add(SP)   
      }
    }
   
}
Michael Hedrick 2Michael Hedrick 2
I am away from my computer but I am fairly certain the lookup on the Sales Program object is Sales Rebate. I will try here shortly. Thanks again.....
M
Michael Hedrick 2Michael Hedrick 2
Hello Zaid,
I have update the code but it is throwing an error: Incompatible element type Sales_Program__c for collection of Sales_Rebate__c
Any thoughts?


trigger AddSalesRebateToSalesProgram on Sales_Program__c (After insert, after update) {

List<Sales_Rebate__c> salesrebate = new List<Sales_Rebate__c>([select id, name, Early_Buy_Rebate_Year__c from Sales_Rebate__c ]);
List<Sales_Rebate__c> salesrebateToUpdate = new List<Sales_Rebate__c>(); // added ;()
List<Sales_Program__c> salesprogram = new List<Sales_Program__c>();
    
    for(Sales_Program__c sp : Trigger.new)
    {
     for (Sales_Rebate__c SR : salesrebate ) 
    {
        if(SR.Early_Buy_Rebate_Year__c == sp.Current_SBF_Year__c ) // I MADE CORRECTION HERE
      {
            //REPLACE NAME OF SalesRebate__c look up field on the Sales Program__c WITH APINAME_OF_FIELD__c
            SP.Sales_Rebate__c = SR.id;
            salesrebateToUpdate.add(Sp)   ;
      }
    }
}
}
Michael Hedrick 2Michael Hedrick 2
Also, The lookup field is named Sales_Rebate__c just like the custom object.
Zaid DarbarZaid Darbar
Sorry i made a mistake

trigger AddSalesRebateToSalesProgram on Sales_Program__c (After insert, after update) {

List<Sales_Rebate__c> salesrebate = new List<Sales_Rebate__c>([select id, name, Early_Buy_Rebate_Year__c from Sales_Rebate__c ]);
List<Sales_Rebate__c> lstsalesProgram = new List<Sales_Rebate__c>(); // added ;()
List<Sales_Program__c> salesprogram = new List<Sales_Program__c>();
    
    for(Sales_Program__c sp : Trigger.new)
    {
     for (Sales_Rebate__c SR : salesrebate ) 
    {
        if(SR.Early_Buy_Rebate_Year__c == sp.Current_SBF_Year__c ) // I MADE CORRECTION HERE
      {
            //REPLACE NAME OF SalesRebate__c look up field on the Sales Program__c WITH APINAME_OF_FIELD__c
            SP.Sales_Rebate__c = SR.id;
            lstsalesProgram.add(Sp)   ;
      }
    }
}
}

This should work let me know if it doesn't.
This was selected as the best answer
Michael Hedrick 2Michael Hedrick 2

Zaid,
Thank you for the quick reply and time.  
I am still getting the same error: Incompatible element type Sales_Program__c for collection of Sales_Rebate__c
 

Do we need 2 lists of the same object?


 

Michael Hedrick 2Michael Hedrick 2
Zaid,
I got it.  Since the code is populating a Lookup field with an existing value I just removed the "lstsalesProgram.add(Sp);"
Also , I changed the trigger evernts to before.  Using after thre an error. 
Thanks again for all your help. You're awesome!

trigger AddSalesRebateToSalesProgram on Sales_Program__c (before insert, before update) {
List<Sales_Rebate__c> salesrebate = new List<Sales_Rebate__c>([select id, name, Early_Buy_Rebate_Year__c from Sales_Rebate__c ]);
List<Sales_Rebate__c> lstsalesProgram = new List<Sales_Rebate__c>(); // added ;()
List<Sales_Program__c> salesprogram = new List<Sales_Program__c>();
    
    for(Sales_Program__c sp : Trigger.new)
    {
     for (Sales_Rebate__c SR : salesrebate ) 
    {
        if(SR.Early_Buy_Rebate_Year__c == sp.Current_SBF_Year__c ) // I MADE CORRECTION HERE
      {
            //REPLACE NAME OF SalesRebate__c look up field on the Sales Program__c WITH APINAME_OF_FIELD__c
            SP.Sales_Rebate__c = SR.id;
         //   lstsalesProgram.add(Sp);
      }
    }
}
}

 
Michael Hedrick 2Michael Hedrick 2
Now I just need to create a test class :)
Cheers,
M
Zaid DarbarZaid Darbar
Glad to know you are good with this :) 
Michael Hedrick 2Michael Hedrick 2
Hey Guys,
Since you two are familiar with the trigger I am using I was wondering when you have time if you could take a look at my test which I posted here:
https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XcoZQAS

I could not get the tets class to work until I changed the Account field on the Sales Program from a Master Detail relationship to just a lookup.  Do you reference a Master Detail field differently than a standard lookup?

Regards,
M