0

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;
    }

}
  • why don't you use String.isBlank() or String.isNotBlank() methods? – Novarg Jan 30 '17 at 10:59
1

Seems like you need to validate different type of fields, so use Object as an parameter to make it dynamic.

Method would be like:

global static boolean isNotNullorEmpty(Object str) {
    return str != null && !String.isBlank(String.valueOf(str));
}

And condition would be like as follows:

if(isNotNullorEmpty(Test.AccountID) && 
    isNotNullorEmpty(wrp.Quantity) && 
    isNotNullorEmpty(wrp.ProductCode)){  
    // your logic
}
  • Hi @RahulSharma Thanks for the help, but now my problem is it's only checking if all Product Code or Quantity in my request is null then it will throw the response, but it should throw eventhough 1 product code or Quantity in my request body is null. – JMDF Jan 31 '17 at 2:30
  • Ah I had changed condition from OR to AND in isNotNullorEmpty() – Raul Jan 31 '17 at 6:28
1

You method signature accepts a string as a parameter, and returns a Boolean....

global static boolean isNotNullorEmpty(string str)
{
    return str!=null || !String.isBlank(str);
}

But your error message indicates that you are calling the method with a decimal data type....

Method does not exist or incorrect signature: isNotNullorEmpty(Decimal)

Look through your code and find out where you are calling that method with a decimal data type (maybe here...)

    else if(!isNotNullorEmpty(wrp.Quantity)){
        response.Message = 'Exception : Required field missing [Quantity]';
            response.ErrorCode = 'ERROR 400 : Bad Request';
            response.AccountID = 'Invalid';
    }

That's what's causing your compiler error. Maybe that else if(...) just needs a different condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.