+ Start a Discussion
Cynthia Rodgers 12Cynthia Rodgers 12 

I need assistance with a trigger that has field update.

I am using a standard object and custom object thats related.  I am trying to solve for the following:  When LLC_BI_Status__c = Complete, update the Opportunity stage (stagename) to "Won".  Object Names; Product Package (API= Product_Package). Status (LLC_BI_Status__c)  and Opportunitites (Stagename)
 
PRABHAKARAN CHOCKALINGAMPRABHAKARAN CHOCKALINGAM
Trigger OppUpdate on Product_Package___c(after insert, after update){

//To store the list of opportunities that needs to be updated.
Set<ID> OppIds = new Set<ID>();

//Get the List of Opportunities for which the related object has the status set to Complete.
for(Product_Package__C prodPkg : Trigger.new){
if(prodPkg.LLC_BI_Status__C == 'Complete') {
OppIds.add(prodPkg.Opportunity__C);
}
}

List<Opportunity> oppLst = new List<Opportunity>();
 
//Query the list of opportunities and update the stageName and update.
for(Opportunity oppToUpd : [SELECT Id, StageName FROM Opportunity WHERE id in :OppIds])
{
oppToUpd.StageName = 'Won';
oppLst.add(oppToUpd);
   }

update oppLst;

}

I haven't compiled this and I assumed the custom object is Product_Package.
Cynthia Rodgers 12Cynthia Rodgers 12
Thanks Prabhakaran.  I am working on compiling now and had to make some updates but Im now getting a unexpected token error on line 13 ; List<Opportunity> oppLst = new List<Opportunity>();                         
I updated the query a bit and this is what I have so far                                                                                                                                 Trigger OppUpdate on Opportunity(after insert, after update){

//To store the list of opportunities that needs to be updated.
Set<ID> OppIds = new Set<ID>();

//Get the List of Opportunities for which the related object has the status set to Complete.
for(Opportunity Name = Trigger.new;;
)if(prodPkg.LLC_BI__Status__C == 'Complete') {
OppIds.add(prodPkg.Name);
}
}

List<Opportunity> oppLst = new List<Opportunity>();
 
//Query the list of opportunities and update the stageName and update.
for(Opportunity oppToUpd : [SELECT Id, StageName FROM Opportunity WHERE id in :OppIds])
{
oppToUpd.StageName = 'Won';
oppLst.add(oppToUpd);
   }

update oppLst;

}
PRABHAKARAN CHOCKALINGAMPRABHAKARAN CHOCKALINGAM
If my understanding is correct, you are trying to update the opportunity based on an update in the custom object(Product_Package), correct?
Or is there a custom field in Opportunity based on which you wanted to updated the StageName. plz confirm.
Cynthia Rodgers 12Cynthia Rodgers 12
Yes that is correct I want to update the opportunity stage based on the status in the custom object for Product package.  The field that is associated to the Product_Package is the opportunity name.
PRABHAKARAN CHOCKALINGAMPRABHAKARAN CHOCKALINGAM
The initial code should work fine. As the trigger event is generated on the custom object, the trigger should be defined for the custom object.
The below line of code has to be updated based on the API field name used for the relation between PRODUCT_PACKAGE & Opportunity,
OppIds.add(prodPkg.Opportunity__C);  -- replace Opportunity__C with the API field name.