You need to sign in to do that
Don't have an account?
Michael 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;
}
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;
}
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
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
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
Thanks
M
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.
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.
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
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
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
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.
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
.
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
if(SR.Early_Buy_Rebate_Year__c == sp.Current_SBF_Year__c )
sorry i forgot to add that...
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;
}
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
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
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)
}
}
}
M
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) ;
}
}
}
}
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.
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?
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);
}
}
}
}
Cheers,
M
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