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
MohandaasMohandaas 

Send data to external script on create/update of contact

Hi All,

 

My salesforce org is integrated with a third party tool. I need to pass the contact Id to a given URL or external script on create or update of contact.

 

Below is the link. Unavailable currently.

http://www.metricmill.com/tool/inc/employee_integration.php?process=create&sfgdc_employee_id=[INJECTION-PARAMATER-OF-SFDC-CONTACT-ID]

 

Any ideas or solutions to implement this. If I have to write apex script how can i invoke.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
MohandaasMohandaas

Haven't tested yet. Did something like this

 

public
class WebServiceCallout {

 

   
@future (callout=true)

   
public static void sendNotification(String name, String city) {

 

       
HttpRequest req = new HttpRequest();

       
HttpResponse res = new HttpResponse();

       
Http http = new Http();

 

       
req.setEndpoint('http://my-end-point.com/newCustomer');

       
req.setMethod('POST');

       
req.setBody('name='+EncodingUtil.urlEncode(name,
'UTF-8')+'&city='+EncodingUtil.urlEncode(city, 'UTF-8'));

       

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('My Test Customer','My City');

   
}

 

}

You can execute
your callout in a trigger with the following example:

 

trigger
AccountCallout on Account (after insert) {

 

       
for (Account a : Trigger.new) {

              
// make the asynchronous web service callout

              
WebServiceCallout.sendNotification(a.Name, a.BillingCity);

       
}

 

}

All Answers

CodeFinderCodeFinder

What do you want to happe? do you just want to send the id to the link. I mena do you want to give a button to which the id must be appended or do you want salesforce to navigate to that external app as soon as the contact  is created?

MohandaasMohandaas

I only want to send the recordID to the link every time a contact is created or updated.

 

Please tell me how to do it. This should happen immediately after user clicks save.

CodeFinderCodeFinder

Check this code. Le me know if this helps you.

 

trigger ExampleTrigger on Account (after insert, after update) {

	Account Acnt = Trigger.new[0];
	Account account =[Select id, name, from from Account where id =:Acnt.Id];

	String s = 'http://www.metricmill.com/tool/inc/employee_integration.php?process=create&sfgdc_employee_id=' + account.id;
	// use the string at the place where you want the string to be updated.
	update account;

}

 

MohandaasMohandaas

I am not sure If this well serve the purpose.

 

From little what I know, we should be using http POST or cURL something of this sort.

CodeFinderCodeFinder

I know that this will be useful in just writing an account . but what is the functionality you want. what shud happen after the record is created?

MohandaasMohandaas

I want to pass the record ID to the external link(end point). Hope you got me right.

CodeFinderCodeFinder

I am not sure what you want to do after you set it as end point.  jUst check this . this is as per my understanding . if you more clear and . let me know what do you want to do next. I am sorry but I am trying to understand the issue.

 

trigger ExampleTrigger on Account (after insert, after update) {

	Account Acnt = Trigger.new[0];
	Account account =[Select id, name, from from Account where id =:Acnt.Id];
	String s = 'http://www.metricmill.com/tool/inc/employee_integration.php?process=create&sfgdc_employee_id=' + account.id;
	req.setEndpoint(s);'
	req.setMethod('POST');
		req.setHeader('Content-Type', 'application/soap+xml');
		req.setTimeout(60000);
	String body = '';//what ever data you want to send
	req.setBody(body);
		Http http = new Http();
	HTTPResponse res = http.send(req);
	String response = res.getBody();

}

 

CodeFinderCodeFinder

Was the code of any help?

MohandaasMohandaas

Haven't tested yet. Did something like this

 

public
class WebServiceCallout {

 

   
@future (callout=true)

   
public static void sendNotification(String name, String city) {

 

       
HttpRequest req = new HttpRequest();

       
HttpResponse res = new HttpResponse();

       
Http http = new Http();

 

       
req.setEndpoint('http://my-end-point.com/newCustomer');

       
req.setMethod('POST');

       
req.setBody('name='+EncodingUtil.urlEncode(name,
'UTF-8')+'&city='+EncodingUtil.urlEncode(city, 'UTF-8'));

       

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('My Test Customer','My City');

   
}

 

}

You can execute
your callout in a trigger with the following example:

 

trigger
AccountCallout on Account (after insert) {

 

       
for (Account a : Trigger.new) {

              
// make the asynchronous web service callout

              
WebServiceCallout.sendNotification(a.Name, a.BillingCity);

       
}

 

}

This was selected as the best answer