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
padmini sspadmini ss 

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

Best Answer chosen by padmini ss
Virendra ChouhanVirendra Chouhan
Hi Padmini !

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
Account__r.BillingCity !=  Route__r.City__r.Name
This is the Best way for this situation.

Best Regards
Virendra
version7.7@hotmail.com (mailto:version7.7@hotmail.com)
 

All Answers

Virendra ChouhanVirendra Chouhan
HI Padmini,

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 
padmini sspadmini ss
Hi Virendra ,
pls give the sample to query it for that , i am struck
Virendra ChouhanVirendra Chouhan
Hi Padmini !
Sure take try this !
 
trigger AvoidCityNomatch on Route_Outlet__c (Before Insert, Before Update) {
      Set<id> routID = new Set<id>();
	Set<id> acctId = new set<Id>();	
     for(Route_Outlet__c ro:Trigger.new){
         routId.add(ro.rout__c);
	acctId.add(ro.account__c);
     }
    list<rout__c> routList = [SELECT id,rout__r.city__c.name FROM rout__c where ID IN: routId];
    String acctBilling = [SELECT BillingCity FROM Account where ID IN: acctId limit 1].BillingCity;

 for(rout__c rt : routList){   
        if(rt.City__c.name != acctBilling)
         rt.City__c.adderror('Account City & Rout City No Match'); // If this is not working in error
	 trigger.new[0].rout__c.addError('Write error here');    // use 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)
Aditya MohanAditya Mohan
Hi Padmini,

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: 
trigger AvoidCityNomatch on Route_Outlet__c (before insert, before update) {
	
	List <Route_Outlet__c> lstRouteOutlet = new List <Route_Outlet__c>();
	
	for(Route_Outlet__c ro: Trigger.new){
		lstRouteOutlet.add(ro);
	}
	
	List <Account> lstAccountCity = new List <Account>();
	lstAccountCity = [SELECT ID,
							 BillingCity
							 FROM Account
							 WHERE ID =:lstRouteOutlet.Account__c
					 ];

	List <City_c> lstCity = new new List <City_c>();
	lstCity = [SELECT ID,
					  City_c
					  FROM Route__c
					  WHERE ID =:lstRouteOutlet.Route__c]

	for(Route_Outlet__c newRO : lstRouteOutlet){
		if(lstAccountCity.BillingCity!=lstCity.City_c){
			newRO.adderror("Account and Route City do not match");
		}
	}

}
padmini sspadmini ss

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];

padmini sspadmini ss

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

Virendra ChouhanVirendra Chouhan
Hi Padmini,
Here's a little mistak.
 just correct the query with
list<route__c> routList = [SELECT id, city__r.name FROM route__c where ID IN: routId];

Here City__c is Parent object right?

Regards
Virendra
padmini sspadmini ss
yes, virenda..
padmini sspadmini ss

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];

padmini sspadmini ss

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');
        
}
    

}


}

 

Virendra ChouhanVirendra Chouhan
Hi Padmini

Now you are combinig both codes mine and Aditya's.
Both are diffrent approch.

If you are going with set then use this code
    trigger AvoidCityNomatch on Route_Outlet__c (Before Insert, Before Update) {
        Set<id> routID = new Set<id>();
        Set<id> acctId = new set<Id>();
       for(Route_Outlet__c ro:Trigger.new){
        	  routId.add(ro.rout__c);
	          acctId.add(ro.account__c);
          }

     list<rout__c> routList = [SELECT id,city__r.name FROM rout__c where ID IN: routId];

     String acctBilling = [SELECT BillingCity FROM Account where ID IN: acctId limit 1].BillingCity;

  for(rout__c rt : routList){  
          if(rt.City__c.name != acctBilling){
          rt.City__c.adderror('Account City & Rout City No Match'); // If this is not working in error
          trigger.new[0].rout__c.addError('Write error here');    // use this
         }
     }
}

 
padmini sspadmini ss
Hi Virendra , i am trying wtih ur 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>
Virendra ChouhanVirendra Chouhan
Its route__c not __r
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];
Virendra ChouhanVirendra Chouhan
I apologies ! my mistak in spelling of route__c to rout__c :-|
trigger AvoidCityNomatch on Route_Outlet__c (Before Insert, Before Update) {
        Set<id> routID = new Set<id>();
        Set<id> acctId = new set<Id>();
       for(Route_Outlet__c ro:Trigger.new){
              routId.add(ro.route__c);
              acctId.add(ro.account__c);
          }

     list<route__c> routList = [SELECT id,city__r.name FROM route__c where ID IN: routId];

     String acctBilling = [SELECT BillingCity FROM Account where ID IN: acctId limit 1].BillingCity;

  for(route__c rt : routList){  
          if(rt.City__r.name != acctBilling){
          rt.City__c.adderror('Account City & Rout City No Match'); // If this is not working in error
          
         }
     }
}

The Correct one!
But its not Bulkify!
Virendra ChouhanVirendra Chouhan
Hi Padmini !

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
Account__r.BillingCity !=  Route__r.City__r.Name
This is the Best way for this situation.

Best Regards
Virendra
version7.7@hotmail.com (mailto:version7.7@hotmail.com)
 
This was selected as the best answer
padmini sspadmini ss
Thank you Virendra, The Trigger is working fine., Also The Validatin Is Simple And Working 100%