Friday, 11 April 2014

Salesforce to Salesforce Connection Using Trigger

Hi

When we publish connection between one org to another org, it will be very difficult for the end user to forward the data manually to the another org.

Salesforce provide some standard object, to get the connection and do some automation for connections.



PartnerNetworkConnection - It is used to get the connection Id from the salesforce.


PartnerNetworkRecordConnection - It is used to push the record into another organization.


Apex Trigger Code.

trigger ConnectionOnLead_Trg on Lead (after insert,after update) {
List<Lead> Leadlist = new List<Lead>();
for(Lead l: trigger.New)
{
    if(l.Status == 'Closed Xfer' && l.Lead_Source_Original__c == 'Prod1')
    {
        Leadlist.add(l);
    }
}
if(Leadlist.size() > 0)
{
    List<PartnerNetworkConnection> connMap = new List<PartnerNetworkConnection>();
    List<PartnerNetworkRecordConnection> connMaplist = new List<PartnerNetworkRecordConnection>();
    connMap = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionStatus = 'Accepted'];
    system.debug('---------Connection Trg----------------------'+connMap+'*****SIZE*******'+connMap.size());
    for(PartnerNetworkConnection Network : connMap)
    {
        for(Lead le : Leadlist)
        {
             PartnerNetworkRecordConnection conn= new PartnerNetworkRecordConnection();
             conn.ConnectionId = network.Id;
             conn.LocalRecordId = le.Id;
             conn.SendClosedTasks = false;
             conn.SendOpenTasks = false;
             conn.SendEmails = false;
           //  conn.ParentRecordId = newContact.AccountId);
           connMapList.add(conn);
        }
    }
    if(connMapList.size() > 0)
    {
        insert connMapList;
    }
}
}

If it met the criteria, it will automatically publish the record to another org.

2 comments:

  1. Here Org means sandboxes in same company or two different companies.

    ReplyDelete
  2. Here I meant was 2 different companies, but if you want to make connection between 2 different sandbox that is also possible.

    ReplyDelete