You need to sign in to do that
Don't have an account?
brozinickr
Problem with Validation Trigger
Hello,
I'm having issues with a trigger that I wrote to prevent users from saving cases with adding the Account Name and Contract ID. Users can't submit record with a blank Account Name and Contract ID but they are able to submit records with blank Account Name and have a Contract ID.
Here's my code:
trigger PopulateSPIDContractIdonCase on Case (before insert, before update) { Profile p = [SELECT Id from Profile where Name='System Administrator']; if(System.Trigger.IsUpdate||System.Trigger.IsInsert) { for (Case c : trigger.new) if (UserInfo.getProfileId() != p.Id && (c.Reason__c == 'Recon Related') && (c.Account == NULL) && (c.Contract_ID__c == NULL)) { c.addError(' Please enter a Contract ID and Account Name.'); } } }
Yes this should fix your problem. . . . "OR" logic will make your code so that if account name OR contract id is empty, the error will display.
"I want it to throw an error if they are both blank."
In what instance would you want to throw an error if one is blank and one is not? I'm assuming you always want those fields to be populated?
Don't forget to mark this as solution and give Kudos!
All Answers
You are saying if the account name is empty and the contract id is empty then throw an error. . . But if the contract ID is not empty, the condition is false; therefore, it does not matter if there is an account name. You need to change this from "And" logic to "Or" logic.
Try this:::
if(userinfo.getprofileid() != p.id && (c.reason__c == ' recon related') && (c.account == Null || c.contract_id__c == null))
Don't forget to mark this as solution and give Kudos!
Yes, so if Contract ID and Account Name are false, throw an error. I want it to throw an error if they are both blank. The weird thing is that when I type in Account Name and leave Contract ID, it throws the error. But if I just type Contract ID and leave Account Name blank, it will let me the save the record. Will adding the or fix this?
Yes this should fix your problem. . . . "OR" logic will make your code so that if account name OR contract id is empty, the error will display.
"I want it to throw an error if they are both blank."
In what instance would you want to throw an error if one is blank and one is not? I'm assuming you always want those fields to be populated?
Don't forget to mark this as solution and give Kudos!
i made your change but now it's erring out when I try to save the record with all the values, it won't let me save.
http://smg.photobucket.com/user/NobodysAngel621/media/fsdfsdagewt_zps45600408.jpg.html?filters[user]=168379&filters[recent]=1&sort=1&o=0
I think I solved the Issue. When I changed the reference to c.Account to c.AccountId, I was able to get the validation to work. What I find interesting is that it even let me use c.Account and didn't give me an exception error or save error. Oh well. Thanks for your help!
Rachel