You need to sign in to do that
Don't have an account?
I have one rest resource class where in the post method i need to return response in json format.
Below is my program :-
@RestResource(URLMapping='/SendEmailForOTP/*')
global class WordpressIntegrationForEmail {
@HTTPPOST
global static FinalReturnWrapper SendEmailMsg(string email , string otp,string mob,boolean send_otp_if_exists)
{
FinalReturnWrapper returnResponse = new FinalReturnWrapper();
cls_data objData = new cls_data();
contact_data cobjData = new contact_data();
string failed = 'Email not exist'; //Need to create custom label
string success = 'Email successfully sent to email address' ;//Need to create custom label
list<contact> conList = [Select id,Do_Not_Mail__c,Email_Verification_Status__c, Email_Verification_Status_Date__c from contact where email =: email Limit 1];
if(conList.size() > 0 ) // If the email id is exist then ...
{
objData.contact_exists = true;
for(contact c : conList)
{
objData.contact_id =c.id;
cobjData.name=c.Name;
if(c.Do_Not_Mail__c == false && c.Email_Verification_Status__c == null && c.Email_Verification_Status_Date__c == null)
{
OtpViaEmail.sendEmail(email,otp);
objData.otp_sms_sent = true;
objData.otp_email_sent = true;
}
}
returnResponse.obj = objData;
returnResponse.cobj= cobjData ;
return returnResponse;
}
else // If the email id not exist then...
{
if(send_otp_if_exists ==false )
{
OtpViaEmail.sendEmail(email,otp);
objData.otp_sms_sent = true;
objData.otp_email_sent = true;
return returnResponse;
}
}
}
global class FinalReturnWrapper {
public cls_data obj ;
public contact_data cobj;
}
public class cls_data {
public boolean contact_exists;
public boolean otp_sms_sent;
public boolean otp_email_sent;
public string contact_id;
}
public class contact_data {
public string name;
}
}
See here i need to return FinalReturnWrapper but when i called this from postman so getting error as parser error.
So can someone please let me know how can I change the response to json file. Please guid me the code.
You can use the JSON generator to return the response as JSON. Please see the sample code below-
Please refer the link for more details- JSON Generator (https://developer.salesforce.com/docs/atlas.en-us.242.0.apexref.meta/apexref/apex_class_System_JsonGenerator.htm)
Let me know if this helps!
Thanks,
Shivdeep