+ Start a Discussion
ALAL 

Salesforce to Salesforce Adding Opportunity Products

Hello I am trying to add opportunity products to my opportunity when I share my opportunity records with my target org. This trigger to be on the target org. Right now I believe the code is only setup to include one product per opportunity, but some opportunuties may have two products.  One error that I'm getting is: List has more than 1 row for assignment to SObject.

Below is my code. Thank you for your help.

trigger OppProduct3 on Opportunity (after insert) {

    List<Pricebook2> pbe = [SELECT id from Pricebook2 Where Name = 'Test Pricebook 7-17'];

      If(Trigger.isAfter){
          
    OpportunityLineItem[] lines = new OpportunityLineItem[0];
          
PricebookEntry entry = [SELECT ID From PricebookEntry Where Pricebook2Id = '01s8E000000IU4C' AND Product2.ProductCode = 'DEFAULT'   ];
        
        set<ID> FinalOptyIds = new set<id>();
          
          List<PartnerNetworkRecordConnection> ConListOpty = new List<PartnerNetworkRecordConnection>([select Id, ConnectionId, PartnerRecordId, LocalRecordId from PartnerNetworkRecordConnection
                                                                                                     where LocalRecordId in :Trigger.newmap.keyset() AND (Status ='Sent' OR Status = 'Received') AND ConnectionId='04P8E000000CaSKUA0']);
        system.debug('ConListOpty-----' + ConListOpty);
        
        If(!ConListOpty.IsEmpty()){
            
            for(PartnerNetworkRecordConnection op : ConListOpty){
                if(op.LocalRecordId != null){
                    FinalOptyIds.add(op.LocalRecordId);
                }
                system.debug('FinalOptyIds-----' + FinalOptyIds);
            }
        }
          
          If(!pbe.IsEmpty()){
          
          for(Opportunity record: Trigger.new){
              lines.add(new OpportunityLineItem(PricebookEntryId = entry.Id, OpportunityId = record.Id, Quantity =1));
              
          }
         insert lines;
          }
          
      }
}
Maharajan CMaharajan C
Hi,

Change this line:  OpportunityLineItem[] lines = new OpportunityLineItem[0];  --->  OpportunityLineItem[] lines = new OpportunityLineItem[];

Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
​Raj
ALAL
Hi Raj,

Thanks, I tried but it didn't work. I'm trying to modify the code to use a SOQL statement in the Opportunity Line. Also, because this is a salesforce to salesforce connection, I'm trying to pass the UnitPrice of the opportunity product from the source org to the target org. In the below code I receive two errors:

1. Line 7 - Type requires name= value pair construction: OpportunityLineItem
2. Variable does not exist; License_Amount__c

trigger OppProduct3 on Opportunity (after insert) {

    List<Pricebook2> pbe = [SELECT id from Pricebook2 Where Name = 'Test Pricebook 7-17'];

      If(Trigger.isAfter){
          
    OpportunityLineItem[] lines = new OpportunityLineItem([SELECT Id, License_Amount__r from OpportunityLineItem where OpportunityLineItem.Name = 'Product 1' OR OpportunityLineItem.Name = 'Product 2' ]);
          
          
PricebookEntry entry = [SELECT ID From PricebookEntry Where Pricebook2Id = '01s8E000000IU4C' AND Product2.ProductCode = 'DEFAULT'   ];
    
  
        
        set<ID> FinalOptyIds = new set<id>();
          
          List<PartnerNetworkRecordConnection> ConListOpty = new List<PartnerNetworkRecordConnection>([select Id, ConnectionId, PartnerRecordId, LocalRecordId from PartnerNetworkRecordConnection
                                                                                                     where LocalRecordId in :Trigger.newmap.keyset() AND (Status ='Sent' OR Status = 'Received') AND ConnectionId='04P8E000000CaSKUA0']);
        system.debug('ConListOpty-----' + ConListOpty);
        
        If(!ConListOpty.IsEmpty()){
            
            for(PartnerNetworkRecordConnection op : ConListOpty){
                if(op.LocalRecordId != null){
                    FinalOptyIds.add(op.LocalRecordId);
                }
                system.debug('FinalOptyIds-----' + FinalOptyIds);
            }
        }
          
          If(!pbe.IsEmpty()){
          
          for(Opportunity record: Trigger.new){
              lines.add(new OpportunityLineItem(PricebookEntryId = entry.Id, UnitPrice = entry.License_Amount__c, OpportunityId = record.Id, Quantity =1));
              
          }
         insert lines;
          }
          
      }
}