0

Need help in catching error message and displaying status as failure in the response, when id is not generated.

Apex Code :

  @HttpPost
  global static List<SamplesResponse> doPost()
  {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;

        system.debug('-----------data--------'+req.requestBody.toString()) ;

        list<SamplesAccount> samplesAccList = (list<SamplesAccount>)JSON.deserialize(req.requestBody.toString(),list<SamplesAccount>.class);

        system.debug('----------samplesAccList----------'+samplesAccList);
        List<SamplesResponse> returnList = new List<SamplesResponse>();  
        List<Account> samplesList = new List<Account>();

        RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];


        if(!samplesAccList.isEmpty())
        {

            for(SamplesAccount mas: samplesAccList)
            { 
                    Account acc = new Account();

                    if(mas.SamplesID!=null && mas.SamplesID!='')    
                        acc.Samples__c = mas.SamplesID; 
                    acc.firstname= mas.FName;                
                    acc.lastname= mas.LName;               
                    acc.Phone= mas.Phone;
                    acc.Email__c= string.valueof(mas.Email);                
                    acc.Billing_City__c = string.valueof(mas.BillingCity);                
                    acc.Billing_State_Province__c = string.valueof(mas.BillingStateProvince);                
                    acc.Billing_Zip_Postal_Code__c = string.valueof(mas.BillingZip);               
                    acc.RecordTypeId = personAccountRecordType.id;
                    acc.Customer_Class__c = '(SA) Samples';
                samplesList.add(acc);
            }   

        }
                system.debug('**samples list**'+samplesList);

           if(!samplesList.isEmpty()){
              string errMessage;
              try {
                  Database.UpsertResult[] upsertResult = Database.upsert(samplesList,Account.Email__c,false);
                  system.debug('@@@@Samples List@@@@@'+upsertResult );
                  }
              catch(Exception e)  // exception handling
                {
                System.debug('The following exception has occurred: '+e.getTypeName() + ' - ' + e.getCause() + ': ' + e.getMessage());
                errMessage = e.getMessage();
                }  
                /*for(Account samples : samplesList){
                    SamplesResponse sp = new SamplesResponse(samples,errMessage);
                    returnList.add(sp);
                } */
                for (Database.upsertResult upsertResult  : samplesList) {
                        if (upsertResult.isSuccess()) {
                            status = 'Success';
                            sf_id = insertedSamples.id;
                            samplesID = insertedSamples.Samples__c;
                            email = insertedSamples.Email__c;
                        }
                        else {
                            // Operation failed, so get all errors                
                            for(Database.Error err : samplesList.getErrors()) {

                        }
                        }
                    }


             }  
 return returnList;      

Wrapper Class:

   global class SamplesResponse{
        public String samplesID;
        public String email;
        public String status;
        public String error;
        public String sf_id;

    public SamplesResponse(Account insertedSamples,String statusMsg){
    system.debug('$$$Response caught$$'+insertedSamples+'$$$$'+statusMsg);
    if(statusMsg!=null){
        status = 'Failure';
        error = statusMsg;
    }
    else{
        status = 'Success';
        sf_id = insertedSamples.id;
        samplesID = insertedSamples.Samples__c;
        email = insertedSamples.Email__c;  
    }

    }
   }
1
  • You do not have anything in your code that would tell you it failed. When you upset you need to be iterating for the upsertresult and checking the isSuccess method. If you find one that is false then you can handle it accordingly
    – Eric
    Dec 27, 2016 at 7:06

1 Answer 1

0

After inserting loop through upsertResult and built your SamplesResponse.

for (Database.upsertResult sr  : upsertResult) {
            if (sr.isSuccess()) {
                //do nothing
            }
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : sr.getErrors()) {
                //built your Sampleresponse
            }
    }
2
  • Still facing some issue (variable dosent found)
    – B1 sfdc
    Dec 27, 2016 at 9:06
  • Can you check now I have updated my answer I had made an mistake while coding.
    – Shankish
    Dec 27, 2016 at 11:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .