{"id":1408,"date":"2020-07-24T11:14:43","date_gmt":"2020-07-24T11:14:43","guid":{"rendered":"https:\/\/staging.txtbox.in\/docs\/developers\/?post_type=lsvr_kba&#038;p=1408"},"modified":"2020-07-24T11:14:43","modified_gmt":"2020-07-24T11:14:43","slug":"send-a-sms-message-from-apex","status":"publish","type":"lsvr_kba","link":"https:\/\/staging.txtbox.in\/docs\/developers\/knowledge-base\/send-a-sms-message-from-apex\/","title":{"rendered":"Send a SMS Message from Apex"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>Prerequisites<\/strong><\/h3>\n\n\n\n<p>The developer will need proficiency in:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Salesforce.com Object model&nbsp;<\/li><li>Apex programming<\/li><\/ul>\n\n\n\n<p>The APIs\u2019 can be worked on via this&nbsp;<a href=\"https:\/\/api.sms-magic.com\/doc\/\">link<\/a><\/p>\n\n\n\n<p>Apex is a strongly-typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Salesforce Lightning Platform server, together with calls to the API.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Object &amp; Fields Information<\/strong><\/h3>\n\n\n\n<p>There is a custom object in the SMSMagic Interact Managed package &nbsp;known as&nbsp;<strong>SMS History<\/strong>, &nbsp;and the corresponding API name is&nbsp;<strong>smagicinteract__smsMagic__c<\/strong>. This object stores SMS message data. Considering the need of complex customization for implementing various business workflows, &nbsp;we have provided a simple way to send SMS from Apex code.&nbsp;<\/p>\n\n\n\n<p>This table contains fields that must be populated to successfully send messages from Apex. Continue reading below to see how to send a SMS message from Apex code.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong>Name<\/strong><\/td><td><strong>Field API name<\/strong><\/td><td><strong>Purpose<\/strong><\/td><td><strong>Required<\/strong><\/td><\/tr><tr><td>SenderId<\/td><td>smagicinteract__SenderId__c<\/td><td>Phonenumber or business identity of your business<\/td><td>Yes<\/td><\/tr><tr><td>Mobile Number<\/td><td>smagicinteract__PhoneNumber__c<\/td><td>Phone number of Contact\/Lead whom you are sending message to.<\/td><td>Yes<\/td><\/tr><tr><td>Name<\/td><td>smagicinteract__Name__c<\/td><td>Name of person whom you are sending this message to.<\/td><td>No<\/td><\/tr><tr><td>SMSText<\/td><td>smagicinteract__SMSText__c<\/td><td>Content of message<\/td><td>Yes<\/td><\/tr><tr><td>Disable SMS on Trigger<\/td><td>smagicinteract__<br>disableSMSOnTrigger__c<\/td><td>This is used to control trigger. If set 1, trigger is deactivated, so message won\u2019t be sent out only record will be created. If set 0, message will be sent out. It should be 0 by default.<\/td><td>Yes<\/td><\/tr><tr><td>External Field<\/td><td>smagicinteract__external_field__c<\/td><td>This is indexed and unique field used as a reference to update delivery reports.<\/td><td>Yes<\/td><\/tr><tr><td>Object Type<\/td><td>smagicinteract__ObjectType__c<\/td><td>Identifier of the object from which message will be sent out<\/td><td>No<\/td><\/tr><tr><td>Direction<\/td><td>smagicinteract__Direction__c<\/td><td>With 1.49 onwards, this new field is used to indicate it\u2019s an &nbsp;outgoing or incoming message. Set its value as \u201cOUT\u201d for sending out message.<\/td><td>Yes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Send an SMS message from Apex code<\/strong><\/h3>\n\n\n\n<p>The developer would first create an instance of the&nbsp;<strong>SMS History<\/strong>&nbsp;object, populate all required fields, and then insert the instance of that object using a database insert. SMS-Magic provides a custom trigger that will (a) execute after insertion of the record and (b) send out messages to the&nbsp;<strong>Mobile Number<\/strong>. The trigger will also populate other fields in the&nbsp;<strong>SMS History<\/strong>&nbsp;object instance with default values.<\/p>\n\n\n\n<p>This sample code sends an SMS messages. Feel free to copy it and modify it according to your environment.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">List  smsObjectList = new List ();\nString senderId = 'smsMagic'; \/\/ Please replace the 'smsMagic' with your relevant sender ID.\nString templateText = 'test SMS by Screen Magic'; \/\/ you can fetch the template text by querying the record on smagicinteract__SMS_Template__c object\nsmagicinteract__smsMagic__c smsObject = new smagicinteract__smsMagic__c();\nsmsObject.smagicinteract__SenderId__c = senderId;\nsmsObject.smagicinteract__PhoneNumber__c = contact.MobilePhone;\nsmsObject.smagicinteract__Name__c = contact.Name; \/\/ records name\nsmsObject.smagicinteract__ObjectType__c = 'Contact'; \/\/ record type\nsmsObject.smagicinteract__disableSMSOnTrigger__c = 0; \/\/ this field either be 0 or 1, if you specify the value as 1 then sms will not get send but entry of sms will get create under SMS History object\nsmsObject.smagicinteract__external_field__c = smagicinteract.ApexAPI.generateUniqueKey();\nsmsObject.smagicinteract__SMSText__c = templateText;\nsmsObjectList.add(smsObject);\nDatabase.insert(smsObjectList, false);\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Troubleshooting<\/strong><\/h3>\n\n\n\n<p>If you encounter any problems, consider the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Ensure that your code is not invoked from a scheduled method of any other trigger.<\/li><li>A user on whose behalf this code is executed must have permission to use&nbsp;<strong>SMS History<\/strong>&nbsp;objects.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Prerequisites The developer will need proficiency in: Salesforce.com Object model&nbsp; Apex programming The APIs\u2019 can be worked on via this&nbsp;link Apex is a strongly-typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Salesforce Lightning Platform server, together with calls to the API. Object &amp; Fields Information There is [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"lsvr_kba_cat":[153],"lsvr_kba_tag":[],"class_list":["post-1408","lsvr_kba","type-lsvr_kba","status-publish","format-standard","hentry","lsvr_kba_cat-sms-magic-for-developers"],"_links":{"self":[{"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/lsvr_kba\/1408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/lsvr_kba"}],"about":[{"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/types\/lsvr_kba"}],"author":[{"embeddable":true,"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/comments?post=1408"}],"version-history":[{"count":1,"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/lsvr_kba\/1408\/revisions"}],"predecessor-version":[{"id":1409,"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/lsvr_kba\/1408\/revisions\/1409"}],"wp:attachment":[{"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/media?parent=1408"}],"wp:term":[{"taxonomy":"lsvr_kba_cat","embeddable":true,"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/lsvr_kba_cat?post=1408"},{"taxonomy":"lsvr_kba_tag","embeddable":true,"href":"https:\/\/staging.txtbox.in\/docs\/developers\/wp-json\/wp\/v2\/lsvr_kba_tag?post=1408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}