You need to sign in to do that
Don't have an account?
Trigger To Check Before Saving the Record
Ther are 4 objects.
Route__c is a master
Route_outlet__c is Junction object for Route__c & Account.
And For Route__c there is one lookup for City__c
So My Requirment is,,,, While saving the Rout_outlet__c Record.
We must check the Account__r.BillingCity & route__r.City__r.name
if these two matches the rout_outlet__c record must save.
my Trigger is this
trigger AvoidCityNomatch on Route_Outlet__c (Before Insert, Before Update) {
for(Route_Outlet__c ro:Trigger.new){
if(ro.route__r.City__c!=ro.Account__r.BillingCity)
Ro.City__c.adderror('Account City & Rout City No Match');
}
}
in debug log ro.route__r.City__c showing null, pls guide me
By the way you can do this thing using validation on Route_Outlet__c object.
why don't you use it. it is very easy compare to trigger.
just put this in Error condition This is the Best way for this situation.
Best Regards
Virendra
version7.7@hotmail.com (mailto:version7.7@hotmail.com)
All Answers
In trigger.new list it only store the data from that perticular Sobject and about related object only one field is stored that is Id.
you can't access the 2nd level record from the Trigger.new or old.
In this case you have to query for that.
Regards
Virendra
pls give the sample to query it for that , i am struck
Sure take try this !
Note : this is not a tested code! may be here some minor mistakes please solve them.
Let me know it is working or not.
Regards
Virendra
version7.7@hotmail.com (mailto:version7.7@hotmail.com)
Using Trigger.new will give you access to the fields of that particular object and ID of related object. You need to access the related object fields using SOQL. You can do it like this:
Hi Aditya....,Virendra Chouhan, i am getting an error like this...
Didn't understand relationship 'route__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 8 column 31
In this line,
list<route__c> routList = [SELECT id,route__r.city__c.name FROM route__c where ID IN: routId];
Even i changed to ...
list<route__c> routList = [SELECT id,route__r.city__r.name FROM route__c where ID IN: routId];
Hi Virendra,
Initial term of field expression must be a concrete SObject: LIST<RouteOutlet__c> at line 17 column 66
This is where the error is Virendra...
lstAccountCity = [SELECT ID, BillingCity FROM Account WHERE ID =:lstRouteOutlet.Account__r];
Help me out..pls
thanks
Here's a little mistak.
just correct the query with
Here City__c is Parent object right?
Regards
Virendra
now it throws...
Variable does not exist: routId at line 17 column 79
list<route__c> routList = [SELECT id, city__r.name FROM route__c where ID IN: routId];
this is my entire code...now...
trigger AvoidCityNomatch on RouteOutlet__c (before insert, before update)
{
List <RouteOutlet__c> lstRouteOutlet = new List <RouteOutlet__c>();
for(RouteOutlet__c ro: Trigger.new)
{
lstRouteOutlet.add(ro);
}
List <Account> lstAccountCity = new List <Account>();
list<route__c> routList = [SELECT id, city__r.name FROM route__c where ID IN: routId];
List <City__c> lstCity = new List <City__c>();
lstCity = [SELECT ID,City__c FROM Route__c
WHERE ID =:lstRouteOutlet.Route__c];
for(RouteOutlet__c newRO : lstRouteOutlet)
{
if(lstAccountCity.BillingCity!=lstCity.City_c)
{
newRO.adderror('Account and Route City do not match');
}
}
}
Now you are combinig both codes mine and Aditya's.
Both are diffrent approch.
If you are going with set then use this code
list<rout__c> routList = [SELECT id,city__r.name FROM route__r where ID IN: routId];
Error
Initial term of field expression must be a concrete SObject: LIST<RouteOutlet__c>
list<rout__c> routList = [SELECT id,city__r.name FROM route__r where ID IN: routId];
replace
list<rout__c> routList = [SELECT id,city__r.name FROM route__c where ID IN: routId];
The Correct one!
But its not Bulkify!
By the way you can do this thing using validation on Route_Outlet__c object.
why don't you use it. it is very easy compare to trigger.
just put this in Error condition This is the Best way for this situation.
Best Regards
Virendra
version7.7@hotmail.com (mailto:version7.7@hotmail.com)