You need to sign in to do that
Don't have an account?
Klivingston
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.
You will need in your trigger the following
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
All Answers
reformatting (you really should format your code to get the best possible assistance)
The trigger has a conceptual problem. You are looping through triggered Accounts
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
I am sorry for the format.
Can you give an example of fetching please! I thought this
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?
You will need in your trigger the following
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
Thanks a lot. The trigger is now working. I need to create a test class now.
Klivingston
Take a look at this http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods to learn how to do this