You need to sign in to do that
Don't have an account?
Ramanjot Sidhu
How to map a field from child object to parent object using a trigger
Trigger Not Working
I have two objects: Opportunity (Parent), Sponsorship Offer(Line_item__c) (Child)
On the Opportunity object there is a standardpicklist field called "Stage (StageName), and there is a custom field on the Sponsorship Offer page called Stage (Stage__c) with the same picklist. When an individual goes on to the Opportunity page, and tries to change the Stage, if the value is not equal to the Sponsorship Offer field "Stage", then I want to produce an error.. My trigger is not working. Any help would be greatly appreciated as I am new to apex.
Compile Error: Variable does not exist: o.StageName at line 7 column 8
trigger UpdateStage on Opportunity (before update) {
List<Line_item__c> opportunitywithsponsorshipoffer = [SELECT Opportunity__r.id, Opportunity__r.StageName
from Line_item__c where Line_item__c.Opportunity__c <> Null];
for (Opportunity o: opportunitywithsponsorshipoffer);
for(Line_item__c li : o.li){
o.StageName = li.Stage__c;
if(o.StageName <> null){//If the opportunity stage name is not the same as the Sponsorship Offer stage name present error message
o.StageName = Line_item__c.Stage__c;
} else {
o.adderror ('Cannot track Stage on Opportunity page, please refer to the related Sponsorship Offer page');
} //close for loop
}// close for if statement
}// close trigger
I have two objects: Opportunity (Parent), Sponsorship Offer(Line_item__c) (Child)
On the Opportunity object there is a standardpicklist field called "Stage (StageName), and there is a custom field on the Sponsorship Offer page called Stage (Stage__c) with the same picklist. When an individual goes on to the Opportunity page, and tries to change the Stage, if the value is not equal to the Sponsorship Offer field "Stage", then I want to produce an error.. My trigger is not working. Any help would be greatly appreciated as I am new to apex.
Compile Error: Variable does not exist: o.StageName at line 7 column 8
trigger UpdateStage on Opportunity (before update) {
List<Line_item__c> opportunitywithsponsorshipoffer = [SELECT Opportunity__r.id, Opportunity__r.StageName
from Line_item__c where Line_item__c.Opportunity__c <> Null];
for (Opportunity o: opportunitywithsponsorshipoffer);
for(Line_item__c li : o.li){
o.StageName = li.Stage__c;
if(o.StageName <> null){//If the opportunity stage name is not the same as the Sponsorship Offer stage name present error message
o.StageName = Line_item__c.Stage__c;
} else {
o.adderror ('Cannot track Stage on Opportunity page, please refer to the related Sponsorship Offer page');
} //close for loop
}// close for if statement
}// close trigger
Please try below code and it should work.
trigger UpdateStage on Opportunity (before update) {
List<Line_item__c> opportunitywithsponsorshipoffer = [SELECT Opportunity__r.id, Opportunity__r.StageName
from Line_item__c where Line_item__c.Opportunity__c IN :Trigger.NewMap.keyset()];
if( opportunitywithsponsorshipoffer !=null && !opportunitywithsponsorshipoffer.isEmpty())
{
for(Line_item__c liRecord : opportunitywithsponsorshipoffer)
{
if(Trigger.newMap.get(liRecord.Opportunity__r.id)!=null && Trigger.newMap.get(liRecord.Opportunity__r.id).StageName != liRecord.Opportunity__r.StageName )
{
Trigger.newMap.get(liRecord.Opportunity__r.id).adderror('Cannot track Stage on Opportunity page, please refer to the related Sponsorship Offer page');
}
}
}
}
Best Regards,
-Vivek
All Answers
Could you please try below code and let me know if it works.
trigger UpdateStage on Opportunity (before update) {
List<Line_item__c> opportunitywithsponsorshipoffer = [SELECT Opportunity__r.id, Opportunity__r.StageName
from Line_item__c where Line_item__c.Opportunity__c IN :Trigger.NewMap.keyset();
if( opportunitywithsponsorshipoffer !=null && !opportunitywithsponsorshipoffer.isEmpty())
{
for(Line_item__c liRecord : opportunitywithsponsorshipoffer)
{
if(Trigger.newMap(liRecord.Opportunity__r.id)!=null && Trigger.newMap(liRecord.Opportunity__r.id).StageName != liRecord.Opportunity__r.StageName )
{
Trigger.newMap(liRecord.Opportunity__r.id).adderror('Cannot track Stage on Opportunity page, please refer to the related Sponsorship Offer page');
}
}
}
}
Best Regard,
-Vivek
Thanks for the quick reply. I get this error :
Error: Compile Error: unexpected token: ';' at line 3 column 87
Please try below code and it should work.
trigger UpdateStage on Opportunity (before update) {
List<Line_item__c> opportunitywithsponsorshipoffer = [SELECT Opportunity__r.id, Opportunity__r.StageName
from Line_item__c where Line_item__c.Opportunity__c IN :Trigger.NewMap.keyset()];
if( opportunitywithsponsorshipoffer !=null && !opportunitywithsponsorshipoffer.isEmpty())
{
for(Line_item__c liRecord : opportunitywithsponsorshipoffer)
{
if(Trigger.newMap(liRecord.Opportunity__r.id)!=null && Trigger.newMap(liRecord.Opportunity__r.id).StageName != liRecord.Opportunity__r.StageName )
{
Trigger.newMap(liRecord.Opportunity__r.id).adderror('Cannot track Stage on Opportunity page, please refer to the related Sponsorship Offer page');
}
}
}
}
Mark the best answer if it helps you.
Best Regards,
-Vivek
New error produced:
Variable does not exist: Trigger at line 11 column 15
Please try below code and it should work.
trigger UpdateStage on Opportunity (before update) {
List<Line_item__c> opportunitywithsponsorshipoffer = [SELECT Opportunity__r.id, Opportunity__r.StageName
from Line_item__c where Line_item__c.Opportunity__c IN :Trigger.NewMap.keyset()];
if( opportunitywithsponsorshipoffer !=null && !opportunitywithsponsorshipoffer.isEmpty())
{
for(Line_item__c liRecord : opportunitywithsponsorshipoffer)
{
if(Trigger.newMap.get(liRecord.Opportunity__r.id)!=null && Trigger.newMap.get(liRecord.Opportunity__r.id).StageName != liRecord.Opportunity__r.StageName )
{
Trigger.newMap.get(liRecord.Opportunity__r.id).adderror('Cannot track Stage on Opportunity page, please refer to the related Sponsorship Offer page');
}
}
}
}
Best Regards,
-Vivek