+ Start a Discussion
pranav shah 40pranav shah 40 

WrapperResponse Issue

Hi All,

Below is my code to insert opportunty on basis of custom object(Cable unit) in salesforce. record is getting inserted.. but in response i am getting some issue... response is added at the end... i am fetching response throgh workbench

Also i dont want opportunity to be created,if their is no cable unit created

@RestResource(urlMapping='/OpportunityCreation/*')
global class OpportunityCreate
{
@HttpPost   
    global static  List<responseWrapper> createNewOpportunity(list<OpportunityWrapper> oppwrapList) 
    {
        //RestRequest req = RestContext.request;
        //RestResponse res = RestContext.response;
        string returnString='';
        string cuNo;
        List<responseWrapper> response = new List<responseWrapper>();


        //set<Id> setNewOpp=new set<Id>();
        try
        {
            list<Opportunity> opplist = new list<Opportunity>();
            Map<String,Id> cableUnitNoToIdMap=new Map<String,Id>();
            Map<String,id> cableUnitNoToAccountIdMap=new Map<String,id>();
            Set<String> cableUnitSet=new Set<String>();
            list<Database.SaveResult> srList;
            list<String> kissCUNos = new list<String>();
            list<String> successkissCUNos = new list<String>();


              for(OpportunityWrapper wrapObj:oppWrapList)
              {
                  cableUnitSet.add(wrapObj.cableUnitNo); // heck variable name in wrapper class 
              }
              system.debug('--------------------cableUnitSet-----------------'+cableUnitSet);
              list<Cable_Unit__c> cableUnitRecords = [select Id,Customer__r.Id,Customer__c,Cable_Unit_No__c from Cable_Unit__c where Cable_Unit_No__c IN :cableUnitSet];
              system.debug('-----------cableUnitRecords Size-------------'+cableUnitRecords.size());
             for(Cable_Unit__c c:cableUnitRecords)
              {
                  system.debug('------------CU record-----------------'+c);
                  cableUnitNoToIdMap.put(c.Cable_Unit_No__c,c.Id);
                  //cableUnitNoToAccountIdMap.put(c.Cable_Unit_No__c, c.Customer__c);
              }
              List<Account> AccountRecords=[select Id,Name,Cable_Unit_No__c from Account where Cable_Unit_No__c IN:cableUnitSet];
              for(Account acct:AccountRecords)
              {
                   system.debug('------------Customer record-----------------'+acct);
                   //cableUnitNoToIdMap.put(acct.Cable_Unit_No__c,acct.Id);
                   cableUnitNoToAccountIdMap.put(acct.Cable_Unit_No__c, acct.id);
              }
              system.debug('--------------------cableUnitNoToIdMap size-----------------------------'+cableUnitNoToIdMap.size());
              system.debug('--------------------cableUnitNoToIdMap-----------------------------'+cableUnitNoToIdMap);
              system.debug('--------------------cableUnitNoToAccountIdMap size-----------------------------'+cableUnitNoToAccountIdMap.size());
              system.debug('--------------------cableUnitNoToAccountIdMap -----------------------------'+cableUnitNoToAccountIdMap);
                for(OpportunityWrapper wrap : oppwrapList) 
                {           
                    
                    Opportunity opp = new Opportunity();
                    opp.Name= wrap.Name;
                    opp.StageName =wrap.StageName;
                    opp.CloseDate = date.valueof(Label.CloseDate);
                    //customLabelValue = System.Label.CloseDate;
                    //opp.CloseDate =date.valueof(wrap.closedate);
                  
                    if(cableUnitNoToIdMap.containskey(wrap.cableUnitNo))
                    { 
                        opp.cable_unit__c=cableUnitNoToIdMap.get(wrap.cableUnitNo);
                    }
                    if(cableUnitNoToAccountIdMap.containskey(wrap.cableUnitNo)) 
                    {
                        opp.AccountId =cableUnitNoToAccountIdMap.get(wrap.cableUnitNo);
                        system.debug('---------------cableUnitNoToAccountIdMap.get(wrap.cableUnitNo);--------------------'+cableUnitNoToAccountIdMap.get(wrap.cableUnitNo));
                        system.debug('---------------opp.AccountId--------------------'+opp.AccountId);
                    }
                    opplist.add(opp);
                    system.debug('*****' +  oppwrapList);
                }
                 {
                   srList = Database.insert(opplist,false);
                 }
               //create response for all the CUs'for which Opportunity and CU record has been successfully inserted in SF Database.

               for(Opportunity insertedopportunity : opplist)
               {
               
                successkissCUNos.add(cableUnitNoToIdMap.get(insertedopportunity.cable_unit__c));
                responseWrapper responseRec = new responseWrapper();
                responseRec.cableUnitNo = cableUnitNoToIdMap.get(insertedopportunity.cable_unit__c);
                responseRec.cableUnitSFID =insertedopportunity.cable_unit__c;
                responseRec.OpportunitySFID = insertedOpportunity.id;
                responseRec.IsSuccess = 'true';
                response.add(responseRec);
               }
           
            //these condition checks the Opp records for which opp and CU records are not created in SF.
            
                if(!successkissCUNos.contains(CuNo))
                {
                    responseWrapper responseError = new responseWrapper();
                    responseError.IsError = 'true'; 
                    responseError.CableUnitNo = CuNo;
                    responseError.ErrorMessage = 'Cable Unit and Opportunity Record Record Creation failed In Salesforce';
                    response.add(responseError);
                }
            
            system.debug('------response---------'+response);                
        }
        catch(exception ex)
        {
            returnString = 'Request got failed: Error details-'+ex.getmessage();
        }
         return response;
    }
    
    
   global class OpportunityWrapper
       {
        global string StageName;
        global string CloseDate;
        global string CableUnitNo;
        global string name;
       }
   global class responseWrapper
       {
        global String cableUnitNo;
        global String OpportunitySFID;
        global String cableUnitSFID;
        //global String AccountSFID;
        global String IsSuccess;
        global String IsError;
        global String ErrorMessage;
       }    
}

But while inserting through workbench i am not getting cable number in response...


[
{
"OpportunitySFID" : "006M000000FaLTQIA3",
"IsSuccess" : "true", "IsError" : null,
"ErrorMessage" : null,
"cableUnitSFID" : "a1TM0000001yrZwMAI",
"cableUnitNo" : null
},
 {
"OpportunitySFID" : "006M000000FaLTRIA3",
"IsSuccess" : "true",
"IsError" : null,
"ErrorMessage" : null,
"cableUnitSFID" : "a1TM0000001yrZxMAI",
"cableUnitNo" : null
} ]


please suggest....
Raj VakatiRaj Vakati
use this code
 
@RestResource(urlMapping='/OpportunityCreation/*')
global class OpportunityCreate
{
@HttpPost   
    global static  List<responseWrapper> createNewOpportunity(list<OpportunityWrapper> oppwrapList) 
    {
        //RestRequest req = RestContext.request;
        //RestResponse res = RestContext.response;
        string returnString='';
        string cuNo;
        List<responseWrapper> response = new List<responseWrapper>();


        //set<Id> setNewOpp=new set<Id>();
        try
        {
            list<Opportunity> opplist = new list<Opportunity>();
            Map<String,Id> cableUnitNoToIdMap=new Map<String,Id>();
            Map<String,id> cableUnitNoToAccountIdMap=new Map<String,id>();
            Set<String> cableUnitSet=new Set<String>();
            list<Database.SaveResult> srList;
            list<String> kissCUNos = new list<String>();
            list<String> successkissCUNos = new list<String>();


              for(OpportunityWrapper wrapObj:oppWrapList)
              {
                  cableUnitSet.add(wrapObj.cableUnitNo); // heck variable name in wrapper class 
              }
              system.debug('--------------------cableUnitSet-----------------'+cableUnitSet);
              list<Cable_Unit__c> cableUnitRecords = [select Id,Customer__r.Id,Customer__c,Cable_Unit_No__c from Cable_Unit__c where Cable_Unit_No__c IN :cableUnitSet];
              system.debug('-----------cableUnitRecords Size-------------'+cableUnitRecords.size());
             for(Cable_Unit__c c:cableUnitRecords)
              {
                  system.debug('------------CU record-----------------'+c);
                  cableUnitNoToIdMap.put(c.Cable_Unit_No__c,c.Id);
                  //cableUnitNoToAccountIdMap.put(c.Cable_Unit_No__c, c.Customer__c);
              }
              List<Account> AccountRecords=[select Id,Name,Cable_Unit_No__c from Account where Cable_Unit_No__c IN:cableUnitSet];
              for(Account acct:AccountRecords)
              {
                   system.debug('------------Customer record-----------------'+acct);
                   //cableUnitNoToIdMap.put(acct.Cable_Unit_No__c,acct.Id);
                   cableUnitNoToAccountIdMap.put(acct.Cable_Unit_No__c, acct.id);
              }
              system.debug('--------------------cableUnitNoToIdMap size-----------------------------'+cableUnitNoToIdMap.size());
              system.debug('--------------------cableUnitNoToIdMap-----------------------------'+cableUnitNoToIdMap);
              system.debug('--------------------cableUnitNoToAccountIdMap size-----------------------------'+cableUnitNoToAccountIdMap.size());
              system.debug('--------------------cableUnitNoToAccountIdMap -----------------------------'+cableUnitNoToAccountIdMap);
                for(OpportunityWrapper wrap : oppwrapList) 
                {           
                    
                    Opportunity opp = new Opportunity();
                    opp.Name= wrap.Name;
                    opp.StageName =wrap.StageName;
                    opp.CloseDate = date.valueof(Label.CloseDate);
                    //customLabelValue = System.Label.CloseDate;
                    //opp.CloseDate =date.valueof(wrap.closedate);
                  
                    if(cableUnitNoToIdMap.containskey(wrap.cableUnitNo))
                    { 
                        opp.cable_unit__c=cableUnitNoToIdMap.get(wrap.cableUnitNo);
                    }
                    if(cableUnitNoToAccountIdMap.containskey(wrap.cableUnitNo)) 
                    {
                        opp.AccountId =cableUnitNoToAccountIdMap.get(wrap.cableUnitNo);
                        system.debug('---------------cableUnitNoToAccountIdMap.get(wrap.cableUnitNo);--------------------'+cableUnitNoToAccountIdMap.get(wrap.cableUnitNo));
                        system.debug('---------------opp.AccountId--------------------'+opp.AccountId);
                    }
                    opplist.add(opp);
                    system.debug('*****' +  oppwrapList);
                }
                 {
                   srList = Database.insert(opplist,false);
                 }
               //create response for all the CUs'for which Opportunity and CU record has been successfully inserted in SF Database.

               for(Opportunity insertedopportunity : opplist)
               {
               
                successkissCUNos.add(cableUnitNoToIdMap.get(insertedopportunity.cable_unit__c));
                responseWrapper responseRec = new responseWrapper();
                responseRec.cableUnitNo = cableUnitNoToIdMap.get(insertedopportunity.cable_unit__c);
                responseRec.cableUnitSFID =insertedopportunity.cable_unit__c;
                responseRec.OpportunitySFID = insertedOpportunity.id;
                responseRec.IsSuccess = 'true';
                response.add(responseRec);
               }
           
            //these condition checks the Opp records for which opp and CU records are not created in SF.
            
                if(!successkissCUNos.contains(CuNo))
                {
                    responseWrapper responseError = new responseWrapper();
                    responseError.IsError = 'true'; 
                    responseError.CableUnitNo = CuNo;
                    responseError.ErrorMessage = 'Cable Unit and Opportunity Record Record Creation failed In Salesforce';
                    response.add(responseError);
                }
            
            system.debug('------response---------'+response);                
        }
        catch(exception ex)
        {
            returnString = 'Request got failed: Error details-'+ex.getmessage();
        }
         return response;
    }
    
    
   global class OpportunityWrapper
       {
        global string StageName{get;set;}
        global string CloseDate{get;set;}
        global string CableUnitNo{get;set;}
        global string name{get;set;}
       }
   global class responseWrapper
       {
        global String cableUnitNo{get;set;}
        global String OpportunitySFID{get;set;}
        global String cableUnitSFID{get;set;}
        //global String AccountSFID;
        global String IsSuccess{get;set;}
        global String IsError{get;set;}
        global String ErrorMessage{get;set;}
       }    
}

 
Raj VakatiRaj Vakati
wrapper class should be
 
global class OpportunityWrapper
       {
        global string StageName{get;set;}
        global string CloseDate{get;set;}
        global string CableUnitNo{get;set;}
        global string name{get;set;}
       }
   global class responseWrapper
       {
        global String cableUnitNo{get;set;}
        global String OpportunitySFID{get;set;}
        global String cableUnitSFID{get;set;}
        //global String AccountSFID;
        global String IsSuccess{get;set;}
        global String IsError{get;set;}
        global String ErrorMessage{get;set;}
       }

 
Raj VakatiRaj Vakati
I dnt understand the requirement .. can you please explan and here is the code to check cableUnitNo
 
@RestResource(urlMapping='/OpportunityCreation/*')
global class OpportunityCreate
{
@HttpPost   
    global static  List<responseWrapper> createNewOpportunity(list<OpportunityWrapper> oppwrapList) 
    {
        //RestRequest req = RestContext.request;
        //RestResponse res = RestContext.response;
        string returnString='';
        string cuNo;
        List<responseWrapper> response = new List<responseWrapper>();


        //set<Id> setNewOpp=new set<Id>();
        try
        {
            list<Opportunity> opplist = new list<Opportunity>();
            Map<String,Id> cableUnitNoToIdMap=new Map<String,Id>();
            Map<String,id> cableUnitNoToAccountIdMap=new Map<String,id>();
            Set<String> cableUnitSet=new Set<String>();
            list<Database.SaveResult> srList;
            list<String> kissCUNos = new list<String>();
            list<String> successkissCUNos = new list<String>();


              for(OpportunityWrapper wrapObj:oppWrapList)
              {
                  cableUnitSet.add(wrapObj.cableUnitNo); // heck variable name in wrapper class 
              }
              system.debug('--------------------cableUnitSet-----------------'+cableUnitSet);
              list<Cable_Unit__c> cableUnitRecords = [select Id,Customer__r.Id,Customer__c,Cable_Unit_No__c from Cable_Unit__c where Cable_Unit_No__c IN :cableUnitSet];
              system.debug('-----------cableUnitRecords Size-------------'+cableUnitRecords.size());
             for(Cable_Unit__c c:cableUnitRecords)
              {
                  system.debug('------------CU record-----------------'+c);
                  cableUnitNoToIdMap.put(c.Cable_Unit_No__c,c.Id);
                  //cableUnitNoToAccountIdMap.put(c.Cable_Unit_No__c, c.Customer__c);
              }
              List<Account> AccountRecords=[select Id,Name,Cable_Unit_No__c from Account where Cable_Unit_No__c IN:cableUnitSet];
              for(Account acct:AccountRecords)
              {
                   system.debug('------------Customer record-----------------'+acct);
                   //cableUnitNoToIdMap.put(acct.Cable_Unit_No__c,acct.Id);
                   cableUnitNoToAccountIdMap.put(acct.Cable_Unit_No__c, acct.id);
              }
              system.debug('--------------------cableUnitNoToIdMap size-----------------------------'+cableUnitNoToIdMap.size());
              system.debug('--------------------cableUnitNoToIdMap-----------------------------'+cableUnitNoToIdMap);
              system.debug('--------------------cableUnitNoToAccountIdMap size-----------------------------'+cableUnitNoToAccountIdMap.size());
              system.debug('--------------------cableUnitNoToAccountIdMap -----------------------------'+cableUnitNoToAccountIdMap);
                for(OpportunityWrapper wrap : oppwrapList) 
                {           
                    
                    Opportunity opp = new Opportunity();
                    opp.Name= wrap.Name;
                    opp.StageName =wrap.StageName;
                    opp.CloseDate = date.valueof(Label.CloseDate);
                    //customLabelValue = System.Label.CloseDate;
                    //opp.CloseDate =date.valueof(wrap.closedate);
                  
                    if(cableUnitNoToIdMap.containskey(wrap.cableUnitNo))
                    { 
                        opp.cable_unit__c=cableUnitNoToIdMap.get(wrap.cableUnitNo);
                    }
                    if(cableUnitNoToAccountIdMap.containskey(wrap.cableUnitNo)) 
                    {
                        opp.AccountId =cableUnitNoToAccountIdMap.get(wrap.cableUnitNo);
                        system.debug('---------------cableUnitNoToAccountIdMap.get(wrap.cableUnitNo);--------------------'+cableUnitNoToAccountIdMap.get(wrap.cableUnitNo));
                        system.debug('---------------opp.AccountId--------------------'+opp.AccountId);
                    }
                    opplist.add(opp);
                    system.debug('*****' +  oppwrapList);
                }
                 {
                   srList = Database.insert(opplist,false);
                 }
               //create response for all the CUs'for which Opportunity and CU record has been successfully inserted in SF Database.

               for(Opportunity insertedopportunity : opplist)
               {
               
                successkissCUNos.add(cableUnitNoToIdMap.get(insertedopportunity.cable_unit__c));
                responseWrapper responseRec = new responseWrapper();
			   If(insertedopportunity.cable_unit__c!=null){
                responseRec.cableUnitNo = cableUnitNoToIdMap.get(insertedopportunity.cable_unit__c);
			   }else{
				    responseRec.cableUnitNo ='0';
			   }
                responseRec.cableUnitSFID =insertedopportunity.cable_unit__c;
                responseRec.OpportunitySFID = insertedOpportunity.id;
                responseRec.IsSuccess = 'true';
                response.add(responseRec);
               }
           
            //these condition checks the Opp records for which opp and CU records are not created in SF.
            
                if(!successkissCUNos.contains(CuNo))
                {
                    responseWrapper responseError = new responseWrapper();
                    responseError.IsError = 'true'; 
                    responseError.CableUnitNo = CuNo;
                    responseError.ErrorMessage = 'Cable Unit and Opportunity Record Record Creation failed In Salesforce';
                    response.add(responseError);
                }
            
            system.debug('------response---------'+response);                
        }
        catch(exception ex)
        {
            returnString = 'Request got failed: Error details-'+ex.getmessage();
        }
         return response;
    }
    
    
   global class OpportunityWrapper
       {
        global string StageName{get;set;}
        global string CloseDate{get;set;}
        global string CableUnitNo{get;set;}
        global string name{get;set;}
       }
   global class responseWrapper
       {
        global String cableUnitNo{get;set;}
        global String OpportunitySFID{get;set;}
        global String cableUnitSFID{get;set;}
        //global String AccountSFID;
        global String IsSuccess{get;set;}
        global String IsError{get;set;}
        global String ErrorMessage{get;set;}
       }    
}