Hi i need help with my response message when inserting records with webservice, I need to have 3 response message. 1 where in If all the products have no error, 1 for the error message of each record and then if one of the records inserted has an error. I used a wrapper when inserting records because i can insert many records of one parent in one call.
Can someone help me figure this out.
This is my code
@restResource(urlMapping='/merchandise/*')
global with sharing class buyProduct{
global with sharing class Wrapper1{
public string CustomerNumber {get;set;}
public List<Wrapper2> wrapper2 {get;set;}
}
global with sharing class Wrapper2{
public decimal Quantity {get;set;}
public string ProductCode{get;set;}
public date StartDate;
}
global class ResponseWrapper{
public String Status;
public String Message;
public String ErrorCode;
public string CustomerNumber;
}
//global static boolean isNotNullorEmpty(Object str) {
//return str != null || !String.isBlank(String.valueOf(str));
//}
@httpPost
global static List<ResponseWrapper> getID(Wrapper1 test){
boolean selector = False;
Map<String,Account> CNnumber = new Map<String,Account>();
List<Account> accList = [Select Id, Customer_Number__c , Account_Credit__c from Account];
for(Account acn : accList){
CNnumber.put(acn.Customer_Number__c, acn);
}
List<ResponseWrapper> responseList = new List<ResponseWrapper>();
Wrapper2 wrp = new Wrapper2();
Map<String,Product__c> prdCode = new Map<String,Product__c>();
List<Product__c> prd = [Select Id, ProductCode__c, Quantity__c, type__c, Price__c from Product__c];
for(Product__c pc : prd){
prdCode.put(pc.ProductCode__c, pc);
}
decimal totalPrice2 = 0;
decimal TotalPrice3 = 0;
Account acc = CNnumber.get(test.CustomerNumber);
List<OrderProduct__c> opList = new List<OrderProduct__c>();
for(Wrapper2 soW : test.wrapper2){
ResponseWrapper response = new ResponseWrapper();
if(prdCode.get(soW.ProductCode) == null && soW.ProductCode !=null){
response.Status = 'Error';
response.Message = 'Invalid Product Code';
response.ErrorCode = 'ERROR 400 : Bad Request';
response.CustomerNumber = 'Invalid';
responseList.add(response);
selector = True;
}
else if(soW.ProductCode == null){
response.Status = 'Error';
response.Message = 'Exception : Required field missing [Product Code]';
response.ErrorCode = 'ERROR 400 : Bad Request';
response.CustomerNumber = 'Invalid';
responseList.add(response);
selector = True;
}
else if (soW.Quantity == null){
response.Status = 'Error';
response.Message = 'Exception : Required field missing [Quantity]';
response.ErrorCode = 'ERROR 400 : Bad Request';
response.CustomerNumber = 'Invalid';
responseList.add(response);
selector = True;
}
else if(prdCode.get(soW.ProductCode).Quantity__c < soW.Quantity){
response.Status = 'Error';
response.Message = 'Not Enough Stock for your Order';
response.ErrorCode = 'ERROR 400 : Bad Request';
response.CustomerNumber = 'Invalid';
responseList.add(response);
selector = True;
}
else if (prdCode.get(soW.ProductCode).type__c == 'Subscription' && soW.StartDate == null){
response.Status = 'Error';
response.Message = 'Exception : Required field missing [Start Date ]';
response.ErrorCode = 'ERROR 400 : Bad Request';
response.CustomerNumber = 'Invalid';
responseList.add(response);
selector = True;
}
//Validation for One time payment
else if(soW.ProductCode != null && soW.Quantity != null){
OrderProduct__c op = new OrderProduct__c();
//op.Subscription_Order__c = so.Id;
op.Subscription_Product__c = prdCode.get(soW.ProductCode).id;
op.Quantity__c = soW.Quantity;
op.Start_Date__c = soW.StartDate;
if(prdCode.get(soW.ProductCode).Type__c == 'One Time Payment'){
op.Subscription__c = 'No';
}
Decimal totalPrice = prdCode.get(soW.ProductCode).Price__c * soW.Quantity;
if(prdCode.get(soW.ProductCode).type__c == 'One Time Payment' && totalPrice > CNnumber.get(test.CustomerNumber).Account_Credit__c)
{
response.Status = 'Error';
response.Message = 'insufficient Credit for this type of Product';
response.ErrorCode = 'ERROR 400 : Bad Request';
response.CustomerNumber = 'Invalid';
responseList.add(response);
selector = True;
system.debug('#####OGVALIDATION' + response.Message);
system.debug('#####' + totalPrice);
}
if(!Selector){
if(prdCode.get(soW.ProductCode).type__c == 'One Time Payment' ){
TotalPrice2 = TotalPrice2 + TotalPrice;
if(TotalPrice2 < acc.Account_Credit__c){
response.Status = 'Order On Hold because of one of the Products';
response.Message = 'Successful';
response.CustomerNumber = test.CustomerNumber;
response.ErrorCode = 'Invalid';
responseList.add(response);
System.debug('#####TOTALPRICE2' + totalPrice2);
System.debug('#####VALIDATION1' + response.Message);
opList.add(op);
}
else{
response.Status = 'Error';
response.Message = 'insufficient Credit for this type of Product';
response.ErrorCode = 'ERROR 400 : Bad Request';
response.CustomerNumber = 'Invalid';
responseList.add(response);
system.debug('#####TOTALPRICE2' + totalPrice2);
SYSTEM.debug('#####VALIDATION2' + response.Message);
Selector = True;
}
//opList.add(op);
}
else if(prdCode.get(soW.ProductCode).type__c == 'Subscription' ){
response.Status = 'Order On Hold because of one of the Products';
response.Message = 'Successful';
response.CustomerNumber = test.CustomerNumber;
response.ErrorCode = 'Invalid';
responseList.add(response);
opList.add(op);
}
else{
TotalPrice3 = TotalPrice3 + TotalPrice;
opList.add(op);
}
}
}
}
System.debug('#####' + TotalPrice2);
if(Selector){
return responseList;
}
else{
ResponseWrapper response = new ResponseWrapper();
Subscription_Order__c so = new Subscription_Order__c();
so.Account__c = CNnumber.get(test.CustomerNumber).id;
insert so;
System.debug('#####1' + so.Id);
for(OrderProduct__c op : opList){
op.Subscription_Order__c = so.Id;
}
if(TotalPrice2 < Acc.Account_Credit__c){
acc.account_credit__c = acc.account_credit__c - totalPrice2;
update acc;
}
/*
response.Status = 'Product Bought';
response.Message = 'Successful';
response.CustomerNumber = test.CustomerNumber;
response.ErrorCode = Null ;
responseList.add(response);
*/
System.debug('#####' + opList);
insert opList;
return responseList;
}
}
}
My problem right now is if all of the products inserted has no error it still throw a response message of Order On Hold
Your help is much appreciated thanks.