0% found this document useful (0 votes)
3 views2 pages

Restsource

Uploaded by

harshasai0044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Restsource

Uploaded by

harshasai0044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

@RestResource(urlMapping='/v1/ServiceOrderResource/*')

global class ServiceOrderResource {


@HttpPost
global static void createOrder() {
String requestBody;
RestRequest req=RestContext.request;
requestBody= req.requestBody.toString();
System.debug(requestBody);
List<Order> newOrders = new List<Order>();
try{

List<Object> parseData = (List<Object>)


JSON.deserializeUntyped(requestBody);
List<Map<String, Object>> orderData = new List<Map<String, Object>>();
system.debug('Output1'+ orderData);
for(Object obj : parseData){
orderData.add((Map<String, Object>) obj);
}
for(Map<String, Object> data : orderData){
Order newOrder = new Order();
// newOrder.Id=null;
newOrder.Name =(String) data.get('Name');

String extAccountId =(String)


data.get('Account_External_ID__c');
System.debug('Output33'+ (String)
data.get('Account_External_ID__c'));
System.debug('Output3333'+ (Id)
data.get('Account_External_ID__c'));

Account acc = [Select Id,Account_External_ID__c From Account


where Account_External_ID__c =:extAccountId LIMIT 1];
System.debug('Ouput66'+acc);
if(acc!=null){
newOrder.AccountId = acc.Id;
System.debug('Output6666' +newOrder.AccountId);
}

newOrder.Status = (String) data.get('Status');


if(data.get('EffectiveDate') !=null){
newOrder.EffectiveDate = Date.valueOf((String)
data.get('EffectiveDate'));
}
newOrders.add(newOrder);
system.debug('Output2'+ newOrders);
}
update newOrders;

RestContext.response.statusCode =201;
RestContext.response.responseBody = Blob.valueOf('Orders created
successfully.');
}catch(Exception e){

System.debug('Output error'+e);
System.debug('Output line '+ e.getLineNumber());
System.debug('Output msg '+e.getMessage());
}
}
}

You might also like