Create Messaging User for WhatsApp Channel in Salesforce

If you are using Digital Engagement also called Messaging of Salesforce, you may come up with a requirement to create messaging user record automatically. Here are key fields that we need to provide while creating the messaging user. Given example is to be used in Apex but same can be used in flow:

String name = 'ayub';
String mobilenumber = '+919100449667';
MessagingEndUser me = new MessagingEndUser();
me.MessageType = 'WhatsApp';
me.Name = name;
me.MessagingPlatformKey = 'whatsapp:'+mobilenumber;
me.MessagingConsentStatus = 'DoublyOptedIn';
me.MessagingChannelId = '0Mj5200000000RM';
insert me;

For more detail: https://help.salesforce.com/s/articleView?id=sf.messaging_import_user_data.htm&type=5