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
Roshan singh 21Roshan singh 21 

how to write validation trigger and send mail when validation fails

Hi All,

Please help me to write a trigger to validate and send mail when validation error comes.
  1. While a user is creating Opportunity and selecting Account in the opportunity page then need to check if the Account having Country and State is same as Individual object County and state then a user can able to create opportunity else throw an error and send mail to a current user's manager.  
User-added image

Please help me how to write trigger for this seenario?

 
Ramu kolliRamu kolli
Hi Roshan,
1. Please check Opportunity.WhatID starts with '001' which is Account.
2. then check if Account's country and State with Individual object County and state. 
3. put above two steps in AND condition then use flags to create an opportunity record or send an email based on boolean flag.
Roshan singh 21Roshan singh 21
Hi Ramu thanks for your reply can you please provide sample code for this?
Ramu kolliRamu kolli
Hi Roshan,
You can try below steps.
declare :
List<Opportunity> OpptList = new List<Opportunity>();
boolean flag = false;
String optId = Oppt.WhatId;
if(optId.startsWith('001'){
//This opportunity is related with account
//You can use like Oppt.User_Details__c to fetch user details records associated with Opportunity
//You need to write SOQL to fetch Individual(country, state) from User details objet like below (as per your diagram)
//create a Set(UserDetailsSet) and add records from Oppt.User_Details__c and pass the Set of IDs in below SOQL
//example:
Set<ID> UserDetailsSet = new Set<ID>();
for(User_Details__c rec: Oppt.User_Details__c){
UserDetailsSet.add(rec.Id);
}
List<User_Details__c> userDtlsList = [SELECT ID,country__c, state__c from User_Details__c where id IN :=UserDetailsSet];
for(User_Details__c dtl : userDtlsList){
   if(dtl.country == oppt.country && dtl.state == oppt.state){
flag = true;
OpptList.add(oppt);
}
}

}else{
    if(!flag){
         //send an email to users
     }
}
if(OpptList.size() > 0){
insert OpptList;
}


P.S : You can use Query Editor in Developer console to check your SOQL. Above code may not compile successfuly. Please let me know if you are still facing issues.
If you are using this code in trigger, Please use context variables trigger.new or trigger.newMap
Roshan singh 21Roshan singh 21
Hi Ramu,
Please find details requirement and help me  

My current scenario I got stuck. please help me 
I am an opportunity object where Account is lookup field. and now i want to validate account country field with individual object country filed that is not directly related to Account object.
But  User object is having a relationship with the Individual object. user and Individual having one to one relationship
now when the user is trying to create opportunity and selecting an account then I need to write validation if selected account country is same as User ->>Individual object country field then the only user can create the opportunity else throw an error. 

Object details:
       1. user
       2. Individual 
       3. Opportunity 
       4. Account

now Individual is having country__c field and account is having Shippingcountry field I need to write a trigger to throw an error.
Please help me it's very urgent 

Thanks
Ramu kolliRamu kolli
Hi Roshan,
This seems very simple.
Id UserId = UserInfo.getUserId();
if(Opportunity.Account.country__c == UserId.Individual__r.country__c){
//create Opportunity here or add to the Opportunity list and finally insert opportunity list
}else{
//throw custom exception

}

Hope this helps.