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
SFDC pvSFDC pv 

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;    
    }  
CharuDuttCharuDutt
Hii SFDC
Try Below Code
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]);
    
    if(listOfcons.size()>0){
        for(contact c : listOfcons) {
            if(mapOfIDwithAcc.containsKey(c.AccountId)){
                Account ac = mapOfIDwithAcc.get(c.AccountId);
                 ac.Client_Contact__c = c.Id;
                ListofaccsUpd.add(ac);
            }
        }
        if(ListofaccsUpd.size()>0){
            update ListofaccsUpd; 
        }      
    }  
}
Please Mark It As Best Asnwer If It Helps
Thank You!
SFDC pvSFDC pv
Tried that as well doesn’t work
CharuDuttCharuDutt
HIi SFDC 
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
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]);
    
    if(listOfcons.size()>0){
        for(contact c : listOfcons) {
            if(mapOfIDwithAcc.containsKey(c.AccountId)){
                Account ac = mapOfIDwithAcc.get(c.AccountId);
                 ac.Client_Contact__c = c.Id;
                ListofaccsUpd.add(ac);
            }
        }
        if(ListofaccsUpd.size()>0){
            update ListofaccsUpd; 
        }      
    }  
}
Please Mark It As Best Asnwer If It Helps
Thank You!