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
Sahil YadavSahil Yadav 

Creating Opportunity Line Item Record in Target org Using Rest API

Hello Developers,
           I had came accross one business requirement where i needed to send the opportunity line item record from source org to an external org using Rest API.
WHile sending a request a method i kept is POST and at that moment only i will be sending the data of opportunity Line Item in Json format and at the target org the request which is been sent is also coming but after deserializing the request in target org the data is becooming null and while insertion of record in target org throuwing error.
Sending the Request to Target Org======


Global class IntegrationClass {
    /*
    @HttpGet
    global static List<opportunity> doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String BuyerId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account result = [SELECT Id, Name, Phone,Bi_Id__c, Website FROM Account WHERE Bi_Id__c = :BuyerId];
        
            
                String acc = result.Id;
                List<Opportunity> opp = [Select id, name, accountId from Opportunity where accountId =:acc ];
                 return opp;
            
           
        
        
    }*/
    public static void sendData(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://capge56-dev-ed.my.salesforce.com/services/apexrest/v1/opportunityLineItem/*');
        request.setMethod('POST');
        OpportunityLineItem opp = [Select id, name,ListPrice,UnitPrice, Opportunity.Id,Opportunity.CloseDate, Opportunity.Account.BI_ID__c from OpportunityLineItem  Limit 1];
        //OpportunityLineItem opp = [Select id, name,ListPrice,UnitPrice from OpportunityLineItem  Limit 1];
         String jsonString  =  JSON.serialize(opp);
        System.debug(jsonString);
        String name = opp.name;
        System.debug('!!!' +name);
        Decimal listPrice = opp.ListPrice;
        System.debug('!!!' +listPrice);
        Decimal unitPrice = opp.UnitPrice;
        System.debug('!!!' +unitPrice);
        Date closeDate = opp.Opportunity.CloseDate;
        System.debug('!!!' +closeDate);
        
        request.setBody(jsonString);
       // request.setBodyAsBlob(OpportunityLineItem);
        //request.setBody('{"Name":"any--JUL 2022 Mobile", "ListPrice" : "123.00", "UnitPrice" : "123.00" , "CloseDate" : "2022-07-01 00:00:00"}');
        request.setHeader('Content-Type', 'Application/Json; charset = UTF-8');
        System.debug('!!!'+request);
        HttpResponse response = http.send(request);
        System.debug('!!!'+response);
    }
        

}
 
Exposing an Apex class as Rest Web Service Class


@RestResource(urlMapping = '/v1/opportunityLineItem/*')
Global class createOpportunityLineItem {
    
    @HttpPost
    Global static opportunityLineItemWrapper insertOpportunityLineItem( /*opportunityLineItemWrapper oLIW*/){
        
        RestRequest req = RestContext.request;
        
        RestResponse response = RestContext.response;
        System.debug('@@@@' +req.requestBody);
        
       String  result = req.requestBody.toString();
       //String result = JSON.Stringify(req.requestBody);
        System.debug('@@@'+result);
        
        opportunityLineItemWrapper oLIW = (opportunityLineItemWrapper)JSON.deserialize(result, opportunityLineItemWrapper.class );
        System.debug('@@@'+oLIW);
         
        /*
        for(OpportunityLineItem o : oLIW ){
            o.Id = null;
            insert o;
        }*/
        
        OpportunityLineItem oLIRecord =  new OpportunityLineItem();
        // oLIRecord = oLIW.opportunityLineItemRecord;
       // oLIRecord.Name = oLIW.Name;
        //oLIRecord.ListPrice = oLIW.ListPrice;
        oLIRecord.UnitPrice = oLIW.UnitPrice;
        oLIRecord.OpportunityId = oLIW.OpportunityId;
        oLIRecord.Quantity = oLIW.Quantity;
       // insert oLIRecord;
        //System.debug('@@@'+oLIRecord);
        //insert oLIRecord;
              
        
        
        //Opportunity oppRecord = oLIW.opportunityRecord;
        //insert oppRecord;
        
        //Product2 prodRecord = oLIW.productRecord;
        //insert prodRecord;
        
                
        //OpportunityLineItem oLIRecord = oLIW.opportunityLineItemRecord;
        //oLIRecord.Product2Id = prodRecord.Id;
        //oLIRecord.OpportunityId = oppRecord.Id;
        //Insert oLIRecord;
        
        return oLIW;
        
       
        
    }
    
    
    
    

}
 
Wrapper class for Opportunity Line Item



Global class opportunityLineItemWrapper{
    // Global Opportunity opportunityRecord {get; set;}
    //Global Product2 productRecord {get; set;}
    //Global OpportunityLineItem opportunityLineItemRecord {get; set;}
    // Global class opportunityLineItem{
    public string name;
    public integer ListPrice;
    public integer UnitPrice;
    public string OpportunityId;
    public integer Quantity;
    
    // }
    // 
    
    public static opportunityLineItemWrapper parse(String json) {
        return (opportunityLineItemWrapper) System.JSON.deserialize(json, opportunityLineItemWrapper.class);
    }
    public class opportunityLineItemWrappers
    {
    public List<opportunityLineItemWrapper> opportunityLineItemWrapper { get; set; }
    }
    
    
}
 
Sending Request Through PostMan
/*
{
  "OpportunityLineItem": {
    
    "UnitPrice": "123",
    "OpportunityId" : "0065j00000TSPdFAAX",
    "Quantity" : "4",
    "Product2Id" : "01t5j0000049jQsAAI"
  }
}
*/