+ Start a Discussion
ALAL 

Rest API Callout Adding Opportunity Products

Hello I am creating a REST api callout between two Salesforce instances in order to send opportunitiy records and their opportunity products. The current Salesforce to Salesforce functionality isn't allowing us to transfer more than one product for an opportunity. I am able to update the opportunity name using the workbench rest explorer, but I can't add or delete opportunity products. Any help with modifying my code to add or delete products would be appreciated. 

 
Rest Class

@RestResource(urlMapping='/Opportunities/*')
global with sharing class S2SOpp {

@HttpPut 
 //   global static List<Opportunity> upsertOpp(String name, String id){
      global static ID upsertOpp(String name, String id, String oppProductId, String oppProductName, Double quantity, Double unitprice,
                                Double listprice, Double totalprice, String product2Id, String opportunityId){
        HttpRequest req = new HttpRequest();
        req.setEndpoint('callout:S2S_Integration') ;
        req.setMethod('GET');
        Http http = new Http();
        HTTPResponse res = http.send(req);
        System.debug(res.getBody());
        List<Opportunity> listOpps = new List<Opportunity>();
        Set<Opportunity> setOpps = new Set<Opportunity>();
        Set<Id> ParentsIds = new Set<Id>();
        Set<OpportunityLineItem> setOli = new Set<OpportunityLineItem>();
        List<OpportunityLineItem> oli = [Select Id, OpportunityId from OpportunityLineItem where OpportunityId IN: listOpps];
        Map<Id, Opportunity> mapOpp = new Map<Id, Opportunity>([Select Id from Opportunity]);
        PriceBookEntry pbeEntry = new PriceBookEntry();
        List<PriceBookEntry> priceBookList = [Select Id, Product2Id, Product2.Id, Product2.Name 
                                             from PriceBookEntry where PriceBook2.isStandard = true];
       //  List<OpportunityLineItem> oppProds = new List<OpportunityLineItem>(OpportunityId = o.Id, PriceBookEntryId = pbeEntry.Id, Quantity = 1);
        for(Opportunity o: mapOpp.values()){
            
      
            for(OpportunityLineItem olis: o.opportunitylineitems){
                if(olis.Id != null){
                 listOpps.add(o);
                }
            }
            
        }
        RestRequest request = RestContext.request;
        PricebookEntry pbe = new PricebookEntry();
        Opportunity thisOpp = new Opportunity(Id = id, Name = name);
        OpportunityLineItem thisOLI = new OpportunityLineItem(Id = oppProductId, Quantity = 1, UnitPrice = unitprice, 
                                                              TotalPrice = totalprice, Product2Id = product2Id,
                                                             OpportunityId = opportunityId);
         
        
        upsert listOpps;
        upsert thisOpp;
        return thisOpp.Id;
    }
    
}



Workbench 

/services/apexrest/Opportunities/*

{
"name" : "Test",
"id" : "0061Q00000ohYrj",
"oppProductId" : "00k1Q00003ZzZbb",
"quantity" : "1.00",
"unitprice": "17.00",
"totalprice":  "17.00",
"product2Id" : "01t1Q000006nJn7",
"opportunityId" : "0061Q00000ohYrj"

}