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
IPIP 

How to write a "After Insert" trigger that depend on the "Before insert".

I am working on "salesforce to salesforce". I am trying to write a trigger to automate the sharing.

My Scenario is: When i share an opportunity from my sandbox, it has to be shared to the other sandbox and also it has to be mapped to the same account. I know that it can be done using "ParentRecordId" parameter. But This is possible only if the respective account is already shared to the target sandbox. When the respective account is not shared, I want it to share the account first, and then the opportunity should be shared  by mapping to the account. So what i did is i've wrote two triggers on Opportunity object, one is "before insert" and the other is "After insert", as pasted below.

 

trigger sharelookupparent on Opportunity(before insert) {

    PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();
    newrecord.ConnectionId = '04P9000000008mXEAQ';
    newrecord.LocalRecordId = trigger.new[0].AccountId;  
    insert newrecord;
    
 }

 

The above trigger will create the account first.

 

The below trigger has to create the oppty and set the parent as the recently created acoount.

 

trigger sharelookup on Opportunity(after insert) {

     
     PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();
     newrecord.ConnectionId = '04P9000000008mXEAQ';
    newrecord.LocalRecordId = trigger.new[0].Id;  
    newrecord.ParentRecordId = trigger.new[0].accountid;
    insert newrecord;
    
 }

 

 

When i use these triggers it creates both the account and Opty. but the relationship between them is not established. I think this is because the Database Commit for both the trigger is happening together.

 

can someone please give me a solution for this.??

 

are there any way to make the Database Commit separately.??

 

Or please suggest someother way to achieve this?