+ Start a Discussion
KlivingstonKlivingston 

Error: Compile Error: Expression cannot be assigned at line -1 column -1

Can anyone find the problem with this code please:

trigger SalesHistoryRecordTypeTrigger on Account (after insert, after update){
 //One query, fill two maps. 
Map<String,Id> accountRecordTypes = new Map<String,Id>();
 Map<String,Id> ytdRTMap = new Map<String,Id>(); 
for(RecordType rt: [Select Name, Id, sObjectType From RecordType Where sObjectType in ('Account','Sales_History_YTD__c')and isActive=true]) { 
    if(String.valueof(rt.sObjectType).equalsIgnoreCase('Account')){
       accountRecordTypes.put(rt.Name,rt.Id); } 
  else{ ytdRTMap.put(rt.name,rt.Id); } 
} 
for(Account a : Trigger.new){ 
if(a.RecordTypeId == accountRecordTypes.get('Regional_View_Lit_ppl')){
    Sales_History_YTD__c.RecordTypeId = ytdRTMap.get('Regional_View_Record'); } 
else 
if(a.RecordTypeId == accountRecordTypes.get('Dollar View')){ 
    Sales_History_YTD__c.RecordTypeId = ytdRTMap.get('Page Layout Dollar view'); } 
else if(a.RecordTypeId == accountRecordTypes.get('Dual Currency View')){
    Sales_History_YTD__c.RecordTypeId = ytdRTMap.get('Page Layout Dual View'); }
 }
 } 

 

I am trying to change the record type of a child object (Sales_History_YTD__c) whenever a record type of its parent object changes (Account).

 

Your help is much appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

You will need in your trigger the following

 

Map<ID, Account> aIdToAccountWRelRecsMap = new Map<ID,Account> ([select id, (select id, recordTypeId from Sales_History_YTD__r) from Account where id IN : Trigger.newMap.keySet()]);

 This will fetch into a map keyed by the account id the Account and all child Sales_History_YTD__c records for that Account

 

Your main loop will need to change to something like this

List<Sales_History_YTD__c> shyUpdList = new List<Sales_History_YTD__c> ();
for (Account a: Trigger.new) {
  for (Sales_History_YTD__c shy : aIdToAccountWRelRecsMap.get(a.id).sales_history_ytd__r) {
//  do the work of assigning the recordTypeId to each Sales_History_YTD__c
   shyUpdateList.add(shy);
  }
}
try {
update shyUpdList;
}
catch (Exception e) {// your exception logic here}

 

All Answers

crop1645crop1645

reformatting (you really should format your code to get the best possible assistance)

trigger SalesHistoryRecordTypeTrigger on Account (after insert, after update){
 //One query, fill two maps. 
Map<String,Id> accountRecordTypes = new Map<String,Id>();
 Map<String,Id> ytdRTMap = new Map<String,Id>(); 
for(RecordType rt: [Select Name, Id, sObjectType From RecordType Where sObjectType in ('Account','Sales_History_YTD__c')and isActive=true]) { 
    if(String.valueof(rt.sObjectType).equalsIgnoreCase('Account')){
       accountRecordTypes.put(rt.Name,rt.Id); } 
  else{ ytdRTMap.put(rt.name,rt.Id); } 
} 
for(Account a : Trigger.new){ 
if(a.RecordTypeId == accountRecordTypes.get('Regional_View_Lit_ppl')){
    Sales_History_YTD__c.RecordTypeId = ytdRTMap.get('Regional_View_Record'); } 
else 
if(a.RecordTypeId == accountRecordTypes.get('Dollar View')){ 
    Sales_History_YTD__c.RecordTypeId = ytdRTMap.get('Page Layout Dollar view'); } 
else if(a.RecordTypeId == accountRecordTypes.get('Dual Currency View')){
    Sales_History_YTD__c.RecordTypeId = ytdRTMap.get('Page Layout Dual View'); }
 }
 } 

The trigger has a conceptual problem. You are looping through triggered Accounts

 

for(Account a : Trigger.new){ .. }

 but you never fetch the child Sales_History_YTD__c objects in order to set their record type.  You will also need to use a DML update on these fetched objects once their record type is set

KlivingstonKlivingston

I am sorry for the format.

Can you give an example of fetching please! I thought this

  else{ ytdRTMap.put(rt.name,rt.Id); } 

 doing the job? the Sales_History_YTD__c is the child object and I fetched the record types!

 

also would

    update a;

do the job?

crop1645crop1645

You will need in your trigger the following

 

Map<ID, Account> aIdToAccountWRelRecsMap = new Map<ID,Account> ([select id, (select id, recordTypeId from Sales_History_YTD__r) from Account where id IN : Trigger.newMap.keySet()]);

 This will fetch into a map keyed by the account id the Account and all child Sales_History_YTD__c records for that Account

 

Your main loop will need to change to something like this

List<Sales_History_YTD__c> shyUpdList = new List<Sales_History_YTD__c> ();
for (Account a: Trigger.new) {
  for (Sales_History_YTD__c shy : aIdToAccountWRelRecsMap.get(a.id).sales_history_ytd__r) {
//  do the work of assigning the recordTypeId to each Sales_History_YTD__c
   shyUpdateList.add(shy);
  }
}
try {
update shyUpdList;
}
catch (Exception e) {// your exception logic here}

 

This was selected as the best answer
KlivingstonKlivingston

Thanks a lot. The trigger is now working. I need to create a test class now.

crop1645crop1645

Klivingston

 

Take a look at this http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods to learn how to do this