What I need to do is create a validation in my Rest Api class that if a field is null or empty it will throw a response error.
I don't know what's wrong with my code it says Method does not exist or incorrect signature: isNotNullorEmpty(Decimal)
This is my code
Hoping someone can help me,
I also tried not using isNotNullorEmpty
global class but it's not returning properly just a simple Test.AccountID = null
for the fields that require validation but it's not returning properly.
@restResource(urlMapping='/buyproduct/*')
global with sharing class buyProduct{
global with sharing class Wrapper1{
public string AccountID {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 AccountID;
}
global static boolean isNotNullorEmpty(string str)
{
return str!=null || !String.isBlank(str);
}
@httpPost
global static ResponseWrapper getID(Wrapper1 test){
ResponseWrapper response = new ResponseWrapper();
Wrapper2 wrp = new Wrapper2();
if(isNotNullorEmpty(Test.AccountID) && isNotNullorEmpty(wrp.Quantity) && isNotNullorEmpty(wrp.ProductCode)){
Subscription_Order__c so = new Subscription_Order__c();
so.Account__c = test.AccountId;
insert so;
System.debug('#####1' + so.Id);
Map<String,Id> prdCode = new Map<String,Id>();
List<Product__c> prd = [Select Id, ProductCode__c from Product__c];
for(Product__c pc : prd){
prdCode.put(pc.ProductCode__c, pc.Id);
}
List<OrderProduct__c> opList = new List<OrderProduct__c>();
for(Wrapper2 soW : test.wrapper2){
OrderProduct__c op = new OrderProduct__c();
op.Subscription_Order__c = so.Id;
op.Subscription_Product__c = prdCode.get(soW.ProductCode);
op.Quantity__c = soW.Quantity;
op.Start_Date__c = soW.StartDate;
opList.add(op);
response.Status = 'Create Account Complete';
response.Message = 'Successful';
response.ErrorCode = 'Invalid';
}
insert opList;
}
else if(!isNotNullorEmpty(test.AccountId)){
response.Status = 'Error';
response.Message = 'Exception : Required field missing [AccountID]';
response.ErrorCode = 'ERROR 400 : Bad Request';
response.AccountID = 'Invalid';
}
else if(!isNotNullorEmpty(wrp.Quantity)){
response.Message = 'Exception : Required field missing [Quantity]';
response.ErrorCode = 'ERROR 400 : Bad Request';
response.AccountID = 'Invalid';
}
else if (!isNotNullorEmpty(wrp.ProductCode)){
response.Message = 'Exception : Required field missing [Product Code]';
response.ErrorCode = 'ERROR 400 : Bad Request';
response.AccountID = 'Invalid';
}
return response;
}
}
String.isBlank()
orString.isNotBlank()
methods? – Novarg Jan 30 '17 at 10:59