You need to sign in to do that
Don't have an account?
simple field update trigger - doesn't fire
Hello,
Need some help here, it's not my first trigger thought i'm still a starter. I've written the following simple cross-object field update trigger, basically when a call center agent enter his report (custom object) some fields need to be update on account level.
I've been checking the manual, boards and can only come up with the idea it's coded correctly yet i have the following issues:
1) the trigger doesn't fire when the RecordType.Name does match but does whenever it doesn't match
2) when the trigger fires, a System.QueryException: List has no rows for assignment to SObject is returned for Account__r.Id
I guess i'm overlooking something really obvious here but i'm out of ideas and starting to have a headache.
Here is my trigger:
trigger updatefields on Prospection_Call_report__c (after insert, after update) {
for(Prospection_Call_report__c ob : Trigger.new){
//issue 1
if (ob.RecordType.Name == 'Account managers' ) {
//issue 2 Account__r.Id returns no rows
Account acc = [SELECT Id FROM Account WHERE Id = :ob.Account__r.Id];
acc.Current_Operator__c = ob.actual_provider__c;
acc.Current_Tariff__c = ob.Actual_tariff__c;
acc.Current_Number_Of_Cards__c = ob.Subscriptions_AM__c;
acc.Current_Contract_End_Date__c = ob.Contract_duration_AM__c;
update acc;
}
}
}
No risk of bulk action as no mass uploads/updates will be done, ever.
Thanks in advance for any help provided
Cheers,
Fred
All Answers
Change the query as follows.
It will work, as of my knowledge whenever you gave __r.the field value will be null, just check with debug.
I hope this will help you.
Thanks a lot
Indeed, it made the trick with Account__c and fixed the second issue.
Any idea why the RecordType.Name doesn't return any value ?
Guess it's for the same reason as Account__r, since both are part of the parents records.
You are correct, the sObject references can only be populated from a SOQL or SOSL query.
-greg
just in case it could be of some use here's the corrected trigger and test method
trigger
trigger updatefields on Prospection_Call_report__c (after insert, after update) {
for(Prospection_Call_report__c ob : Trigger.new){
if (ob.RecordTypeId == '012200000009QSzAAM' ) {
Account acc = [SELECT Id FROM Account WHERE Id = :ob.Account__c];
acc.Current_Operator__c = ob.actual_provider__c;
acc.Current_Tariff__c = ob.Actual_tariff__c;
acc.Current_Number_Of_Cards__c = ob.Subscriptions_AM__c;
acc.Current_Contract_End_Date__c = ob.Contract_duration_AM__c;
update acc;
}
}
}
test method
@isTest
Private class updatefieldsTest {
static testmethod void update_fields_test() {
//first we create the account as a user with correct profile
User testuser = [SELECT Id FROM User WHERE UserRole.Name = 'Account Manager East' AND IsActive = true LIMIT 1];
System.runAs(testuser)
{
Account a1 = new Account (name='test account',Status__c='Prospect');
insert a1;
// next it creates a new report to test after insert
date taskydate = System.today();
Prospection_Call_report__c ob1 = new Prospection_Call_report__c (Account__c = a1.Id, actual_provider__c = 'Mobistar', Actual_tariff__c = 'Prepaid', Subscriptions_AM__c = 20,Contract_duration_AM__c = taskydate );
insert ob1;
// last we check the after update part
ob1.Subscriptions_AM__c = 30;
update ob1;
}
}
}
Cheers,
Fred