-3

I have the Rest API that will return id if Lead/Contact exits in Salesforce with emailid using get if record not exists then create a new Lead for that record .I write the follwing code

 @RestResource(urlMapping='/1.0/Persons/*')

   global class SQUser{

    @HttpGet

   global static Void getLeadContact(){
     String EmailID = RestContext.request.params.get('Email_id');
     LIST<Contact> conID = [Select ID from Contact where Email=:EmailID LIMIT 1];
     LIST<Lead>    ledID = [Select ID from Lead where Email=:EmailID LIMIT 1];
     if(conID.size()>0){
      RestContext.response.addHeader('Content-Type', 'application/json');
     // Return a single Contact as an example
      RestContext.response.responseBody = 
 Blob.valueOf(JSON.serialize((Contact)[Select ID from Contact where Email=:EmailID

 LIMIT 1]));

 }

  else if(ledID.size()>0){

 RestContext.response.addHeader('Content-Type', 'application/json');

 // Return a single Lead as an example

 RestContext.response.responseBody = 

 Blob.valueOf(JSON.serialize((Lead)[Select ID from Lead where Email=:EmailID LIMIT 
   1]));
  }else{

RestContext.response.addHeader('Content-Type', 'application/json');

 // Return a single Null as an example

 RestContext.response.responseBody = 

 Blob.valueOf(JSON.serialize((null)));

   }

  }

 @HttpPost

 global static void CreateRecord(){

//Initlize the lead object

Lead ledID = new Lead();

//Get requested parameter one by one

ledID.FirstName = RestContext.request.params.get('First_nm');

ledID.LastName = RestContext.request.params.get('Last_nm');

ledID.Phone = RestContext.request.params.get('Phone');

ledID.Email = RestContext.request.params.get('Email');

ledID.Company= RestContext.request.params.get('Company');

// insert lead record in salesforce 

insert ledID;

RestContext.response.addHeader('Content-Type', 'application/json');

 // Return a single Lead as an example

 RestContext.response.responseBody = 

 Blob.valueOf(JSON.serialize((Lead)[Select ID from Lead where ID=:ledID.ID]));

 } 
}

But i test HttpPost method using follwing code then got the error message " Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName, Company]: [LastName, Company]"

   @isTest

 private class SQUserTest {

 static {

     // setup test data  

    }

static testmethod void TestCreateRecord(){
RestRequest req = new RestRequest();

RestResponse res = new RestResponse();

// pass the req and resp objects to the method   

req.requestURI = 'https://ap1.salesforce.com/services/apexrest/1.0/Persons?

 First_nm=Arun&Last_nm=kumar&Phone=97757587&Email=kumar@gmail.com&Company=GL';

req.httpMethod = 'POST';

RestContext.request = req;

RestContext.response = res;

SQUser.CreateRecord();

System.debug('Insert executed successfully.');

 }
}
  • Have you test to display the parameters in your CreateRecord() method ? – SF_user Aug 11 '14 at 12:08
0

try this one

@RestResource(urlMapping='/Persons/*')
global class SQUser{   
    @HttpPost
    global static void CreateRecord(String First_nm,String Last_nm,String Phone,String Email,String Company){        
        //Initlize the lead object        
        Lead ledID = new Lead();

        //Get requested parameter one by one       
        ledID.FirstName = First_nm;
        System.debug('FirstName'+ledID.FirstName);

        ledID.LastName = Last_nm;
        System.debug('LastName'+ledID.LastName);

        ledID.Phone = Phone;

        ledID.Email = Email;

        ledID.Company= Company;
        System.debug('Company'+ledID.Company);

        // insert lead record in salesforce         
        insert ledID;

        RestContext.response.addHeader('Content-Type', 'application/json');

        // Return a single Lead as an example       
        RestContext.response.responseBody =             
            Blob.valueOf(JSON.serialize((Lead)[Select ID from Lead where ID=:ledID.ID]));        
    }
}

Test Class

@isTest
private class SQUserTest {   
    static {       
        // setup test data        
    }

    static testmethod void TestCreateRecord(){
        RestRequest req = new RestRequest();

        RestResponse res = new RestResponse();

        // pass the req and resp objects to the method           
        String json ='{"First_nm" : "","Last_nm"  : "arjun","Phone"    : "1234567890","Email"    : "xxxx@gmail.com","Company": "xxx"}';

        req.requestURI = 'https://ap1.salesforce.com/services/apexrest/1.0/Persons/
        req.httpMethod = 'POST';

        req.requestBody = Blob.valueof(json);

        RestContext.request = req;

        RestContext.response = res;

        SQUser.CreateRecord('First','Last','Phone','Email','Company');

        System.debug('Insert executed successfully.');        
    }
}

Thanks Nagarjuna

  • nagarjuna -- on SFSE, we try and avoid reminding the original poster to make our solutions as accepted except perhaps in the case where there has been an extended comment trail back-and-forth – cropredy Aug 11 '14 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.