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
Eager-2-LearnEager-2-Learn 

Custom Clone button that also clones the Partner records???

Hello,

 

I have found a link in this forum that took me a website that had a class developed that creates a Clones a PO and Line Items too.  I used it to create my Clone class that will Clone the Opportunity and related Partner records but I cannot get the Partner record portion to work correctly.  I get the following error message.  Hopefully someone can help me past this issue?

•Opportunity: field integrity exception: OpportunityId, AccountToId (account to cannot be opportunity account) •java.sql.SQLException: ORA-20067: ORA-06512: at "HAPPY.GUTIL", line 42 ORA-06512: at "HAPPY.SPARTNER", line 458 ORA-06512: at line 1 : {call sPartner.insert_detail(?,?,?,?,?,?)}

 

The code that I have is below and the Opportunity will clone successfully if I comment out the portion that attempts to clone the partner related records that is in bold red below.

 

public class OppCloneWithPartnerRecsController { //added an instance varaible for the standard controller private ApexPages.StandardController controller {get; set;} // add the instance for the variables being passed by id on the url private Opportunity opp {get;set;} // set the id of the record that is created -- ONLY USED BY THE TEST CLASS /////public ID newRecordId {get;set;} // initialize the controller public OppCloneWithPartnerRecsController(ApexPages.StandardController controller) { //initialize the stanrdard controller this.controller = controller; // load the current record opp = (Opportunity)controller.getRecord(); } // method called from the VF's action attribute to clone the po public PageReference cloneWithPartners() { // setup the save point for rollback Savepoint sp = Database.setSavepoint(); Opportunity newOpp; try { //copy the purchase order - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE //WHEN MOVED TO PRODUCTION ADD THESE FIELDS // Strenghts__c, Underwriting__c, opp = [select Id, AccountId, Name, CloseDate, StageName, Effective_Date__c from Opportunity where Id = :opp.id]; opp.CloseDate = opp.CloseDate + 365; opp.Effective_Date__c = opp.Effective_Date__c + 365; newOpp = opp.clone(false); insert newOpp; // set the id of the new po created for testing //// newRecordId = newPO.id; // copy over the partners - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE List<Partner> partners = new List<Partner>(); for (Partner pr : [Select AccountToId, AccountFromId, p.IsPrimary, p.OpportunityId, p.Role From Partner p where OpportunityId = :opp.id]) { Partner newPR = pr.clone(false); newPR.OpportunityId = newOpp.id; partners.add(newPR); System.debug(newOpp.id); } insert partners; } catch (Exception e){ // roll everything back in case of errors Database.rollback(sp); ApexPages.addMessages(e); return null; } //return new PageReference('/'+newOpp.id+'/e?retURL=%2F'+newOpp.id); return new PageReference('/'+newOpp.id); } }

 

Best Answer chosen by Admin (Salesforce Developers) 
Martha_SenetaMartha_Seneta

What appears in the UI to be a single Opportunity Partner record, is actually two records on the back-end.  In one of those records, the "AccountToId" is the Opportunity's own Account; in the other record, the "AccountToId" is the partner Account.

When you clone, you need to ignore the existing Partner records on which "AccountToId" is the Opportunity's own Account.  Only clone the other records.  (So, in other words, you will only clone and insert half of the records you retrieve by querying.)  Insert these as Partner records (not OpportunityPartner) and SFDC will automatically generate the pair of OpportunityPartner records on the back-end.

 

Here is what the highlighted portion of your code would look like with the cloning part wrapped in an if clause so it ignores as appropriate:

 

List<Partner> partners = new List<Partner>();
for (Partner pr : [Select AccountToId, AccountFromId, p.IsPrimary, p.OpportunityId, p.Role From Partner p where OpportunityId = :opp.id]) {
    if (pr.AccountToId != newOpp.AccountId) {
        Partner newPR = pr.clone(false);
        newPR.OpportunityId = newOpp.id;
        partners.add(newPR);
        System.debug(newOpp.id);
    }

}
insert partners;

All Answers

Eager-2-LearnEager-2-Learn

I am sorry but the original post I include the error message when I took out the AccountFromId field.  When I put that field back in the query the error is...

 

Opportunity: Cannot specify both Opportunity and AccountFrom

Eager-2-LearnEager-2-Learn

I took another approach to see if I got the same error message and I did. I used the DataLoader and exported two Partner records.  When I tried to insert those two records back into the system I still the the error:

 

Cannot specify both Opportunity And AccountFrom

Is there anyone else who is wanting to clone the partner records associated with the opportunity or even trying to use the dataloader to reinstate partner data who is having this same issue?

Martha_SenetaMartha_Seneta

What appears in the UI to be a single Opportunity Partner record, is actually two records on the back-end.  In one of those records, the "AccountToId" is the Opportunity's own Account; in the other record, the "AccountToId" is the partner Account.

When you clone, you need to ignore the existing Partner records on which "AccountToId" is the Opportunity's own Account.  Only clone the other records.  (So, in other words, you will only clone and insert half of the records you retrieve by querying.)  Insert these as Partner records (not OpportunityPartner) and SFDC will automatically generate the pair of OpportunityPartner records on the back-end.

 

Here is what the highlighted portion of your code would look like with the cloning part wrapped in an if clause so it ignores as appropriate:

 

List<Partner> partners = new List<Partner>();
for (Partner pr : [Select AccountToId, AccountFromId, p.IsPrimary, p.OpportunityId, p.Role From Partner p where OpportunityId = :opp.id]) {
    if (pr.AccountToId != newOpp.AccountId) {
        Partner newPR = pr.clone(false);
        newPR.OpportunityId = newOpp.id;
        partners.add(newPR);
        System.debug(newOpp.id);
    }

}
insert partners;

This was selected as the best answer
Jerun JoseJerun Jose
Good to know. Thanks for sharing.