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
DbjensenDbjensen 

How to assign Ids from SQOL

Hello - I need help adding Account Ids to Contacts. I tried using a map but I don't have any matching keySet that will return the Id. Can I add the Ids to a list and then assign the Id from the list to a updateCt.AccountId?  If so, how can I do this without writing a different class?
 
//Query newly created Account
       List <Account> insertedAcct = [SELECT Id FROM Account WHERE Id =:insertAccounts];

//Query contact that needs the new Account Id 
        List <Contact> indClientIdList = [SELECT Id, AccountId FROM Contact WHERE Id = :clientIdList];
        
        if(indClientIdList.size() > 0){
            for(Contact updateCT : indClientIdList){
                updateCt.AccountId =   NEW ACCOUNT Id HERE );
            }
        }



 
Best Answer chosen by Dbjensen
Agustin BAgustin B
Hi, you could have a unique field to know how to connect which contact with which account.
For example if you have the same phone as the account in the contact you could do something like this.
Map<String,Id> accountIdMap = new Map<String,Id>();
//Query newly created Account
       List <Account> insertedAcct = [SELECT Id,Phone FROM Account WHERE Id =:insertAccounts];
for(Account a : insertedAcct ){
                accountIdMap.put(a.Phone,a.Id);
            }
//Query contact that needs the new Account Id 
        List <Contact> indClientIdList = [SELECT Id,Phone, AccountId FROM Contact WHERE Id = :clientIdList];
        
        if(indClientIdList.size() > 0){
            for(Contact updateCT : indClientIdList){
                updateCt.AccountId =   accountIdMap.get(updateCT.Phone);
            }
        }


Try using something in common, you need a way to know which contact goes with which account.

If it helps please like and if it solves your issue please mark as correct as it may help others.
good luck!

All Answers

Agustin BAgustin B
Hi, you could have a unique field to know how to connect which contact with which account.
For example if you have the same phone as the account in the contact you could do something like this.
Map<String,Id> accountIdMap = new Map<String,Id>();
//Query newly created Account
       List <Account> insertedAcct = [SELECT Id,Phone FROM Account WHERE Id =:insertAccounts];
for(Account a : insertedAcct ){
                accountIdMap.put(a.Phone,a.Id);
            }
//Query contact that needs the new Account Id 
        List <Contact> indClientIdList = [SELECT Id,Phone, AccountId FROM Contact WHERE Id = :clientIdList];
        
        if(indClientIdList.size() > 0){
            for(Contact updateCT : indClientIdList){
                updateCt.AccountId =   accountIdMap.get(updateCT.Phone);
            }
        }


Try using something in common, you need a way to know which contact goes with which account.

If it helps please like and if it solves your issue please mark as correct as it may help others.
good luck!
This was selected as the best answer
Jitendra Singh ShahiJitendra Singh Shahi

Hi,

I want to add one more thing in the above solution, which is the null pointer exception. There is a possibility that when you execute this accountIdMap.get(updateCT.Phone) maybe that phone number of contact not in the map key and you are trying to get its value, which results in a null pointer exception.so you have to handle it here: (I hope it helps you.)

if(indClientIdList.size() > 0){
    for(Contact updateCT : indClientIdList){
        if(accountIdMap.containsKey(updateCT.Phone)){
            updateCt.AccountId =   accountIdMap.get(updateCT.Phone);
        }
    }
}
DbjensenDbjensen
Hi Augustin - Thanks so much for the help. I'll look for some form of matching value. 

Thanks for the info, Jitendra. I appreciate the help.