function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pranavshahpranavshah 

passing a creation of record id after a record gets inserted or error message in apex class in salesforce

Hi All,

I need to pass a record id, when  a record gets inserted ...For EG if i have  a contact object, and their is  status field on contact object... so when a contact gets inserted via apex class. instead of passing string as succesful or failure.. i want to display salesforce id of record which gets created and also the value of status field...when i am doing testing through work bench,,,,
below is the sample code for the same

 if (accList <> null && acclist.size() > 0)
            {
             returnString='Successful';
            }
             else
            {
            returnString='Failed';
            }        
            }
        catch(exception ex)
        {
            returnString = 'Request got failed: Error details-'+ex.getmessage();
        }
         return returnString;
    }

please help..
Regards
Pranav
Niraj Kr SinghNiraj Kr Singh
Hi Pranav,
 There are two way you can achive. Because you are processing bulk record, you will get value in list
1.  You can create list of wrapper class where this class have record id and status. And method will return list of wrapper.
//  inner class... Also called wrapper class
Public class wrapperclass {
       Public ID recordID;
       Public string status;
       Public wrapperclass(Id recid, string st){
             recordID = recid;   status = St;
        }
}

 if (accList <> null && acclist.size() > 0)
  {       
          Insert accList;
          List<wrapperclass> lstwrapper = new List<wrapperclass>();
             For(account objacc: acclist){
                    lstwrapper.add(new wrapperclass (objacc.id, objacc.status);
              }
              return lstwrapper;
    }
Else{
     return null;
}
************************************
2 . You can create a map of record id and status. And return this map. Like this:
 if (accList <> null && acclist.size() > 0)
  {       
          Insert accList;
          Map<Id, String> lstwrapper = new Map<Id,. String>();
             For(account objacc: acclist){
                    lstwrapper.put(objacc.id, objacc.status);
              }
              return lstwrapper;
    }
Else{
     return null;
}

Hope this will help you !!
Niraj Kr SinghNiraj Kr Singh
Hi Pranav,
You just use this line of code while returning lstwrapper. Because current return value is not a string. You should convert into string. Like this:
​return JSON.serialize(lstwrapper);
 
Mark your ans if it solves ur problem.