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
MktdevMktdev 

creating one map using two maps based on keyset

Hi,

 

I have two map one is Contact Account and another is Contact User .I want to create another map by comparing these two and create a new map for Account User.

 

Please help.

 

Regards,

Mktg

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Final chance 

 

List<AccountShare> lsMultipleAccountShare = new List<AccountShare>(); 
        for(Object__c assAcc : lsObject)
        {
            if(userAccountMap.containsKey(assAcc.Account__c))
            {	
            Id userId = userAccountMap.get(assAcc.Account__c);
            AccountShare accShare = new AccountShare();  
            accShare.AccountId=assAcc.Account__c;
            accShare.UserOrGroupId=userId;
            lsMultipleAccountShare.add(accShare);
            }
            
        }
         if(!lsMultipleAccountShare.isEmpty()){
            Database.SaveResult[] accShareInsertResult = Database.insert(lsMultipleAccountShare,false);            
            }

 If it also gives error please provide comple class.

All Answers

Shashikant SharmaShashikant Sharma

I think you want to create a MAP which will give you user for Account

 

try this

Map<Account , User> mapAccountUser = new Map<Account , User>();
Map<ContactId, User> mapContactUser = new Map<Contact, User>();
Map<ContactId , Account> mapContactAccount = new Map<Contact, Account>();

for(ID conID  : mapContactAccount.KeySet())
{
if(mapContactUser.containsKey(conID))
{
 mapAccountUser.put(mapContactAccount.get(ConID) ,mapContactUser.get(conID));
}
}

 

let me know if any issues in it.

MktdevMktdev

Thanks Shashikant !

 

Sorry ! for this dump question!

 

Since I have to use this new map to enter in account sharing table.So I am try:

 

List<AccountShare> lsMultipleAccountShare = new List<AccountShare>();

For(Object__c obj1:listofobject)

{

 

}

will map be used?

 

Thanks in advance!

Shashikant SharmaShashikant Sharma

I don't think AccountShare give you directly User for this Account, I think in case you need a MAP

MktdevMktdev

 

 

Yes, I am using the same map which you have helped me to create.But using this I m getting an error

 

Loop must iterate over a collection type: Id at line

 

List<AccountShare> lsMultipleAccountShare = new List<AccountShare>(); 
        for(Object__c assAcc : lsObject)
        {
			
            for(Id userId : userAccountMap.get(assAcc.Account__c))
            {
            AccountShare accShare = new AccountShare();  
            accShare.AccountId=assAcc.Account__c;
            accShare.UserOrGroupId=userId;
            lsMultipleAccountShare.add(accShare);
            }
        }
         if(!lsMultipleAccountShare.isEmpty()){
            Database.SaveResult[] accShareInsertResult = Database.insert(lsMultipleAccountShare,false);            
            }  

 

Shashikant SharmaShashikant Sharma

Just try this 

 

List<AccountShare> lsMultipleAccountShare = new List<AccountShare>(); 
        for(Object__c assAcc : lsObject)
        {
			
            for(User userObj : userAccountMap.get(assAcc.Account__c))
            {
            Id userId = userObj.id;
            AccountShare accShare = new AccountShare();  
            accShare.AccountId=assAcc.Account__c;
            accShare.UserOrGroupId=userId;
            lsMultipleAccountShare.add(accShare);
            }
        }
         if(!lsMultipleAccountShare.isEmpty()){
            Database.SaveResult[] accShareInsertResult = Database.insert(lsMultipleAccountShare,false);            
            }

 

MktdevMktdev

Thanks!

 

Still getting the same error.

 

Thanks,

Mktg

Shashikant SharmaShashikant Sharma

Ok now i got the issue try this

 

List<AccountShare> lsMultipleAccountShare = new List<AccountShare>(); 
        for(Object__c assAcc : lsObject)
        {
            if(userAccountMap.containsKey(assAcc.Account__c))
            {	
            User userObj = userAccountMap.get(assAcc.Account__c);
            Id userId = userObj.id;
            AccountShare accShare = new AccountShare();  
            accShare.AccountId=assAcc.Account__c;
            accShare.UserOrGroupId=userId;
            lsMultipleAccountShare.add(accShare);
            }
            
        }
         if(!lsMultipleAccountShare.isEmpty()){
            Database.SaveResult[] accShareInsertResult = Database.insert(lsMultipleAccountShare,false);            
            }

 Now this code expect that your one account will only have one user, I think you have multiple user's on multiple contacts case. If so we need to make change in map. let me know if it works for you or you need change in MAP.

MktdevMktdev

I have a requirment that a contact can be part of multiple accounts and this relationship is stored in Object__c object with AccountId and ContactID,

 

Therefore what I am doing :

Map:

Account and contact

Contact and User

Account User (thanks to you)

 

Currently what you just provided it says "Illegal assignment from Id to SOBJECT:User at ..

 

Regards,

Mktg

MktdevMktdev

Oh Sorry!

 

My all map is of <Id,Id>

 

this was accepted:

 

 Id userId=userAccountMap.get(assAcc.Account__c);

 

Regards,

Mktg

Shashikant SharmaShashikant Sharma

Final chance 

 

List<AccountShare> lsMultipleAccountShare = new List<AccountShare>(); 
        for(Object__c assAcc : lsObject)
        {
            if(userAccountMap.containsKey(assAcc.Account__c))
            {	
            Id userId = userAccountMap.get(assAcc.Account__c);
            AccountShare accShare = new AccountShare();  
            accShare.AccountId=assAcc.Account__c;
            accShare.UserOrGroupId=userId;
            lsMultipleAccountShare.add(accShare);
            }
            
        }
         if(!lsMultipleAccountShare.isEmpty()){
            Database.SaveResult[] accShareInsertResult = Database.insert(lsMultipleAccountShare,false);            
            }

 If it also gives error please provide comple class.

This was selected as the best answer
MktdevMktdev

Many Thanks!

 

Code is working fine but sharing is not working .once done I will confirm you.

 

Regards,

Mktg