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
Salesforce 283Salesforce 283 

When we click on button in one org it will automatically create contact in another org by using Soap API Callout

Hi folks,

I have written one class in org1.I generated wsdl file and partner wsdl of that class. I login to other salesforce org2 and import those two files into this org. I created one stub class in org2 and using this class i have called those classes and methods and callout this stub class using developer console. While executing this class it throws an error [System.CalloutException: IO Exception: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://login.salesforce.com/services/Soap/u/34.0]. I have pasted login url in Remote site settings in org 2.

Apex class in Org1:
global class webservicesoap1
{
 webservice static string createcontact(string Fname,string Lname,String phone)
 {
 list<contact> conlist=[select id from contact where firstname =:fname and lastname =:lname and phone =:phone];
  if(conlist.size()!=0)
   {
    return 'contact already exists';
    }
    else{
     contact c=new contact();
     c.firstname=fname;
     c.lastname=lname;
     c.phone=phone;
     insert c;
     return c.id;
     }
     }
     }

Stub Class i implemented in org2:

public class soapcallout
{

    public soapcallout(ApexPages.StandardController controller) {

    }

 public soapcallout()
 {
    partnerSoapSforceCom.soap spc=new partnerSoapSforceCom.soap();
    string username ='sfgt@283.com';
    string password ='radhekrishna***AIyGjqKUpuRnKaBzoX6XRUnzH';
    partnerSoapSforceCom.loginresult loginresult = spc.login(username, password);
    system.debug(loginresult.sessionid);
    
    soapSforceComSchemasClassWebservice.webservicesoap1 websrvc= new soapSforceComSchemasClassWebservice.webservicesoap1();
    soapSforceComSchemasClassWebservice.SessionHeader_element sessionheader =new soapSforceComSchemasClassWebservice.SessionHeader_element();
    sessionheader.sessionid = loginresult.sessionid;
    websrvc.timeout_x = 120000;
    websrvc.sessionheader = sessionheader;
    string s = websrvc.createcontact('Mahesh','Krishna','9526389523');
    }
    }



 
KaranrajKaranraj
Using NamedCredentials you can able to integrate between two different salesforce org easily. Check this blog post for the step by step procedure to create Name Credentials in salesforce  http://www.jitendrazaa.com/blog/salesforce/salesforce-to-salesforce-integration-using-named-credentials-in-just-5-lines-of-code/

Here is the below sample code which will create contact using REST API
HttpRequest feedRequest = new HttpRequest();
feedRequest.setEndpoint('callout:Salesforce_Org/services/data/v33.0/sobjects/contact');
JSONGenerator generator = JSON.createGenerator(true);   //instantiation of the generator
generator.writeStartObject();               // Writes the starting marker of a JSON object '{'
generator.writeStringField('LastName', 'RESTAPEXCONTACT');   //Writes the # of contacts
generator.writeEndObject();   
String jsonString = generator.getAsString();
feedRequest.setMethod('POST');
feedRequest.setBody(jsonString);
Http http = new Http();
HTTPResponse feedResponse = http.send(feedRequest);
System.debug(feedResponse);

Thanks,
Karan
 
Shashikant SharmaShashikant Sharma
Have youtrid your webservice with SoapUI ? Please try once if webservice is working fine.

Could you paste the code of Apex Class that you have generated from WSDL file
Salesforce 283Salesforce 283
Hi Shashikant, Apex code WSDL file:
Salesforce 283Salesforce 283
Hi Karan, I had already seen this blog. i tried named credentials but didnt get that. Can you pls send me the steps clearly how to create if you are free.
KaranrajKaranraj
Step1 [Connected Apps]:
  • First you have to create Connect apps in your destination org(where you want to create contact records). To create Connected App in Salesforce, Navigate to “Setup | Build | Create | Apps | Connected Apps” and click on New.
  • Provide All information except “Callback URL”. We will comeback again on this step later to provide Callback URL.
  • If you click save then it will provide the consumer key and client sceret code which we use in our next step. Refer the screenshot below

User-added image

Step 2 [Authorization Provider]:

Now move into the another org from where you are going to send information and create the Authorization provider. 
  • Navigate to “Setup | Administer | Security Controls | Auth. Providers | Create New”. Select “Salesforce” as provider Type.
  • We need to provide “Consumer Key” and “Consumer Secret” created in previous step.
  • Also one important setting is “Default Scope“, it should have value as “refresh_token full”. “refresh_token” and “full” should be separated by space.
  • Authorize Endpoint URL should be something like “https://login.salesforce.com/services/oauth2/authorize” and Token Endpoint URL “https://login.salesforce.com/services/oauth2/token“.
  • Click save which will generate the some client configuration URLs, copy the Call back URL from the Authorization provider and paste it in the 'Callback URL' section of 'Connected Apps'
Step3 [Named Credentials]:

Now create the Named Credentials. Navigate to “Setup | Administer | Security Controls | Named Credentials | New Named Credential “
  • Label and Name Any suitable value. We will refer that name in the Apex code
  • URL URL of Salesforce instance where we want to Connect
  • Identity Type - Named Principal
  • Authentication - Protocol OAuth 2.0
  • Authentication Provider - Select Authentication Provider created in step 2
  • Scope - refresh_token full
Now run the code which I shared in the previous comment in the anonymous window of developer console to verify.
 
Salesforce 283Salesforce 283
Hi karan, Can i modify any changes in the code and Where can i put the Label name of named credential in Code. In second line of code what is the Salesforce_Org.
Salesforce 283Salesforce 283
Hi karan, Can i modify any changes in the code and Where can i put the Label name of named credential in Code. In second line of code what is the Salesforce_Org.
KaranrajKaranraj
Salesforce_Org is the name of my Named Credentials which we created in the Step 3 you have to replace with your label name of your named credential.
Salesforce 283Salesforce 283
Hi Karan, I have followed your steps which you mentioned above but not created a contact after run the code.
Salesforce 283Salesforce 283
Hi Karan, I have checked a App which i created connected app also in Profile level but still not created.
Salesforce 283Salesforce 283
Hi Karan, Please take me out from this problem if you r free.