+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

SOQL Query for old Team Member ?

Hi,

I am sharing Opportunity record from Quote team members. In This scenario i would like to add one more functionality as mentioned below.

1. As of now this functionality is working for add a new team members.

2. But if already this record has shared to User A, Again i am sharing to same User A. At that time system should update a Old record with latest update (Without Creating a new record).

3. I am little confused to launch this functionality.

Can you please check my code and let me know where i need to make a changes to achieve metioned functionality.


Code For Review
 
public with sharing class GD_CreateQuoteTeam { 
    @AuraEnabled
    public static void saveQuoteTeam(List<GD_Quote_Team__c> teamList, string quoteId){
        try{
            list<GD_Quote_Team__c> qtTeamList = new list<GD_Quote_Team__c>();
            list<opportunityShare> readShareList = new list<opportunityShare>();
            list<opportunityShare> editShareList = new list<opportunityShare>();    
            
            GD_Quote__c qt = [select id,GD_Opportunity__c from GD_Quote__c where id =: quoteId];
            id oppId = qt.GD_Opportunity__c;
            system.debug('Opportunity Id '+ oppId);
            for(GD_Quote_Team__c team : teamList){
                team.GD_Quotes__c = quoteId;
                qtTeamList.add(team);
            }
            if(qtTeamList.size()>0){Insert qtTeamList;}
            
            for(gd_quote_Team__c qTeam : qtTeamList){
                if(qTeam.GD_Quote_Access__c == 'Read Only'){
                    system.debug('Enter Into the Read only = ' + qTeam.GD_Quote_Access__c);
                    opportunityShare shareRecord = new opportunityShare();
                    shareRecord.OpportunityAccessLevel = 'Read';
                    shareRecord.UserOrGroupId = qTeam.GD_user__c;
                    shareRecord.OpportunityId = oppId;
                    readShareList.add(shareRecord);
                }    
            }
            if(readShareList.size()>0){insert readShareList;system.debug('Read Access Provided');}        
            
            for(gd_quote_Team__c qTeam : qtTeamList){  
                if(qTeam.GD_Quote_Access__c == 'Read/write'){
                    system.debug('Access |' + qTeam.GD_Quote_Access__c);
                    opportunityShare shareRecord = new opportunityShare();
                    shareRecord.OpportunityAccessLevel = 'edit';
                    shareRecord.UserOrGroupId = qTeam.GD_user__c;
                    shareRecord.OpportunityId = oppId;
                    editShareList.add(shareRecord);
                } 
            }
            if(editShareList.size()>0){insert editShareList;system.debug('Edit Access Provided');} 
        }catch(Exception e){system.debug('Exception -' + e);}
        
    }
    
}


Thanks in advance,
Soundar Rajan P.
 
Best Answer chosen by Soundar Raj Ponpandi
David Zhu 🔥David Zhu 🔥
To handle that scenario, you need to add deletion in this section
            for(gd_quote_Team__c qTeam : qtTeamList){  
                if(qTeam.GD_Quote_Access__c == 'Read/write'){
                    if (!sharingMap.ContainsKey(qTeam.GD_user__c) || (sharingMap.ContainsKey(qTeam.GD_user__c) && sharingMap.get(qTeam.GD_User__c) == 'Read'))
                    {
                        system.debug('Access |' + qTeam.GD_Quote_Access__c);
                        opportunityShare shareRecord = new opportunityShare();
                        shareRecord.OpportunityAccessLevel = 'edit';
                        shareRecord.UserOrGroupId = qTeam.GD_user__c;
                        shareRecord.OpportunityId = oppId;
                        editShareList.add(shareRecord);
                    }
                } 
            }

All Answers

David Zhu 🔥David Zhu 🔥
You may refer to the code below to prevent the duplicates.
public with sharing class GD_CreateQuoteTeam { 
    @AuraEnabled
    public static void saveQuoteTeam(List<GD_Quote_Team__c> teamList, string quoteId){
        try{
            list<GD_Quote_Team__c> qtTeamList = new list<GD_Quote_Team__c>();
            list<opportunityShare> readShareList = new list<opportunityShare>();
            list<opportunityShare> editShareList = new list<opportunityShare>();    
            
            GD_Quote__c qt = [select id,GD_Opportunity__c from GD_Quote__c where id =: quoteId];
            id oppId = qt.GD_Opportunity__c;
            system.debug('Opportunity Id '+ oppId);
            for(GD_Quote_Team__c team : teamList){
                team.GD_Quotes__c = quoteId;
                qtTeamList.add(team);
            }
            if(qtTeamList.size()>0){Insert qtTeamList;}
            
            List<opportunityShare> existingShareRecords = [SELECT UserOrGroupId,OpportunityAccessLevel from shareRecord where OpportunityId = : oppId ];

            Map<Id,String> sharingMap = new Map<Id,String>();
            for (opportunityShare oppShareRecord: existingShareRecords)
            {
                if (!sharingMap.ContainsKey(oppShareRecord.UserOrGroupId))
                {
                    sharingMap.put(oppShareRecord.UserOrGroupId,oppShareRecord.OpportunityAccessLevel);
                }
                else
                {
                    string accessLevel = sharingMap.get(oppShareRecord.UserOrGroupId);
                    if (accessLevel == 'Read' && oppShareRecord.OpportunityAccessLevel == 'Edit')
                    {
                        sharingMap.put(oppShareRecord.UserOrGroupId,oppShareRecord.OpportunityAccessLevel);
                    }
                }
            }


            for(gd_quote_Team__c qTeam : qtTeamList){
                if(qTeam.GD_Quote_Access__c == 'Read Only'){
                    if (!sharingMap.ContainsKey(qTeam.GD_user__c))
                    {

                        system.debug('Enter Into the Read only = ' + qTeam.GD_Quote_Access__c);
                        opportunityShare shareRecord = new opportunityShare();
                        shareRecord.OpportunityAccessLevel = 'Read';
                        shareRecord.UserOrGroupId = qTeam.GD_user__c;
                        shareRecord.OpportunityId = oppId;
                        readShareList.add(shareRecord);
                    }
                }    
            }
            if(readShareList.size()>0){insert readShareList;system.debug('Read Access Provided');}        


            for(gd_quote_Team__c qTeam : qtTeamList){  
                if(qTeam.GD_Quote_Access__c == 'Read/write'){
                    if (!sharingMap.ContainsKey(qTeam.GD_user__c) || (sharingMap.ContainsKey(qTeam.GD_user__c) && sharingMap.get(qTeam.GD_User__c) == 'Read'))
                    {

                        system.debug('Access |' + qTeam.GD_Quote_Access__c);
                        opportunityShare shareRecord = new opportunityShare();
                        shareRecord.OpportunityAccessLevel = 'edit';
                        shareRecord.UserOrGroupId = qTeam.GD_user__c;
                        shareRecord.OpportunityId = oppId;
                        editShareList.add(shareRecord);
                    }
                } 
            }
            if(editShareList.size()>0){insert editShareList;system.debug('Edit Access Provided');} 
        }catch(Exception e){system.debug('Exception -' + e);}
        
    }
    
}
Soundar Raj PonpandiSoundar Raj Ponpandi
Hi David Zhu,

Hearty Thanks for your quick response.

Record sharing is perfect, at the same time and duplicate record also created.

Create Team
 
Here list of record inserted before checking the old and new value

Can you please help me here.

if(qtTeamList.size()>0){Insert qtTeamList;}
            
  List<opportunityShare> existingShareRecords = [SELECT UserOrGroupId,OpportunityAccessLevel from shareRecord where OpportunityId = : oppId ];


​​

Regards,
Soundar.​​​​​​​
David Zhu 🔥David Zhu 🔥
List<opportunityShare> existingShareRecords = [SELECT UserOrGroupId,OpportunityAccessLevel from opportunityShare where OpportunityId = : oppId ];
Soundar Raj PonpandiSoundar Raj Ponpandi
Hi David Zhu,

This sharing functionality is working fine.

1. Already i have shared opportunity  record to "User A - ReadOnly".

2. Again, When i am sharing for "Read / write" new record is creatd for "User A - Read / Write".

3. Now it's 2 records created for  same user "User A", I need to avoid this functionality only. Instead for creating we need to update the record if it's same User.

Regards,
Soundar.
David Zhu 🔥David Zhu 🔥
To handle that scenario, you need to add deletion in this section
            for(gd_quote_Team__c qTeam : qtTeamList){  
                if(qTeam.GD_Quote_Access__c == 'Read/write'){
                    if (!sharingMap.ContainsKey(qTeam.GD_user__c) || (sharingMap.ContainsKey(qTeam.GD_user__c) && sharingMap.get(qTeam.GD_User__c) == 'Read'))
                    {
                        system.debug('Access |' + qTeam.GD_Quote_Access__c);
                        opportunityShare shareRecord = new opportunityShare();
                        shareRecord.OpportunityAccessLevel = 'edit';
                        shareRecord.UserOrGroupId = qTeam.GD_user__c;
                        shareRecord.OpportunityId = oppId;
                        editShareList.add(shareRecord);
                    }
                } 
            }
This was selected as the best answer
Soundar Raj PonpandiSoundar Raj Ponpandi
Thanks David,

It's Working fine.

Regards,
Soundar.