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
Bilal 25Bilal 25 

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.
@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;
    }    
}

 
Best Answer chosen by Bilal 25
Santosh Kumar 348Santosh Kumar 348
Hi Moh,

I feel there are few changes required at your code.
  1. First of all as you want to return the error if email already exist, so change the return type to String instead of ID.
  2. Please add your else body inside curly braces, as your code after the first line of else will still get executed. 

So method with above changes may work for you. refer the below code for same.
 
global static String 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){
		return '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 String.ValueOf(thislead.Id);
	}
}

Regards,
Santosh​​​​​​​

 

All Answers

Santosh Kumar 348Santosh Kumar 348
Hi Moh,

Try to print thislead.lastName before insterting and see if you are getting the value or not.
 
System.debug('thislead.lastName : ' + thislead.lastName );

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
 
Bilal 25Bilal 25
Hi santosh,

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"
          }
Santosh Kumar 348Santosh Kumar 348
Could you please add a debug statement as well, as I have mentioned previously. Just add debug above " insert thislead;" and see what value you are getting for lastname.
Bilal 25Bilal 25
It returns the value

16:20:03:024 USER_DEBUG [35]|DEBUG|Last name valueBilal ref lead1
Bilal 25Bilal 25
16:25:33:022 USER_DEBUG [35]|DEBUG|thislead.lastName : null
Bilal 25Bilal 25
How to solve this?
Santosh Kumar 348Santosh Kumar 348
Why you have got 2 different value 
16:20:03:024 USER_DEBUG [35]|DEBUG|Last name valueBilal ref lead1
 
16:25:33:022 USER_DEBUG [35]|DEBUG|thislead.lastName : null

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.
Santosh Kumar 348Santosh Kumar 348
Hi Moh,

I feel there are few changes required at your code.
  1. First of all as you want to return the error if email already exist, so change the return type to String instead of ID.
  2. Please add your else body inside curly braces, as your code after the first line of else will still get executed. 

So method with above changes may work for you. refer the below code for same.
 
global static String 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){
		return '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 String.ValueOf(thislead.Id);
	}
}

Regards,
Santosh​​​​​​​

 
This was selected as the best answer
Bilal 25Bilal 25
Its inserting after i give some space before string email.
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 
Bilal 25Bilal 25
Thank you very much santosh. Its help me alot :)