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
Shri BEShri BE 

rest api to retrieve accountid and insert order

Hi friends,

I am working on Rest API service and I got stuck in middle and need your help to complete the code.

I have a common field between account and order and based on that field I need to map account Id and insert order.

Kindly refer the below class and need how to get the account id.
 
@RestResource(urlMapping='/insertOrder/*')
global with sharing class insertOrder
{
    //Insert Method
    
    @HttpPost
    global static String doPost(String cusid, String pl, String res) 
    {
              
        Order ord = new Order();
       // Need to get the account id.
        ord.AccountId = acc.Id;       
       // Customer Id is the common field in both account and order.
        ord.Customer_ID__c = cusid;
        ord.PL__c = pl;
        ord.Res__c = res;
        
        insert ord;
        
        return ord.id;
    }
}

Thanks in Advance.
Best Answer chosen by Shri BE
Danish HodaDanish Hoda
Hi There,
May I know from where you are getting accId?
ord.AccountId = acc.Id;      
// Customer Id is the common field in both account and order.

If CustomerId is common between order and the Account, then you should query for account like:

Account accObj = [SELECT Id FROM Account WHERE Customer_ID__c = cusid];
ord.AccountId = accObj.Id;  

All Answers

Danish HodaDanish Hoda
Hi There,
May I know from where you are getting accId?
ord.AccountId = acc.Id;      
// Customer Id is the common field in both account and order.

If CustomerId is common between order and the Account, then you should query for account like:

Account accObj = [SELECT Id FROM Account WHERE Customer_ID__c = cusid];
ord.AccountId = accObj.Id;  
This was selected as the best answer
Shri BEShri BE
Hi Danish,

Thanks for your reply. This is working.