You need to sign in to do that
Don't have an account?
Neeraj Sharma 103
I have a Custom Field ZipCode(Text) and another Custom Field Owner(Lookup User) on Custom Object Territory
i Everyone i am new in Salesforce
So Please Help Me Out From this Scenario
I have a two Custom Field on a Custom Object
1.ZipCode(Text)
Api Name=Zip_Code__c
2.Owner(Lookup User)
Api Name=Owner
On Territory(Territory__c) Custom Object
My Condition is When i Insert a Zip Code like example:
1.332713=Aaron(Owner)
2.332713=Blake(Owner)
3.332713=Denise(Owner)
When i again insert 332713 and assign to a new Owner then Display a error we can not Assign a zipcode to this user Only a single zipcode is assigned to only three Owners(Sales Representatives)
Three Sales Representatives at most can be assigned to a single zipcode.Display an error if a user attempts to associate another sales representative to a zipcode
Please Replay As Soon Possible I dont know how to over come from this scenario so Please help me
Thanks
Neeraj Sharma
So Please Help Me Out From this Scenario
I have a two Custom Field on a Custom Object
1.ZipCode(Text)
Api Name=Zip_Code__c
2.Owner(Lookup User)
Api Name=Owner
On Territory(Territory__c) Custom Object
My Condition is When i Insert a Zip Code like example:
1.332713=Aaron(Owner)
2.332713=Blake(Owner)
3.332713=Denise(Owner)
When i again insert 332713 and assign to a new Owner then Display a error we can not Assign a zipcode to this user Only a single zipcode is assigned to only three Owners(Sales Representatives)
Three Sales Representatives at most can be assigned to a single zipcode.Display an error if a user attempts to associate another sales representative to a zipcode
Please Replay As Soon Possible I dont know how to over come from this scenario so Please help me
Thanks
Neeraj Sharma
Try with the below code. You may need to change based on your org:
trigger SFDC_ZipcodeValidation on Terriotry__c (before insert,before Update) {
for (Terriotry__c T : Trigger.new)
{
//String OwnerName = T.OwnerId;
Integer y = [Select count() from Terriotry__c where Terriotry__c.OwnerId =:T.OwnerId and Terriotry__c.Id !=: T.Id and Terriotry__c.Zip_Code__c=:T.Zip_Code__c];
if(y>3)
{
T.addError('we can not Assign a zipcode to this user Only a single zipcode is assigned to only three Owners(Sales Representatives)'+y);
}
}
}
If it resolve your issue, mark as answer.
Thanks,
Narendar
All Answers
Try with the below code. You may need to change based on your org:
trigger SFDC_ZipcodeValidation on Terriotry__c (before insert,before Update) {
for (Terriotry__c T : Trigger.new)
{
//String OwnerName = T.OwnerId;
Integer y = [Select count() from Terriotry__c where Terriotry__c.OwnerId =:T.OwnerId and Terriotry__c.Id !=: T.Id and Terriotry__c.Zip_Code__c=:T.Zip_Code__c];
if(y>3)
{
T.addError('we can not Assign a zipcode to this user Only a single zipcode is assigned to only three Owners(Sales Representatives)'+y);
}
}
}
If it resolve your issue, mark as answer.
Thanks,
Narendar
thanku so much your code is working for me i modified your code according to my scenario so its useful for me thanks again
thanks
Neeraj Sharma
Can you pls share the above code in bukified form.
Here SOQL Query is inside for loop, Any suggestion for that.
Posting my version of code here.
trigger zipcount on Territory__c (before insert,before update) {
set<string> zipown = New set<string>();
for(Territory__c ter : Trigger.new){
zipown.add(ter.Zip_Code__c);
}
Map<string,integer> zipoqn = new map<string,integer>();
for(AggregateResult ter1 :[SELECT Zip_Code__c,COUNT(OwnerSR__c) owncount FROM Territory__c WHERE Zip_Code__c IN :zipown GROUP BY Zip_Code__c]){
zipoqn.put(string.valueof(ter1.get('Zip_Code__c')),integer.valueof(ter1.get('owncount')));
}
for(Territory__c ter2 :trigger.new){
if(zipoqn.containskey(ter2.Zip_Code__c) && zipoqn.get(ter2.Zip_Code__c)==3){
ter2.adderror('cannot assign more than 3 reps for this zipcode');
}
}
}
Thank you,
Bharath