• Usha Charles 3
  • NEWBIE
  • 30 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 5
    Replies
Hello,

I am trying to create a validation on Account creation using a trigger (Just to learn APEX). I want only the users with Locale US/UK to be able to create accounts in UK/US & block the others. below is my code. but is dosent seem to be working as I get the error message for all users not just the ones with locale UK/US.

Can Someone please tell me what am I missing here? 

Thanks,
trigger ValidateAccount2 on Account (Before Insert, Before Update) {
    
    String UserLocale = userInfo.getLocale();
       For(Account acc : Trigger.new){
        If (acc.Country__c == ('UK') || acc.country__c =='US' && UserLocale != ('English - United States')){
            acc.addError('You are not allowed to add Accounts to other Regions. Please contact your Sales Admin');
            
        }
    }
}

 
Hello,

Below is a code I created for validating contact deletion. But it dosent seem to work, I'm able to delete the contacts from accounts whose country is India. The trigger is active. Can someone please let me know the reason ?

1    trigger SV1 on Contact (before delete) {
2        //donot allow deletion of any contact whose account country is India
3        for (Contact cont : trigger.old){
4                    if(cont.Account.country__c == 'India'){
5                Cont.addError ('You Cannot delete the contacts of Indian accounts');
6            }
7            
8        }
9            
10    }

Regards,
Usha

 
Hello,

I have the below code to update the country field if the state field is filled. I was able to save the trigger but I'm getting the following error while I try to save a record with the state field. I have System Administrator profile & ave Read/Write Access on Account object..

Please let me know where I have gone wrong, I'm an administrator trying to learn Apex.

1 trigger SU on Account (After update, After insert) {
2     for(Account acct : Trigger.new){
3         if (acct.Billing_state__c == 'Karnataka'){
4             acct.Country__c = 'India';
5             Update acct;
6         }
7     }
8
9 }
10 /** Unsucessful - get this error - Apex trigger SU caused an unexpected exception, contact your administrator: 
11 *SU: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.SU: line 4, column 1
12 **/

 Apex trigger SU caused an unexpected exception, contact your administrator: SU: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.SU: line 4, column 1


Thanks,
Usha


 
Hello,

I have the below code to update the country field if the state field is filled. I was able to save the trigger but I'm getting the following error while I try to save a record with the state field. I have System Administrator profile & ave Read/Write Access on Account object..

Please let me know where I have gone wrong, I'm an administrator trying to learn Apex.

 Apex trigger SU caused an unexpected exception, contact your administrator: SU: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.SU: line 4, column 1

User-added image

Thanks,
Usha



 
Hello,

I am trying to create a validation on Account creation using a trigger (Just to learn APEX). I want only the users with Locale US/UK to be able to create accounts in UK/US & block the others. below is my code. but is dosent seem to be working as I get the error message for all users not just the ones with locale UK/US.

Can Someone please tell me what am I missing here? 

Thanks,
trigger ValidateAccount2 on Account (Before Insert, Before Update) {
    
    String UserLocale = userInfo.getLocale();
       For(Account acc : Trigger.new){
        If (acc.Country__c == ('UK') || acc.country__c =='US' && UserLocale != ('English - United States')){
            acc.addError('You are not allowed to add Accounts to other Regions. Please contact your Sales Admin');
            
        }
    }
}

 
Hello,

Below is a code I created for validating contact deletion. But it dosent seem to work, I'm able to delete the contacts from accounts whose country is India. The trigger is active. Can someone please let me know the reason ?

1    trigger SV1 on Contact (before delete) {
2        //donot allow deletion of any contact whose account country is India
3        for (Contact cont : trigger.old){
4                    if(cont.Account.country__c == 'India'){
5                Cont.addError ('You Cannot delete the contacts of Indian accounts');
6            }
7            
8        }
9            
10    }

Regards,
Usha

 
Hello,

I have the below code to update the country field if the state field is filled. I was able to save the trigger but I'm getting the following error while I try to save a record with the state field. I have System Administrator profile & ave Read/Write Access on Account object..

Please let me know where I have gone wrong, I'm an administrator trying to learn Apex.

1 trigger SU on Account (After update, After insert) {
2     for(Account acct : Trigger.new){
3         if (acct.Billing_state__c == 'Karnataka'){
4             acct.Country__c = 'India';
5             Update acct;
6         }
7     }
8
9 }
10 /** Unsucessful - get this error - Apex trigger SU caused an unexpected exception, contact your administrator: 
11 *SU: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.SU: line 4, column 1
12 **/

 Apex trigger SU caused an unexpected exception, contact your administrator: SU: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.SU: line 4, column 1


Thanks,
Usha


 
Please check if this Challenge is working, I keep getting the error:
Challenge not yet complete... here's what's wrong:
The 'Set Case to Escalated' Workflow field update action was not found.

It is there and it works fine when I create a new case.

Hi,

 

I have read the Apex concepts but I am not getting enough confidence in Apex coding, probably I need to practice more.

I was looking for Apex code examples/samples and use cases in google, but couldnt get any.

Can you please let me know if you have links to the Apex code examples/samples and use cases for Apex to try practicing.

 

Also for Apex classes and triggers, please let me know the following:

 

1. On which parts of Apex, I need to start for code practice and how to proceed further.

2. While practicing, what should be the areas that I need to practice more

3. What areas of Apex are commonly used

4. What areas of Apex are complex which needs more practice

5. How to frame the use cases myself for Apex code practice.

 

 

Thanks,

Priya.