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
sam_Adminsam_Admin 

Update master detail field for trigger

We have custom object order managemet which was child object for account (look up field), some of the order management fields gets populated through trigger, but we recently changed the lookup to master detail and receiving error when creating new order file

Trigger updates account info from opp and opp and account info from Quote, so now since we changed it to master details, account is required field and how to modify the trigger so account info gets populated from opp before saving?

/*
######################################################################
Trigger Description: 
           Updates the Opportunity and account information from Quote
######################################################################
*/

trigger UpdateQuoteAccOppty on Order_Management__c (before insert, before update) {
 list<Order_Management__c>olist = new list<Order_Management__c>();
 list<Id>qId= new list<Id>();
 list<Id>oId = new list<id>();
 for(Order_Management__c o : Trigger.New)
  { 
   if(o.Oracle_Quote__c!=null)
    qId.add(o.Oracle_Quote__c);
   if((o.Opportunity__c!=null))
     oId.add(o.Opportunity__c);
  }
if(qId.size()>0){
 system.debug('-----------qId-----------'+qId); 
 list<cafsl__Oracle_Quote__c> bqlist = [Select b.cafsl__Opportunity__c,b.cafsl__Opportunity__r.Amount,b.cafsl__Opportunity__r.CurrencyIsoCode,b.cafsl__Opportunity__r.Owner.Name, b.cafsl__Opportunity__r.CloseDate, b.cafsl__Opportunity__r.AccountId, b.Id From cafsl__Oracle_Quote__c b where b.Id in:qId];
 system.debug('-----------bqlist-----------'+bqlist);
 map<Id,Id>QuoOppMap = new map<Id,Id>();
 map<Id,Id>QuoAccMap = new map<Id,Id>();
 map<Id,Decimal>QuoAmtMap = new map<id,Decimal>();
 map<Id,String>QuoOwnerMap = new map<Id,String>();
 map<Id,String>QuoCURmap = new map<Id,String>();
 map<Id,String>QuoSCmap = new map<Id,String>();
 for(cafsl__Oracle_Quote__c bq: bqlist){
    QuoOppMap.put(bq.id,bq.cafsl__Opportunity__c);
    QuoAccMap.put(bq.id,bq.cafsl__Opportunity__r.AccountId);
    QuoAmtMap.put(bq.id,bq.cafsl__Opportunity__r.Amount);
    QuoOwnerMap.put(bq.id,bq.cafsl__Opportunity__r.Owner.Name);
    QuoCURmap.put(bq.id,bq.cafsl__Opportunity__r.CurrencyIsoCode);
 // QuoSCmap.put(bq.id,bq.cafsl__Opportunity__r.Sales_Channel__c);
 }
 system.debug('-----------QuoOppMap-----------'+QuoOppMap);
 system.debug('-----------QuoAccMap-----------'+QuoAccMap);
 for(Order_Management__c o : Trigger.New){
    if(o.Oracle_Quote__c!=null){
    o.Opportunity__c = QuoOppMap.get(o.Oracle_Quote__c);
    o.Account__c =  QuoAccMap.get(o.Oracle_Quote__c);
    o.Amount__c =  QuoAmtMap.get(o.Oracle_Quote__c);
    o.Opp_Owner__c = QuoOwnerMap.get(o.Oracle_Quote__c);
    o.CurrencyIsoCode = QuoCURmap.get(o.Oracle_Quote__c);
 // o.Sales_Channel__c = QuoSCmap.get(o.Oracle_Quote__c);
   }
 }
}

if(oId.size()>0){
  
  list<Opportunity>opptylist = [Select o.Id,o.Amount,o.CurrencyIsoCode,o.AccountId, o.OwnerId, o.Owner.Name, o.CloseDate From Opportunity o where o.Id in: oId];  
  system.debug('----opptylist--'+opptylist);
  map<Id,Id>OppAccMap = new map<Id,Id>();
  map<Id,Decimal>OppAmtMap = new map<Id,Decimal>();
  map<Id,String>OppOwnerMap = new map<Id,String>();
  map<Id,String>OppCurMap = new map<Id,String>();
  map<Id,String>OppSCMap = new map<Id,String>();
  for(Opportunity o:opptylist){
    OppAccMap.put(o.Id,o.AccountId);
    OppAmtMap.put(o.Id,o.Amount);
    OppOwnerMap.put(o.Id,o.Owner.Name);
    OppCurMap.put(o.Id,o.CurrencyIsoCode);
 // OppSCMap.put(o.Id,o.Sales_Channel__c);
    system.debug('----OppSCMap---'+OppSCMap);
  }
  for(Order_Management__c o : Trigger.New){
     o.Account__c =  OppAccMap.get(o.Opportunity__c);
     o.Amount__c =  OppAmtMap.get(o.Opportunity__c);
     o.Opp_Owner__c = OppOwnerMap.get(o.Opportunity__c);
     o.CurrencyIsoCode = OppCurMap.get(o.Opportunity__c);
  // o.Sales_Channel__c = OppSCMap.get(o.Opportunity__c);

     }
 } 
}