You need to sign in to do that
Don't have an account?
Sai Bhaskar 16
I need a trigger where i have 3 objects account, contact, and city and the city has no relationship with any object. The account has a field called City--c. if city--c and city object name is matched, then only contact should be created
I need a trigger where I have 3 objects account, contact, and city and the city has no relationship with any object. The account has a field called City--c. If city--c and city object name is matched, then the only contact should be created
Try this one out i hope it is going to work for you,if it is still not working then kindly share with me whatever error and exception you are getting :
Trigger:
Trigger-Handler:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
Try the following trigger and its handler class it may helpful for you and it works in my Org :
Trigger:
TriggerHandler:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Not Working Sir.
Thanks & Regards
Sai Bhaskar 16
This is what I have tried. And there seems to be a mistake here. Please correct the same and help me to get the right one for this Scenario.
When Creating an Account With the With city field name equal to city name in the city Object. Its throwing an error in this way.
Error Message :
Apex trigger AccCityTrigger caused an unexpected exception, contact your administrator: AccCityTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ContactTrigger: maximum trigger depth exceeded Account trigger event AfterInsert Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert Account trigger event AfterUpdate Contact trigger event AfterInsert: []: Trigger.AccCityTrigger: line 17, column 1
Trigger AccCityTrigger on Account (Before Insert, After Insert,After Update) {
List<CityObj__c> CityList =[SELECT Id, Name FROM CityObj__c ];
Set<String> CitySet = New Set<String>();
for(CityObj__c c: CityList){
CitySet.add(c.Name);
}
List<Contact> ConList = New List<Contact>();
for(Account acc :Trigger.New){
if(CitySet.Contains(acc.City__c)){
Contact con = New Contact();
con.LastName ='acc.Name'+'City';
con.AccountId = acc.Id;
ConList.add(con);
}
}
Insert ConList ;
}
Try this one out i hope it is going to work for you,if it is still not working then kindly share with me whatever error and exception you are getting :
Trigger:
Trigger-Handler:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
It's working well Sir. And thank you for your time and this solution of yours helped me a lot.
Thanks and Regards
Sai Bhaskar16