Wednesday, June 15, 2022

PowerShell Script : Convert UTF-8 text file into UTF-16 Unicode encoding file , salesforce dataloader export csv to UTF-16 Unicode SFDC 原生編碼字元轉換補遺

The native dataloader from Salesforce only export .csv file in UTF-8 encoding . And it is asked to exchange information with Therefore™ information management application which uses UTF-16 encoding data .

The pros and cons of UTF-8 and UTF-16 (Unicode) encoding can be found in here.
Storage and indexing speed etc. matters. 

The most easy way to do this is using powershell script to encode the .csv from Salesforce dataloader export to Unicode in mainly one line .PS script. Schedule this script into window task scheduler , done.

  

Get-content -encoding UTF8 $filename | Set-Content -encoding Unicode "$new_filename"

Script below shows how to convert multiple UTF-8 files in folder into UTF-16 files with error log tracking. 

Both UTF-8 and UTF-16 files will be available in the folder after conversion.
UTF-8 abc.csv will be cloned , and a UTF-16 abc_utf16.csv will be created in the same folder.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#Continue conversion even if one file failed , just log down the error.
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
#-path of the log file
Start-Transcript -path "C:\ABCProject\log\utfLog.txt" -append
try {
#double bracket for path with space
#get all csv files inside that folder
get-childitem "C:\ABCProject\write\*.csv" | 

  foreach-object { 
    $name = $_.FullName;
    write-host "The file name" $name;
    $fname = $name.replace(".csv","_utf16.csv");
    write-host "The new file" $fname;
    get-content -encoding UTF8 $name  | Set-Content  -encoding Unicode  "$fname" 
}
 Stop-Transcript
 exit
}
catch {
  #log -message "Get-WmiObject cmdlet failed" -type "Error"
  write-host "Error found " $_.Exception.Message.ToString() -type "Error"

}

Tuesday, June 14, 2022

Salesforce Administration : 2022 Spring email about "Salesforce email verification" from support@salesforce.com SFDC Salesforce 電郵認證郵件是釣魚嗎?

Recently some of the users receiving email from support@salesforce.com with subject "Salesforce email verification". A link is inside that email , users worry it may be phishing email ,so they come to check .

Luckily, it is not a phishing email . From official announcementprior to the Summer '22 release , users may be asked to verify their email .


To get the list of user who may get the mentioned verification email ,s
ystem administrators may use SOQL below :

SELECT Name from User where id IN (SELECT UserId FROM TwoFactorMethodsInfo where HasUserVerifiedEmailAddress = false)


Error message
"sObject is not supported" may be shown if your account do NOT have Manage Multi-Factor Authentication in API permission.

To enable this permission

1.    Go to Setup -> Manage Users -> Permission Sets , click New to create a new permission set.

2.    Select "System Permissions" . 


3.    Click "Edit" , scroll down to items below ,and checked the box. Then save.

        -    Manage Multi-Factor Authentication in API
       -    Manage Multi-Factor Authentication in User Interface




4.    Add the permission set to a user account you use to run the SOQL by using "Assign" button.

5.    Click "Done" to save the setting.

6.    Run the SOQL again.





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, February 16, 2022

Window command : MS-DOS Doskey native window utility check command prompt history . Make record of cmd command easily

 It can be trouble when we runs a lots of command in command prompt . It made so many changes in short time ,which may need to be recovered later.

But it is not easy to remember every command you types , with native utility , we may retrieve command history with line line below :

Just open your command prompt and type 

doskey /history

#For keeping record in text log file
doskey /history > myCmd20220216.log 

Press "Enter"  , Finished ! 

Finally, very important thing is that the history is session history .
It only keeps command history BEFORE you CLOSE the command prompt. 

Thursday, January 13, 2022

Salesforce concept : Validation rule does not fired after approval process field update

Validation rule will not be fired by Workflow rule or Approval Process field update based on document.

Both API level and UI level change of record honored validation rule , but field update is an exception.

Which means , if you have an validation rule block field_ABC value to be "ABC", if you used a field update to do this, the value of  field_ABC can be set as "ABC".



Official IdeaExchange 1

"because updates to records based on workflow rules doesn't trigger validation rules,workflow rules can invalidate previously valid fields "


Official IdeaExchange 2




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();
}

Tuesday, January 11, 2022

Copy files based on file list document 按照文件清單複製檔案 抄送

 Consider of a scenario, partner's SFTP server is down for an hour . A lots of files failed to upload to them.  After comparing the file list of two company, a missing file list text file has been created.
Maybe 100 out of 1000 files are missing.

Hence, you are going to resend those missing files based on the file list.
Batch script below help to do the job in an easy way.

You may just copy all missing files to a new folder, then resend.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
@ECHO ON


SET FileList=C:\Users\XXX\MissingFiles.txt
::The folder that contains the files
SET Source=X:\AllFiles
::The folder that the files should be copied to
SET Destination=Y:\ToBeResend

 

FOR /F "USEBACKQ TOKENS=*" %%F IN ("%FileList%") DO XCOPY /F /Y "%Source%\%%~F" "%Destination%\"

GOTO :EOF

 

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 ...