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
ADNADN 

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();
     }
}


}



Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

One way

 

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); } } }

 

All Answers

Starz26Starz26

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');

 

}

Starz26Starz26

One way

 

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); } } }

 

This was selected as the best answer
ADNADN

Thanks for your help with my Trigger error Message

Your Trigger Example does work and perform exactly the functionality which I want

 

Many thanks 

 

 

ADNADN

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);

       

     }

}

 

 

}



ADNADN

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);
 
 
 }
 }

 



 

Starz26Starz26
Make sure that the account you are testing with also has 2 contacts associated with it.
ADNADN

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:

 

@isTest
 Private class UmbrellaAccountTriggertest{
 static testMethod void testUmbrellaAccount(){


Account a = new account(name='TestAccount',RecordtypeId='012A0000000nY0V'); //Office Record Type
 Insert a;
 a =[select name, updated__c, RecordtypeId, (select id from contacts)From Account where id=: a.id];
System.assertEquals(Null, a.updated__c);
System.assertEquals(0, a.Contacts.size());
 System.assertEquals('012A0000000nY0V', a.RecordtypeId);
 
contact c = new contact(LastName='ContactTest',AccountID=a.id);
insert c;
a = [select name, updated__c, RecordTypeId from account where id=:a.id];
update a;
a = [select name, updated__c, RecordTypeId from account where id=:a.id];
System.assertEquals(1, a.updated__c);

Try{
 a = [select name, updated__c, RecordTypeId from account where id=:a.id];
 a.RecordtypeId='012A0000000nY0a';//Umbrella Record Type 
 Update a;
  }
Catch (DMLException e){
a = [select name, updated__c, RecordTypeId from account where id=:a.id];
system.assert(e.getMessage().contains('You can not save Umbrella Record associated with a contact'));

                      }
 
                                               }
                                           }

and the Trigger was :

 

Trigger UpdatePAccWithOppoName on Opportunity (after insert) {
   
// 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;   
  }



 

 

 

ADNADN

The previous Test Trigger was related to the following Trigger:

 

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 = (Integer) mConSize.get(a.ID);
        
     }
}


}