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
Ashish KhairkarAshish Khairkar 

Populate Related list via Rest Class

I have a Realted list on Case Object Layout "Case Product List"

Now i have written Rest calss which would create a case and populate account and contact information in it.

But i want this "Case Product List" to be alos poulated via the rest class. How can i achieve this

Below is my Rest Code
@RestResource(urlMapping='/RestServiceCreateCase/*')
global with sharing class RESTCreateCaseController {

@HttpPost   
  global static responseClass createNewCase(string ContactName,string AccountName,string ShipToAccName,string BusinessUnit,string SalesRep,string ProductName,string Quantity)
   {
     System.debug('Name: '+ContactName);
     System.debug('AccountName: '+AccountName);
     System.debug('ShipToAccount: '+ShipToAccName);
     System.debug('BusinessUnit: '+BusinessUnit);
     System.debug('SalesRep : '+SalesRep);
     System.debug('ProductName: '+ProductName);
     System.debug('Quantity: '+Quantity);
         
     responseClass rc=new responseClass();
      
    List<Account> Acc = [Select ID, Name, Account_Type__c from Account where Name =:AccountName limit 1];   
     system.debug(Acc);
  
     if(Acc.isEmpty())
        {
          Account NewAccount = new Account (
                                            Name = AccountName,
                                            Account_Type__c = 'MCO'
                                           );
         insert NewAccount;
         Acc = [Select ID, Name, Account_Type__c from Account where Name =:AccountName]; 
         system.debug('Insert Account :'+Acc); 
        }
     else
      {
      if(Acc[0].id != Null)
          {
          system.debug('Account Present'+Acc); 
          } 
      }
    
    List<Account> STAcc = [Select ID, Name, Account_Type__c from Account where Name =:ShipToAccName limit 1];   
     system.debug(STAcc);
  
     if(STAcc.isEmpty())
        {
          Account NewAccount = new Account (
                                            Name = ShipToAccName,
                                            Account_Type__c = 'MCO'
                                           );
         insert NewAccount;
         STAcc = [Select ID, Name, Account_Type__c from Account where Name =:ShipToAccName]; 
         system.debug('Insert Account :'+STAcc); 
        }
     else
      {
      if(STAcc[0].id != Null)
          {
          system.debug('ST Account Present'+STAcc); 
          } 
      }
    
   List<Contact> Con = [Select ID, Name, Email from Contact where LastName =:ContactName limit 1];   
     system.debug(Con );
    
     if(Con.isEmpty())
        {
         Contact NewContact = new Contact (LastName = ContactName);
         insert NewContact;
         Con = [Select ID, Name, Email from Contact where Name =:ContactName]; 
         system.debug('Insert Contact:'+Con); 
        }
     else
      {
      if(Con[0].id != Null)
          {
          system.debug('Contact Present'+Con); 
          } 
      }
    List<User> SelRep = [Select ID, Name from User where Name =:SalesRep limit 1];   
    system.debug(SelRep );
   
    system.debug(Acc[0].id);
    system.debug(Con[0].id);
    system.debug(SelRep[0].id);
    system.debug(STAcc[0].id);   
    
     Case c = new Case();
     c.Status = 'Open';
     c.Origin ='Web';
     c.Service_AGN__c = 'Queixa Profissional Saúde';
     c.Reason = 'Evento Adverso';
     c.Type = 'Update';
     c.Donation_or_Replacement_AGN__c = 'Pharma Complaint';
     c.Sales_Rep_Requester_AGN__c = '0055800000121ma';
     c.Priority = 'Low';
     c.Business_Unit_AGN__c='Ophthalmology';
     c.Complaint_Division_AGN__c='Pharma Complaint';
     c.Patient_AGN__c='a5U6E0000004FLm';
     c.Description='Account Email Update';
     c.AccountId=Acc[0].id;
     c.Ship_To_Account_AGN__c=STAcc[0].id;
     c.ContactId=Con[0].id;    
     insert c;
     system.debug(c); 
      
     Case c1 = [SELECT Id,  CaseNumber FROM Case Where Id =:c.Id];
     rc.CaseNumber= c1.CaseNumber;
     system.debug(rc); 
     return rc;    
  }
  
 global class responseClass{
       string CaseNumber;
   } 
  
}
This is how Case Product List is mapped to Case object