+ Start a Discussion
Sai Bhaskar 16Sai 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
 
Best Answer chosen by Sai Bhaskar 16
Ajay K DubediAjay K Dubedi
Hi Sai,
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 CheckCityAccount on Account (after insert) {
if(trigger.isInsert && trigger.isAfter){
        CheckCityAccountHandler.insertContact(trigger.new);
    }
}

Trigger-Handler:
public class CheckCityAccountHandler {
    public static void insertContact(List<Account> accList){
        if(accList.size()>0){
            try{
                List<Contact> conList=new List<Contact>();
                List<City__c> cityList=new List<City__c>();
                cityList=[SELECT Id,Name FROM City__c LIMIT 10000];
                Set<String> citySet=new Set<String>();
                for(City__c cty:cityList){
                    citySet.add(cty.Name);
                }
                for(Account acc:accList){
                    if(CitySet.Contains(acc.City__c)){
              Contact con = New Contact();
              con.LastName ='acc.Name'+'City';
              con.AccountId = acc.Id;
              conList.add(con);
          }
                }
                Insert conList ;
            }
            catch(Exception ex){
                system.debug('LineNo->'+ex.getLineNumber()+'msg->'+ex.getMessage());
            }
        }
    }
}

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

Praful_GuptaPraful_Gupta
You can achieve this with the help of Trigger and SOQL. Take the City name in a Set, do a Soql on City object to search City record with same name if Soql finds the result create a contact. 
Ajay K DubediAjay K Dubedi
Hi Sai,
Try the following trigger and its handler class it may helpful for you and it works in my Org :
Trigger:
trigger CheckCityContact on Contact (before insert) {
    if(trigger.isInsert && trigger.isBefore){
        CheckCityContactHandler.insertContact(trigger.new);
    }
}

TriggerHandler:
public class CheckCityContactHandler {
    public static void insertContact(List<Contact> conList){
        if(conList.size()>0){
            try{
                List<Account> accList=new List<Account>();
                List<City__c> cityList=new List<City__c>();
                cityList=[SELECT Id,Name FROM City__c LIMIT 10000];
                Boolean flag=false;
                for(Contact con:conList){
                    if(con.AccountId!=null){
                        accList=[SELECT Id,City__c    FROM Account WHERE Id =:con.AccountId LIMIT 1];
                    }
                    if(cityList.size()>0 && accList.size()>0){
                    for(City__c cty:cityList){
                        if(cty.Name==accList[0].City__c){
                            flag=true;
                        }
                    }
                }
                if(flag==false){
                    con.addError('Contact related Account city is not matched in City Object Name.');
                }
                }
            }
            catch(Exception ex){
                system.debug('LineNo->'+ex.getLineNumber()+'msg->'+ex.getMessage());
            }
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
 
U D NU D N
not working sir
Sai Bhaskar 16Sai Bhaskar 16
Hi Ajay Dubedi Sir,

    Not Working Sir.

Thanks & Regards
Sai Bhaskar 16
Sai Bhaskar 16Sai Bhaskar 16
Hi Praful_Gupta Sir,

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 ;
}
 
Ajay K DubediAjay K Dubedi
Hi Sai,
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 CheckCityAccount on Account (after insert) {
if(trigger.isInsert && trigger.isAfter){
        CheckCityAccountHandler.insertContact(trigger.new);
    }
}

Trigger-Handler:
public class CheckCityAccountHandler {
    public static void insertContact(List<Account> accList){
        if(accList.size()>0){
            try{
                List<Contact> conList=new List<Contact>();
                List<City__c> cityList=new List<City__c>();
                cityList=[SELECT Id,Name FROM City__c LIMIT 10000];
                Set<String> citySet=new Set<String>();
                for(City__c cty:cityList){
                    citySet.add(cty.Name);
                }
                for(Account acc:accList){
                    if(CitySet.Contains(acc.City__c)){
              Contact con = New Contact();
              con.LastName ='acc.Name'+'City';
              con.AccountId = acc.Id;
              conList.add(con);
          }
                }
                Insert conList ;
            }
            catch(Exception ex){
                system.debug('LineNo->'+ex.getLineNumber()+'msg->'+ex.getMessage());
            }
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer
Sai Bhaskar 16Sai Bhaskar 16
Hi Ajay Dubedi Sir,

    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