You need to sign in to do that
Don't have an account?
![ADN ADN](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Trigger Error Message
Can somebody tell me what is the problem with this trigger: ? I GET the following error message:
SObject row does not allow errors
I am trying to prevent user form save account Record type of Umbrella if it associated with a contact & If else statement: count number of related contacts to that account: a.Updated__c = cons.size();
Trigger UmbrellaAccountTrigger on Account (before insert,before update) {
Set<Id> accountIds = new Set<Id>();
for (Account a : trigger.new) {
accountIds.add(a.Id);
}
for (Account a : [Select Id, Updated__c ,RecordTypeId ,(Select Id from Contacts)From Account Where Id in :accountIds]) {
Contact[] cons = a.Contacts;
if ((cons.size()!=0 )&& (a.RecordTypeId =='012A0000000nY0a')) {
a.addError('You can not save Umbrella Record associated with a contact');
} else {
a.Updated__c = cons.size();
}
}
}
One way
All Answers
You can only .addError to records in the trigger context......
So you will have to build your lists, etc outside and then loop through the trigger.new checking to see if criteria exists and then add error
for(account a : trigger.new){
//check if conditions exist then
a.addError('msg');
}
One way
Thanks for your help with my Trigger error Message
Your Trigger Example does work and perform exactly the functionality which I want
Many thanks
In order to push this trigger to production envionment, i write a test Trigger; but it doesn't help to much with my first Trigger.
Could you give me an idea or provide me a piece of code in order to pass the test coverage of 75 percent.
Many thanks for you collaboration
Trigger UmbrellaAccountTrigger on Account (before insert,before update) {
Set<Id> accountIds = new Set<Id>();
Set<ID> accWithCon = New Set<ID>();
Map<ID,Integer> mConSize = New Map<ID,Integer>();
for (Account a : trigger.new) {
accountIds.add(a.Id);
}
List<Account> lAcct = [Select Id, Updated__c ,RecordTypeId ,(Select Id from Contacts)From Account Where Id in :accountIds]
for(Account a : lAcct){
if(a.Contacts.size() > 0){
accWithCon.add(a.id);
mConSize.put(a.id,a.contacts.size());
}
}
for (Account a : trigger.new) {
if (accWithCon.contains(a.id) && (a.RecordTypeId =='012A0000000nY0a')) {
a.addError('You can not save Umbrella Record associated with a contact');
} else {
a.Updated__c = mConSize.get(a.id);
}
}
}
I find the way to test my first Trigger in order to deploy it to production environment. But still I get only
%78 coverage & i couldn't find the way to cover other two lines code in that Trigger.
Do you have idea about how to reach %100 coverage for the same trigger.
Many thanks for your help
@isTest
Private class UmbrellaAccountTriggertest{
static testMethod void testUmbrellaAccount(){
// set up the account Record
Account a = new account(name='TestAccount');
insert a;
// verify the initial state is as expected
a =[select name,updated__c,RecordtypeId From Account where id=: a.id];
System.assertEquals(a.updated__c,a.contacts.size());
// set up the contact Record
Contact c = new contact(LastName='ContactTest');
insert c;
c=[select Lastname,accountId from contact where accountId=:a.id];
//Verify that result as expected
a=[select name,updated__c,RecordTypeId from account where id=:a.id];
System.assertEquals(a.updated__c, 1);
}
}
The system assert that the related contacts list is one, it's correct.
Thanks for your help, I find out the way to Test My Trigger and Get %100 coverage code to deploy it to production environment
My Trigger test should be as the following:
and the Trigger was :
// Create a Map from Account Ids to Opportunities.
Map<Id, Opportunity> accountIdOpportunityMap = new Map<Id, Opportunity>();
for(Opportunity o : Trigger.new){
accountIdOpportunityMap.put(o.AccountId, o);
}
// Create a list of Accounts to Update.
List<Account> accounts = new List<Account>();
for(Account a : [SELECT Id, Most_Recently_Created_Opportunity_Name__c
FROM Account WHERE Id IN :accountIdOpportunityMap.keySet()]){
a.Most_Recently_Created_Opportunity_Name__c =((Opportunity) accountIdOpportunityMap.get(a.Id)).Name;
accounts.add(a);
}
update accounts;
}
The previous Test Trigger was related to the following Trigger: