You need to sign in to do that
Don't have an account?

REST API - Insertion error
Hello Folks,
I can't post record from workbench.
Recieving below error message:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName] Class.ReferalProgram.createlead: line 35, column 1
Appreaciate any help can provide.
I can't post record from workbench.
Recieving below error message:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName] Class.ReferalProgram.createlead: line 35, column 1
Appreaciate any help can provide.
@RestResource(urlMapping='/referrallead/*') global with sharing class ReferalProgram { @HttpGet global static lead getLeadById() { // global static opportunity getOpportunityById() { RestRequest request = RestContext.request; String leadId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1); lead result = [SELECT lastname,firstname,email,Phone,status FROM lead WHERE Id = :leadId]; return result; } @HttpPost global static ID createlead(String firstname, String lastname,String email, String phone, String company, String leadsource, String origin, String reffname, String reflname,String rectype) { lead thislead = new lead(); List<Lead> leadlist = [select id,lastname,email from lead where email =: email]; contact thiscontact = new contact(); List<Contact> conList = [select id,name,email from Contact where email =: email]; if(leadlist.size() >0 || conList.size() >0){ thislead.Email.addError('Email already exists'); } else thislead.Lastname = lastname; thislead.Firstname = firstname; thislead.Email = email; thislead.Company = company; thislead.phone = phone; thislead.leadsource = leadsource; thislead.origin__c = origin; thislead.Referrer_First_Name__c = reffname; thislead.Referrer_Last_Name__c = reflname; thislead.RecordTypeId = '012D00000007Q8D'; insert thislead; return thislead.Id; } @HttpPatch global static ID updateleadFields() { RestRequest request = RestContext.request; String leadId = request.requestURI.substring( request.requestURI.lastIndexOf('/')+1); lead thislead = [SELECT Id FROM lead WHERE Id = :leadId]; // Deserialize the JSON string into name-value pairs Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring()); // Iterate through each parameter field and value for(String fieldName : params.keySet()) { // Set the field and value on the Lead sObject thislead.put(fieldName, params.get(fieldName)); } update thislead; return thislead.Id; } }
I feel there are few changes required at your code.
So method with above changes may work for you. refer the below code for same.
Regards,
Santosh
All Answers
Try to print thislead.lastName before insterting and see if you are getting the value or not.
because as the error states there is high chance that you are not passing the lastname from workbench, if is it so then please add the LastName when you are invoking POST method. As LastName is mandatory field it won't allow you to insert "Lead" with blank LastName.
If it helped then can you please mark it as the best answer so that it can be used by others in the future.
Regards,
Santosh Kumar
Thanks for your response.
I passed below parameters from workbench
{
"lastname" : "Bilal ref lead1",
"firstname" : "Bilal ref lead1",
"email" : "bilal@test.ae",
"company" : "leadfandlname",
"phone" : "0554056890",
"leadsource" : "Referral",
"origin" : "Website",
"reffname" : "Bilalfname",
"reflname" : "Bilallname"
}
16:20:03:024 USER_DEBUG [35]|DEBUG|Last name valueBilal ref lead1
can you plese let me know the data you used in both the call, as in second debug you have got the null value so it is obvious that it will throw an error.
I feel there are few changes required at your code.
So method with above changes may work for you. refer the below code for same.
Regards,
Santosh
global static ID createlead(String firstname, String lastname, String email, String phone, String company, String leadsource, String origin, String reffname, String reflname,String rectype)
Conditional block won't check for duplicates. If any duplicates standard duplicate throw error but i need from this code. Can you help.
Thanks