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
BrandiTBrandiT 

Trouble with trigger to update parent opportunity record when child record created

I need a second set of eyes on this trigger I'm trying to build.  I don't have a lot of experience with triggers so I found one I'm trying to modify to my purposes.

 

On our opportunity object, if a user needs to make changes after it's been sold, they have to clone the record and start over, instead of editing.  I have a checkbox field called Superceded that should be checked on the original opportunity after it's cloned (this is what I'm trying to accomplish with my trigger).

 

I also have a lookup field for the newly clonsed record that will show the parent or original opportunity called Previous_Version__c.

 

I'm trying to create a trigger that will fire after insert on new opportunities only if the Previous_Version__c field is populated.  The trigger should update the opportunity in the Previous_Version__c lookup field and check the Superceded__c checbox on that parent record.

 

Here is what I've got so far:

 


trigger UpdatePreviousOpp on Opportunity (after insert){
 
Opportunity[] opp;
opp = Trigger.new;
 
set<Id> prevoppIds = new Set <ID>();
 
for (Opportunity op : opp) {
      if(opp.previous_version__c != null) {
         prevoppIds.add(op.previous_version__c);
    }
 
Map<ID, Opportunity> proppsToUpdate = new Map<ID, Opportunity> (
    [select Id, supercedure__c from Opportunity
    where Id in :prevoppIds]);

 
List<Opportunity> preoppUpd = new List<Opportunity>{};
 
for (Opportunity opp : preoppsToUpdate.values()) {
    opp.supercedure__c = true;
    update opp;
    preoppUpd.add(opp);
}


if (preoppUpd != null && !preoppUpd.isEmpty())
Database.update(preoppUpd);

}
}

 

Here is the message I'm getting.  Any idea how I can get this to work?

 

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

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Jeff MayJeff May

found it -- a different typo..

 

inside your Opportunity for loop, you need to reference the op variable, not the opp list.

 

for (Opportunity op : opp) {

 

}

All Answers

Jeff MayJeff May

On this line, you want parens, not brackets:

 

List<Opportunity> preoppUpd = new List<Opportunity>{};

 

should be:

 

List<Opportunity> preoppUpd = new List<Opportunity>();

Vinit_KumarVinit_Kumar

Hi,

 

Change it from

 

List<Opportunity> preoppUpd = new List<Opportunity>{};

 

to List<Opportunity> preoppUpd = new List<Opportunity>();

BrandiTBrandiT

Thanks for your help!  I made the change that both of you suggested, but I'm still getting the same error  :(

 

Here is my code now --

 

trigger UpdatePreviousOpp on Opportunity (after insert){
 
Opportunity[] opp;
opp = Trigger.new;
 
set<Id> prevoppIds = new Set <ID>();
 
for (Opportunity op : opp) {
      if(opp.previous_version__c != null) {
         prevoppIds.add(op.previous_version__c);
    }
 
Map<ID, Opportunity> proppsToUpdate = new Map<ID, Opportunity> (
    [select Id, superceded__c from Opportunity
    where Id in :prevoppIds]);

 
List<Opportunity> preoppUpd = new List<Opportunity>();
 
for (Opportunity popp : preoppsToUpdate.values()) {
    popp.superceded__c = true;
    update popp;
    preoppUpd.add(popp);
}


if (preoppUpd != null && !preoppUpd.isEmpty())
Database.update(preoppUpd);

}
}

Jeff MayJeff May

found it -- a different typo..

 

inside your Opportunity for loop, you need to reference the op variable, not the opp list.

 

for (Opportunity op : opp) {

 

}

This was selected as the best answer
Vinit_KumarVinit_Kumar

Hi Branit,

 

Try below from

 

Opportunity[] opp;
opp = Trigger.new;

 

to

 

List<Opportunity> opp = Trigger.new;

BrandiTBrandiT

Wow, you guys are so quick!  This worked. 

 

Thanks so much!!