Showing posts with label Apex. Show all posts
Showing posts with label Apex. Show all posts

Tuesday, May 24, 2022

Salesforce Apex Code : Add or Remove ALL (multiple) field permissions of User Profile 快速完成加入或移除個別用戶配置的編輯權限

 Sometimes we may need to update ALL field permission of a user profile due to different purpose, like audit requirement or ISO requirement. 

It can be very time-consuming to do it in UI level . For example, auditor asked to block ALL field "edit" permission of all "Clerk" profile user. You have to go to every object, and untick each field's security checkbox . There is a better way to handle this batch permission update , using "Apex program"


Code below demonstrate how to remove Clerk user's field edit permission right of multiple objects .

   

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// Multiple object names into the list
List<String> validTypeList = new List<String>{'Account','Case','Contact','CustomObj__c'}; 
//ClerkUser can be replaced by any profile name
//Use PermissionsRead if "Read" permission is needed
List<FieldPermissions> fpList = [SELECT SobjectType, Field, PermissionsEdit, Parent.ProfileId 
FROM FieldPermissions 
WHERE SobjectType IN :validTypeList AND PermissionsEdit=TRUE
AND ParentId IN (SELECT Id FROM PermissionSet WHERE PermissionSet.Profile.Name = 'ClerkUser')];
if(!fpList.isEmpty()){
    List<FieldPermissions> updatefpList = new List<FieldPermissions>();
    for(FieldPermissions fp:fpList){
        fp.PermissionsEdit = false;
        updatefpList.add(fp);
    }
    update updatefpList;

   //Print Total field edit permissions REMOVED
    system.debug('UpdatefpList Edit Permission - ' + updatefpList.size()); 
}

Wednesday, January 12, 2022

Salesforce Apex code to detect Chinese or Japanese characters 在字串中尋找漢字 包含中文字 或 日文字

 Sometimes we have to identify the language character inside string . Code below shows how to use Apex code to identify whether a specific string contains "Hanzi" or not . 

#Please be reminded that "Hanzi" NOT just detect Chinese characters , it may also include Japanese Hanzi.  

1
2
3
4
5
6
//true if contain chinese or japanese hanzi
public static Boolean containsChineseCharacters(String InputString){
    Pattern p = Pattern.compile('\\p{IsHan}');
    Matcher m = p.matcher( InputString );
    return m.find();
}

Monday, May 31, 2021

Salesforce Permission : Manual Share Record using Apex Bulk API 分享單筆資料代碼

 

 
There are different way of sharing a "private" object record in sfdc.
Include using inherit role hierarchy ,sharing rule , or pressing custom button "Sharing"
on record detail page in user interface. 

Codes below try to use apex API to do the same thing as the "Sharing" button.
#Salesforce分享單筆資料代碼
 

    // Create new sharing object for the custom object named MyCustomObject.
      MyCustomObject__Share myCustomObjectShr  = new MyCustomObject__Share();
   
      // Set the ID of record being shared.
      myCustomObjectShr.ParentId = recordId;
        
      // Set the ID of user or group being granted access.
      myCustomObjectShr.UserOrGroupId = userOrGroupId;
        
      // Set the access level.
      myCustomObjectShr.AccessLevel = 'Read';
        
      // Set rowCause to 'manual' for manual sharing.
      // You may change to other row cause if you have created other sharing reason.
      myCustomObjectShr.RowCause = Schema.myCustomObject__Share.RowCause.Manual;
        
      // Insert the sharing record and capture the save result. 
      Database.SaveResult sr = Database.insert(myCustomObjectShr,false);
 
      // Process the save results.
      if(sr.isSuccess()){
         // Indicates success
         System.debug(LoggingLevel.INFO, 'Done');
      }
	else{
          // read first save result error.
         Database.Error err = sr.getErrors()[0];
         System.debug(LoggingLevel.ERROR, 'Error code :' + err.getStatusCode() );
      }

Migrating from Renpy to Godot

 Due to the limitation of renpy in rendering dynamic screen ,  due the the black border it gives in different UI scale resolution , finally ...