function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
devrequirementdevrequirement 

Webservice to email to my personal email address

All,

I need to develop a webservice, which will be called in a trigger after some field update. This webservice should send the email to my personal email address. Could someone guide me with the steps required to complete this mini-project?

 

Thanks!!!

sfdcfoxsfdcfox

Couldn't you just create a workflow rule to send an email to your address...?

devrequirementdevrequirement

Thanks for your reply again SfdcFox.

 

Actually the requirement I have is: when a field, which is a picklist field, in SFDC is changed to something else than an existing value, for example a current option in the stage of Opportunity is changed from Awareness to Prospect, then a trigger is fired in Salesforce which calls an external webservice and SFDC also needs to send an email informing that the call of the external webservice worked.

 

Is there any code snippets I can refer to understand and begin to deliver the requirement.

 

Our goal is to call an external service as the change occurs and to send an email confirmation that  informs that the goal was achieved without an error. How should I approach this problem? Can I do this without using trigger at all?

 

Thanks!!!

devrequirementdevrequirement

So far what I am thinking is, I have an after update trigger ready which will execute the callout future method as well as send the Opportunity Id and updated stage name to the external webservice. I am hoping I am heading into right direction

 

Now I have couple of questions. To test if the opportunity Id and Updated stage name were actually passed, is there any  test endpoint available to be used in req.setBody = ...... so that I can see it for myself that the id and updated stage name were passed properly.

And also, how do I embed an email within it which will notify an inbox saying the job of sending the infos to callout was done?

 

Will really appreciate any insights!!

Thanks thanks!!

Jerun JoseJerun Jose
You can use SOAP UI tool to generate a mock webservice to test your callouts. You can also use the Force.com IDE with a developer org to see the actual request and respose XMLs that are sent as part of the callout.

Once you get the response of the request messages you sent in the future calls, you can then check the response to see if you got a successful status and based on this you can send emails using apex. Use the messaging.singleEmailMessage class to help you out.
sfdcfoxsfdcfox

Keep in mind that callouts are not supported in triggers at this time, since a callout could cause a potentially long row-level lock. Instead, you have to use an asynchronous callout from your trigger. The asynchronous code can report the error in a custom field on the record, which will become available to the user after the callout has completed. bob_buzzard posted in one forum reply to note that you can perform callouts in a Visualforce page, so you might be able to use that method instead. Assuming you use the asynchronous call from a trigger, you can have that function send an email as part of its logic.

devrequirementdevrequirement
Thanks guys for the quick response. I do not have to send the email anymore. What I need to do is, when the picklist; upsellOpportunity is changed, a trigger gets fired and does a callout and sends this notification to another CRM, email is not required to send. Below is what I have acheived so far, but do not really know if it works or not as I do have any endpoint yet. Please let me know if the callout would work? I have checked on the trigger and it seems to be working fine, but am unsure on the callout.
Thanks, really appreciate your help.

//******************Trigger******************************//
trigger updateDescription on Account (after update) {
  for (Account acc : Trigger.New) {
   for (Integer i = 0; i < Trigger.new.size(); i++)    {
if (Trigger.new[i].UpsellOpportunity__c != Trigger.old[i].UpsellOpportunity__c) {
     WebServiceCallout.sendNotification(acc.Id, acc.Name, acc.UpsellOpportunity__c);
    }
    }
  }
}
//**************************************callout******************************//
public class WebServiceCallout {
 
    @future (callout=true)
    public static void sendNotification(String id, String Name, String UpsellOpportunity) {
 
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
 
        req.setEndpoint('myendpoint');
        req.setMethod('POST');
        req.setBody('id='+EncodingUtil.urlEncode(id, 'UTF-8')+'&name='+EncodingUtil.urlEncode(name, 'UTF-8')+'&upsellOpportunity='+EncodingUtil.urlEncode(upsellOpportunity__c, 'UTF-8'));
        req.setCompressed(true); // otherwise we hit a limit of 32000
 
        try {
            res = http.send(req);
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(res.toString());
        }
 
    }
 
    // run WebServiceCallout.testMe(); from Execute Anonymous to test
    public static testMethod void testMe() {
        WebServiceCallout.sendNotification('acc-0001','Account1','Yes');
    }
 
}
**********************************************************************************************************
Also, if you could tell me one more thing; if i were to send these information on xml format, would that be possible, and how would i do it.
Thanks!
sfdcfoxsfdcfox

Your code may run into governor limits (e.g. 10 future methods per transaction) on data loads, but you might not be able to help that; I'd say that you might need to use a batch class to send larger amounts of callouts. Other than that, it looks well. As for sending XML, there are XML classes for reading and writing XML documents in memory, so you should look at the docs.

devrequirementdevrequirement
Thank you for your reply and suggestions again. Is there any available guidelines or samples which will explain the steps to covert callouts like these to batches? I really appreciate you helping me out again.
 
Thanks!