+ Start a Discussion
mgodseymgodsey 

Does this trigger need a map? Trying to update parent object based on child.

We have a custom object called Deal_Desk_Form__c which is in a master-detail relationship with Opportunity. I'm trying to write a trigger that updates a field on the Opportunity based on a field on the Deal_Desk_Form__c. This is my current code:

trigger MasterDDValidation on Deal_Desk_Form__c (before insert,before update) {

 
//create list of Opportunity IDs
List<ID>OppIDs = New List <ID>();
for(Deal_Desk_Form__c dd: Trigger.new){
    OppIDs.add(dd.Opportunity__c);
    }

//get Deal Desk Ids + Version Type
List<Deal_Desk_Form__c>DDform=[SELECT Id, VersionType__c, Opportunity__c FROM Deal_Desk_Form__c WHERE Id in: Trigger.new];
   
//create list of opportunities to iterate on
List<Opportunity> OppUpdateList = [SELECT Id, HasMasterDDForm__c FROM Opportunity WHERE id in: OppIDs];
          
    for(integer i=0; i < OppUpdateList.size(); i++){
        if(DDform.VersionType__c == 'Master Terms'){
        OppUpdateList[i].HasMasterDDForm__c += 1;}
       
        if(DDform.VersionType__c != 'Master Terms'){
        OppUpdateList[i].HasMasterDDForm__c +=0;}
}
}
update OppUpdateList;        
}

 

Right now I'm getting the error message: Compile Error: unexpected token: update at line 24 column 0. 

 

Do I need some sort of map to relate the Opportunity to the Deal Desk Form? Thank you so much for any help you can offer!

SLockardSLockard

That error is occurring because you have too many closing brackets. Try taking out one of the closing brackets before the update.

mgodseymgodsey

Hi SLockard. Thanks for your help! Unfortunately now I'm getting the following error message:

 

Compile Error: Initial term of field expression must be a concrete SObject: LIST<Deal_Desk_Form__c> at line 17 column 12. I have line 17 highlighted in red below. Any Idea why this is happening? 

 

trigger MasterDDValidation on Deal_Desk_Form__c (before insert,before update) {


//create list of Opportunity IDs
List<ID>OppIDs = New List <ID>();
for(Deal_Desk_Form__c dd: Trigger.new){
OppIDs.add(dd.Opportunity__c);
}

//get Deal Desk Ids + Version Type
List<Deal_Desk_Form__c>DDform=[SELECT Id, VersionType__c, Opportunity__c FROM Deal_Desk_Form__c WHERE Id in: Trigger.new];

//create list of opportunities to iterate on
List<Opportunity> OppUpdateList = [SELECT Id, HasMasterDDForm__c FROM Opportunity WHERE id in: OppIDs];

for(integer i=0; i < OppUpdateList.size(); i++){
if(DDform.VersionType__c == 'Master Terms'){
OppUpdateList[i].HasMasterDDForm__c += 1;}

if(DDform.VersionType__c != 'Master Terms'){
OppUpdateList[i].HasMasterDDForm__c +=0;}
}
update OppUpdateList;
}

jd123jd123

Hey at the end you have given one extra bracket

 

try this code

 

trigger MasterDDValidation on Deal_Desk_Form__c (before insert,before update) {


//create list of Opportunity IDs
List<ID>OppIDs = New List <ID>();
for(Deal_Desk_Form__c dd: Trigger.new){
OppIDs.add(dd.Opportunity__c);
}

//get Deal Desk Ids + Version Type
List<Deal_Desk_Form__c>DDform=[SELECT Id, VersionType__c, Opportunity__c FROM Deal_Desk_Form__c WHERE Id in: Trigger.new];

//create list of opportunities to iterate on
List<Opportunity> OppUpdateList = [SELECT Id, HasMasterDDForm__c FROM Opportunity WHERE id in: OppIDs];

for(integer i=0; i < OppUpdateList.size(); i++){
if(DDform.VersionType__c == 'Master Terms'){
OppUpdateList[i].HasMasterDDForm__c += 1;}

if(DDform.VersionType__c != 'Master Terms'){
OppUpdateList[i].HasMasterDDForm__c +=0;}
}
update OppUpdateList;
}

sai.sfsai.sf

trigger MasterDDValidation on Deal_Desk_Form__c (after insert,after update) {


//create list of Opportunity IDs
List<ID>OppIDs = New List <ID>();
for(Deal_Desk_Form__c dd: Trigger.new){
    OppIDs.add(dd.Opportunity__c);
    }

//get Deal Desk Ids + Version Type
List<Deal_Desk_Form__c>DDform=[SELECT Id, VersionType__c, Opportunity__c FROM Deal_Desk_Form__c WHERE Id in: Trigger.new];
   
//create list of opportunities to iterate on
//List<Opportunity> OppUpdateList = [SELECT Id, HasMasterDDForm__c,(SELECT Id, VersionType__c, Opportunity__c FROM Deal_Desk_Form__r) FROM Opportunity WHERE id in: OppIDs];
          
    for(integer i=0; i < OppUpdateList.size(); i++){
       for(integer d=0;d<OppUpdateList[i].Deal_Desk_Form__r.size();d++){
        
       if(Deal_Desk_Form__c[d].VersionType__c == 'Master Terms'){
        OppUpdateList[i].HasMasterDDForm__c += 1;}
       
        if(Deal_Desk_Form__c[d].VersionType__c != 'Master Terms'){
        OppUpdateList[i].HasMasterDDForm__c +=0;}
      }
   }

update OppUpdateList;        
}