You need to sign in to do that
Don't have an account?

Hi All, Let me know what's wrong in this code. It says variable doesnot exsits
It says variable doesn't exists ac
trigger ConOnAccInsert on Account (After insert) {
List<Account> ListofaccsUpd = new List<Account>();
List<Contact> listOfcons = new List<Contact>();
Set<Id> accIds = new Set<ID>();
for (Account acc : trigger.new) {
Contact conObj = new contact(firstname = 'acc.name',lastname = 'John',AccountId = acc.Id);
accIds.add(conObj.AccountId);
system.debug('id'+accIds);
listOfcons.add(conObj);
}
if(listOfcons.size()>0) insert listOfcons;
Map<Id,Account> mapOfIDwithAcc = new Map<Id,Account>([Select id ,Name from Account where id =: accIds]);
List<Account> listOfAcc = new List<Account>();
if(listOfcons.size()>0){
for(contact c : listOfcons) {
if(mapOfIDwithAcc.containsKey(c.AccountId))
Account ac = new Account();
ac = mapOfIDwithAcc.get(c.AccountId);
ac.Client_Contact__c = c.Id;
ListofaccsUpd.add(ac);
}
if(ListofaccsUpd.size()>0) update ListofaccsUpd;
}
}
trigger ConOnAccInsert on Account (After insert) {
List<Account> ListofaccsUpd = new List<Account>();
List<Contact> listOfcons = new List<Contact>();
Set<Id> accIds = new Set<ID>();
for (Account acc : trigger.new) {
Contact conObj = new contact(firstname = 'acc.name',lastname = 'John',AccountId = acc.Id);
accIds.add(conObj.AccountId);
system.debug('id'+accIds);
listOfcons.add(conObj);
}
if(listOfcons.size()>0) insert listOfcons;
Map<Id,Account> mapOfIDwithAcc = new Map<Id,Account>([Select id ,Name from Account where id =: accIds]);
List<Account> listOfAcc = new List<Account>();
if(listOfcons.size()>0){
for(contact c : listOfcons) {
if(mapOfIDwithAcc.containsKey(c.AccountId))
Account ac = new Account();
ac = mapOfIDwithAcc.get(c.AccountId);
ac.Client_Contact__c = c.Id;
ListofaccsUpd.add(ac);
}
if(ListofaccsUpd.size()>0) update ListofaccsUpd;
}
}
Try Below Code Please Mark It As Best Asnwer If It Helps
Thank You!
Refresh The Dev Console Window And Paste The Code and then try It's Saving Sucessully on my side if still not please tell the error Please Mark It As Best Asnwer If It Helps
Thank You!