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
Harshala ShewaleHarshala Shewale 

How to create record in another org using APEX

Hi,

I am trying to fetch some Lead records from ORG1  and if some condition is satisfied create records in org2  from my current org.

 

I have credential of both ORGs .

 

list<Record> records = new list<Record>();

SFConnectionController controller = new SFConnectionController();

// First org from where I am fetching Lead records
LoginResult loginResult = controller.login(firstOrgUsername, firstOrgPassword, securityToken);
if(!loginResult.isError)
{
   Response response = controller.query('select Name from Lead, loginResult);

  records = response.data.records;

  if(/*somecondition*/)
 {
   // Second org where I want to insert record 
      LoginResult loginResultPublishingOrg = controller.login(secondOrgUserNmme, secondOrgPassword, securityToken);
   // here I want to insert record - I am not sure is it right syntax or not - Subscribe_User__c is object in second org
      Response response_insert = controller.query('Insert into Subscribe_User__c (User_Name__c)values ('Harshala')', loginResultPublishingOrg);
	
   /**** Getting error in response


 }
}

 

Received following error :

response_insert:Response:[data=Result:[fields={}, records=()], isError=true, message=Query Failed, result=<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="urn:fault.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><soapenv:Fault><faultcode>sf:MALFORMED_QUERY</faultcode><faultstring>MALFORMED_QUERY: unexpected token: Insert</faultstring><detail><sf:MalformedQueryFault xsi:type="sf:MalformedQueryFault"><sf:exceptionCode>MALFORMED_QUERY</sf:exceptionCode><sf:exceptionMessage>unexpected token: Insert</sf:exceptionMessage><sf:row>1</sf:row><sf:column>0</sf:column></sf:MalformedQueryFault></detail></soapenv:Fault></soapenv:Body></soapenv:Envelope>, status=Internal Server Error, statusCode=500]

 

Help me to insert record in ORG2... not getting where I am missing point. :(

 

kiranmutturukiranmutturu

using apex it is not possible to insert records in different organizations.....but there is functionality called salesforce to salesforce where you can transfer records from one org to another ... basic help from here

Harshala ShewaleHarshala Shewale

Hi Kiran,

 

Thanks for reply, my requirement is to develope tthis with apex code - salesforce to saleforce will be a manual process. Process should be automated. 

kiranmutturukiranmutturu

ya this can be automated also.. like consider u r inserting the account object record in to org1 then u can create a trigger in the same org on the account object and share the record to org2...vice versa if you want to sync the org2 to org1 ...... 

Abhinav GuptaAbhinav Gupta

You can try what Aslam suggested here : http://techsahre.blogspot.in/2011/07/apex-toolkit-for-salesforce.html

 

Or this code from Salesforce 2 Salesforce setup should also help

 

PartnerNetworkRecordConnection newConnection =
                    new PartnerNetworkRecordConnection(
                        ConnectionId = networkId,
                        LocalRecordId = newContact.Id,
                        SendClosedTasks = false,
                        SendOpenTasks = false,
                        SendEmails = false,
                        ParentRecordId = newContact.AccountId);
insert newConnection;

 

Harshala ShewaleHarshala Shewale

Hi Abhinav  and Kiran,

 

There will be one issue to use saleforce to salesforce as process of inserting record in ORG2 will be handled in Clients org..so Client can be of any number and how can I make connection for each client. (Client is User who has installed my app). 

 

So, can you suggest me some way to achieve the same.

 

 

@ ABhinav : I am following Aslam's blog only but there he has not mentioned about insertion of records.

 

Thanks,

 

 

 

 

 

craigmhcraigmh

This makes me wonder if you can perform a callout in org1 to the SFDC API using the credentials of org2 to create the record.

Harshala ShewaleHarshala Shewale

Hi Craigmh,

 

Can you please elaborate your idea? Now I have created webservice in org2 and calling that in Client's Org

//Org2 - webservice 

global class InsertSubscribeLicenseUser
{

global InsertSubscribeLicenseUser()
{
} 

webservice static void createRecord(string name)
{

Subscribe_License_User__c rec = new Subscribe_License_User__c();
rec.User_Name__c = name;
insert rec;
}


}

//Client's Org -
//SubscribeData is stub generated in WSDL2APex
SubscribeData.InsertSubscribeLicenseUser publishingOrg = new SubscribeData.InsertSubscribeLicenseUser();

publishingOrg.outputHttpHeaders_x = new Map<string, string>();
publishingOrg.inputHttpHeaders_x = new Map<String, String>();                      
                       
publishingOrg.createRecord('TempUserName');



 

but it is also throwing error while running...

 

 

System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session faultcode=sf:INVALID_SESSION_ID faultactor= 

 


I have search a lot on and spent my day wasted but didnt get anything useful...

 

Can anyone help me ?

craigmhcraigmh

Pretty much the stuff in here:

http://wiki.developerforce.com/page/Apex_Web_Services_and_Callouts

 

I use this for two-way integration between SFDC and my company's servers, but you can probably make it work for one-way integration between two SFDC instances.

CloudDeveloper89CloudDeveloper89

Hi Kiran,

 

I am also facing the same issue. I can succesfully query the records from Server(Org 1) and can display them on Client(Org 2). However, I want a two-way integration from S2S. I need to insert records from Client to Server. I tried to write apex code for it through saveresult[] class but its generating some error.

Is there any workaround for achieving this ? Please suggest. 

 

Thanks,

Soni

murali krishna.ax1501murali krishna.ax1501

i searched for more time but i am unable to find the solution.

Finally i have used email handler to achieve the requirement.