I have two fields in a Lookup relationship with city names, i.e. Start city and End city; In this scenario I want to make sure that , the city which i selected in the start city filed, should not be visible in End city field.
Please try below code by replacing Your_SObject by you Object Name. This code will not remove the city used in Start City but will not let you save record by throwing error if start City and End City are Same.
trigger Triggertovalidate on Your_SObject (BEFORE INSERT,BEFORE UPDATE)
{
LIST<Your_SObject> li = [select id,Start_City__c, End_City__c from Your_SObject where type != null];
for(Your_SObject c :trigger.new)
{
if(c.Start_City__c != null && c.End_City__c != null){
for(Your_SObject existrecord :li){
if(existrecord.End_City__c == c.Start_City__c ){
c.End_City__c .adderror('Start City and End City should be different.');//If you need specify the SObject field also
}
}
}
}
}
Please try below code by replacing Your_SObject by you Object Name. This code will not remove the city used in Start City but will not let you save record by throwing error if start City and End City are Same. Please let me know if helped.
Regards,
Ramakant