• HqSeO
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hello,

I would to know the community opinion if there is any DML operation occurring within the for loop:
FOR LOOP:

//Build the share for each project and each member
for(Id projectId :membersByProjectIds.keySet())
{
    if(Schema.Project__c.sObjectType != projectId.getSobjectType())
        continue;

    //Get the list of members for this project
    Set<Id> memberIds = membersByProjectIds.get(projectId);

    if(memberIds != null && memberIds.size() > 0)
    {
        //Iterate the list of members
        for(Id memberId :memberIds)
        {
            //Test for ID injection
            if(Schema.User.sObjectType != memberId.getSobjectType() && Schema.Group.sObjectType != memberId.getSobjectType())
                continue;

            //Create a share permission record using manager
            Sobject sob = SharePermissionManager.createShareRecord(ConstantHelper.PROJECT_SHARE_OBJECT_NAME, projectId, memberId, permissionAccess, ConstantHelper.PROJECT_SHARE_REASON);

            //Control the result
            if(sob != null)
            {
                //Add the record to the result list
                projectShareList.put(i, sob);
                i++;
            }
        }
    }
}


------------------------------------------------------------------------------------------------------------
UNDERLYING METHOD:

public static SObject createShareRecord(String objectName, Id parentId, Id userId, String accessLevel, String rowCause)
{
    //Control parameter
    if(String.isBlank(objectName) || parentId == null || userId == null || String.isBlank(rowCause) || String.isBlank(accessLevel))
        return null;
    
    //Test the accesslevel
    if(!accesslevel.equals(ConstantHelper.SHARE_ACCESS_LEVEL_READ) && !accesslevel.equals(ConstantHelper.SHARE_ACCESS_LEVEL_EDIT))
        return null;

    Schema.SObjectType shareType = Schema.getGlobalDescribe().get(objectName);

    //Control the return type
    if(shareType == null)
        return null;

    //Instantiate the Share record
    SObject shareRecord = shareType.newSObject();

    //Set each field of the Share record
    shareRecord.put(ConstantHelper.SHARE_PARENTID_COLUMN, parentId);
    shareRecord.put(ConstantHelper.SHARE_USERORGROUPID, userId);

    //Set the accessLevel
    shareRecord.put(ConstantHelper.SHARE_ACCESSLEVEL_COLUMN, accessLevel);
    shareRecord.put(ConstantHelper.SHARE_ROWCAUSE_COLUMN, rowCause);
    
    //Return the record
    return shareRecord;
}

Thanks devs :)
Sylvain​
  • August 22, 2017
  • Like
  • 0
Hello,

I would to know the community opinion if there is any DML operation occurring within the for loop:
FOR LOOP:

//Build the share for each project and each member
for(Id projectId :membersByProjectIds.keySet())
{
    if(Schema.Project__c.sObjectType != projectId.getSobjectType())
        continue;

    //Get the list of members for this project
    Set<Id> memberIds = membersByProjectIds.get(projectId);

    if(memberIds != null && memberIds.size() > 0)
    {
        //Iterate the list of members
        for(Id memberId :memberIds)
        {
            //Test for ID injection
            if(Schema.User.sObjectType != memberId.getSobjectType() && Schema.Group.sObjectType != memberId.getSobjectType())
                continue;

            //Create a share permission record using manager
            Sobject sob = SharePermissionManager.createShareRecord(ConstantHelper.PROJECT_SHARE_OBJECT_NAME, projectId, memberId, permissionAccess, ConstantHelper.PROJECT_SHARE_REASON);

            //Control the result
            if(sob != null)
            {
                //Add the record to the result list
                projectShareList.put(i, sob);
                i++;
            }
        }
    }
}


------------------------------------------------------------------------------------------------------------
UNDERLYING METHOD:

public static SObject createShareRecord(String objectName, Id parentId, Id userId, String accessLevel, String rowCause)
{
    //Control parameter
    if(String.isBlank(objectName) || parentId == null || userId == null || String.isBlank(rowCause) || String.isBlank(accessLevel))
        return null;
    
    //Test the accesslevel
    if(!accesslevel.equals(ConstantHelper.SHARE_ACCESS_LEVEL_READ) && !accesslevel.equals(ConstantHelper.SHARE_ACCESS_LEVEL_EDIT))
        return null;

    Schema.SObjectType shareType = Schema.getGlobalDescribe().get(objectName);

    //Control the return type
    if(shareType == null)
        return null;

    //Instantiate the Share record
    SObject shareRecord = shareType.newSObject();

    //Set each field of the Share record
    shareRecord.put(ConstantHelper.SHARE_PARENTID_COLUMN, parentId);
    shareRecord.put(ConstantHelper.SHARE_USERORGROUPID, userId);

    //Set the accessLevel
    shareRecord.put(ConstantHelper.SHARE_ACCESSLEVEL_COLUMN, accessLevel);
    shareRecord.put(ConstantHelper.SHARE_ROWCAUSE_COLUMN, rowCause);
    
    //Return the record
    return shareRecord;
}

Thanks devs :)
Sylvain​
  • August 22, 2017
  • Like
  • 0