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
krish99krish99 

Prevent duplicates in custom webservice soap

HI,

 

          I am working with custom webservice class in sfdc, by using this custom class php developers connect to salesforce

i have one probelm how can we avoide the duplicates ..

 

  For  Example---for php to sfdc we created a account A, again from php same account is send at that time how php people get error message record already exist, for that how can i implement the custom webservice class in salesforce can any one help me.

Best Answer chosen by Admin (Salesforce Developers) 
digamber.prasaddigamber.prasad

Hi,

 

Lets solve the problem one by one. First lets discuss how we can stop duplication of account record on the basis of account Name. You need to have a member field in response, which will have error message(you can plan it according to your business objective). First create the instance of response object and then check for duplication of account record. I have modified your code snippet for account insertiong. Try to run this first, if account duplication issue solve, then we can discuss next problem.

 

ResponseClass res = new ResponseClass();
List<Account> lstAccount = [Select Id from Account where Name =: req.accName];

if(lstAccount.size() > 0){
	res.errorMessage = 'Account already exists!';
	return res;
}

Account a = new Account();
a.Name = req.accName;
a.AccountNumber =String.valueOf(req.accNumber);
a.BillingStreet = req.bStreet;
a.BillingCity = req.bCity;
a.BillingState= req.bState;
a.BillingPostalCode=req.bZip;
a.BillingCountry = req.bCountry;
a.Id =req.accId;
insert a;

 

Let me know if you see any issue with this.

 

Happy to help you!

 

Regards,

Digamber Prasad

 

All Answers

digamber.prasaddigamber.prasad

Hi,

 

How to do you determine that account is duplicate? Is it on the basis of only Name or may be combination of fields? In your web service method you can check against combination of these fields and if found the record, we can send custom error to caller.

 

However there are other ways too. I will be able to help you with exact solution if you can share snippet of your code.

 

Happy to help you!

 

Regards,

Digamber Prasad

krish99krish99

@

 

   

global class MainClass
{
    global class RequestClass
    {
     //Account Releated Fields
      webservice String accName;
      webservice Integer accNumber; 
      webservice String bStreet;
      webservice String bCity;
      webservice String bState;
      webservice String bZip;
      webservice String bCountry;
     webservice Id  accId;

    }

    global class ResponseClass
    {
      //Account Releated Response
       webservice String resId;
       webservice String resName;

   }

  webservice static ResponseClass behaviourOfWebService(RequestClass req)
     {
       // connection to account
          Account a = new Account();
          a.Name = req.accName;
          a.AccountNumber =String.valueOf(req.accNumber);
          a.BillingStreet = req.bStreet;
          a.BillingCity = req.bCity;
          a.BillingState= req.bState;
          a.BillingPostalCode=req.bZip;
          a.BillingCountry = req.bCountry;
          a.Id =req.accId;
          insert a;  
        
   // sending Response by  ResponseClass
     
        ResponseClass res = new ResponseClass();
        res.resId = a.Id;
        res.resName = a.Name;
        res.rescId = c.Id;
        res.resoId = op.id;
        res.resoName = op.Name;
        res.resqName = q.Name;
        return res;
     
          
     } 
 }

 once check it my code is correct or not 

digamber.prasaddigamber.prasad

Looks like you have done copy-paste of some part of your code, which is fine understanding that you don't want to expose your business to outside world. The only thing I can think of wrong in above code is

a.Id =req.accId;

 You can't have Id before you are inserting a record. 

 

Also, could you please answer my above question and that is how you determine if an account is unique or not? Is it just name of  Name of account or the combination of all fieds sent as part of request?

 

If you tell me about criteria to determine if account is unique or not, I can give you snippet of code which will help you.

 

Happy to help you!

 

Regards,

Digamber Prasad

 

 

krish99krish99

@

 

     with account name

krish99krish99

@

 

  Actully i want to implement a custom webservice by using soap i am getting the fields from php 

 

the fields which i am getting from external service 

 

 //Account Releated Fields
    accountName, accountNumber,  billingStreet, billingCity, billingState, billingZip, billingCountry;

////Contact Releated Fields
      firstname, Lastname,email;

 

// Opportunity Releated Fields 
       Name,closeDate, Stage;,Amount;

 

//Quote Releated Fields
       quoteNumber; Name; Status; expDate;

 

based upon these i have to implement a custom webservice after insert all values into sfdc

i have to insert quotelineiteams so i have to check product is exist in sfdc or not,

if not means i have to insert 

 

this is my requriment so help me how can i develop a custom webservice for above requriment

digamber.prasaddigamber.prasad

I am confused, in start of this conversation you said that you want to send error message if account already exists and now you are talking about inserting QuoteLineItems. Lets take one requirement at a time. Let me know your first requirement.

 

Regards,

Digamber Prasad

krish99krish99

Hi,

@

 

   Actually my requriment is i want to insert quotes which are coming from external system.

 

  For this qoutes insertion i have to first insert accounts,contacts,opportunities.then quotes.

 so i begin my webservice first with account insertion.

digamber.prasaddigamber.prasad

Hi,

 

Lets solve the problem one by one. First lets discuss how we can stop duplication of account record on the basis of account Name. You need to have a member field in response, which will have error message(you can plan it according to your business objective). First create the instance of response object and then check for duplication of account record. I have modified your code snippet for account insertiong. Try to run this first, if account duplication issue solve, then we can discuss next problem.

 

ResponseClass res = new ResponseClass();
List<Account> lstAccount = [Select Id from Account where Name =: req.accName];

if(lstAccount.size() > 0){
	res.errorMessage = 'Account already exists!';
	return res;
}

Account a = new Account();
a.Name = req.accName;
a.AccountNumber =String.valueOf(req.accNumber);
a.BillingStreet = req.bStreet;
a.BillingCity = req.bCity;
a.BillingState= req.bState;
a.BillingPostalCode=req.bZip;
a.BillingCountry = req.bCountry;
a.Id =req.accId;
insert a;

 

Let me know if you see any issue with this.

 

Happy to help you!

 

Regards,

Digamber Prasad

 

This was selected as the best answer
krish99krish99

@

 

   Hi thanq for givinig reply then in which way i can test this class my final business class is

global class MainClass
{
  
    global class RequestClass
    {
    public String errorMessage{get;set;}
     //Account Releated Fields
      webservice String accName;
      webservice Integer accNumber; 
      webservice String bStreet;
      webservice String bCity;
      webservice String bState;
      webservice String bZip;
      webservice String bCountry;
      webservice Id  accId;     
  
    }

    global class ResponseClass
    {
     public String errorMessage{get;set;}
       webservice String resId;
       webservice String resName; 
   
    } 
   
    webservice static ResponseClass behaviourOfWebService(RequestClass req)
     {
     
       ResponseClass res = new ResponseClass();
   List<Account> lstAccount = [Select Id from Account where Name =: req.accName];

    if(lstAccount.size() > 0)
    {
    res.errorMessage = 'Account already exists!';
    return res;
    }

     Account a = new Account();
    a.Name = req.accName;
    a.AccountNumber =String.valueOf(req.accNumber);
    a.BillingStreet = req.bStreet;
    a.BillingCity = req.bCity;
    a.BillingState= req.bState;
    a.BillingPostalCode=req.bZip;
     a.BillingCountry = req.bCountry; 
     insert a;
     a.Id =req.accId;
      
    // ResponseClass res = new ResponseClass();
        res.resId = a.Id;
        res.resName = a.Name;
        return res; 
       
          
     } 
     
     

}

 

digamber.prasaddigamber.prasad

Hi,

 

First of all, in your ResponseClass, errorMessage variable needs to be webservice, as shown in below code snippet.

 

//public String errorMessage{get;set;}
webservice String errorMessage;

 

To test it, you can always use SOAPUI. Or if you have the guy who will finally consume your wsdl, you can give him wsdl upto this point and ask him to do testing, which is not recommended. So, I will still suggest you should go with SOAPUI, which is easy to use.

 

Regards,

Digamber Prasad

 

krish99krish99

@

 

   Thanq for Replying, But I have a doubt without writing the test class how can we move sandbox to production,if we test our webservice by using SoapUI means webservice code coverage is increased, without writing  of  test class.

 

   could you please help me how can i insert contact to particular account, opportunity to particular Account, quote into particular

opportunity, shall i use same webservice or i have to create another webservice program. if same webservice means how can i insert that values.

 

    

////Contact Releated Fields
      firstname, Lastname,email;

 

// Opportunity Releated Fields 
       Name,closeDate, Stage;,Amount;

 

//Quote Releated Fields
       quoteNumber; Name; Status; expDate;

digamber.prasaddigamber.prasad

Hi, 

 

You don't need to write separate web service for all these objects, as all can be clubbed into same web service. Please see below code snippet to give you an idea about how you can do this:-

 

ResponseClass res = new ResponseClass();
List<Account> lstAccount = [Select Id from Account where Name =: req.accName];

if(lstAccount.size() > 0){
	res.errorMessage = 'Account already exists!';
	return res;
}

Account a = new Account();
a.Name = req.accName;
a.AccountNumber =String.valueOf(req.accNumber);
a.BillingStreet = req.bStreet;
a.BillingCity = req.bCity;
a.BillingState= req.bState;
a.BillingPostalCode=req.bZip;
a.BillingCountry = req.bCountry;
a.Id =req.accId;
insert a;

Contact c = new Contact()
c.Name = req.contactName;
c.AccountId = a.Id;	//Id from account created above
insert c;	//you can populate other fields too, as done above for account

Opportunity oppty = new Opportunity();
//populate other fields as per your requirement
Oppt.AccountId = a.Id;
insert oppt;

//same for quote and other objects

 

Hopefully it will help you!

 

Regards,

Digamber Prasad

 

 

 

krish99krish99

@

 

  Hi Thanks very very very much..........giving reply to me.

  

   I have one more doubt After creation of quote i want to insert quotelineiteams to particular quote.

   for quoteline iteams i have to insert product details

 

   for me i am getting fields from external system is

 

   product Name  ,product code(SKU) ,description,orginal price ,quantity,price.

 

   for this first i have to check getting product exist in salesforce or not, if exist means it automatically asign to particular quote,

else i have to create a product.and then account---to--quote

 

 can you give me idea about how can i create a quote and how it is asign to a standard price book.

how it can be asign to particular quote.

digamber.prasaddigamber.prasad

Hi,

 

What is unique criteria for creation of product, is it only product key or combination of more than one field?

 

Happy to help you!

 

Regards,

Digamber Prasad

krish99krish99

@

 

        HI,

 

           Product code is the unique item for creating record.

digamber.prasaddigamber.prasad

Hi,

 

I hope below code snippet will help you.

String productId = '';
Product2 prod = [Select Id, ProductCode from Product2 where ProductCode =: req.productCode limit 1];

if(prod != null){
	productId = prod.Id;
}else{
	Product2 prod = new Product2(ProductCode = req.productCode, Name = req.productName);	//you can add more fields as per your requirement
	insert prod;
	productId = prod.Id;
}

Quote__c q = new Quote__c(Product__c = productId);	//you can set other fields as per your requirement

 

Let me know if you have any other problem.

 

Happy to help you!

 

Regards,

Digamber Prasad

krish99krish99

@prasad

 

HI,

   when i am testing my code means i am getting folling error i am using soapUi tool if u need means i will give my code also.to test

      

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="urn:fault.enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <soapenv:Fault>
         <faultcode>INVALID_LOGIN</faultcode>
         <faultstring>INVALID_LOGIN: malformed id: organizationId '?'</faultstring>
         <detail>
            <sf:LoginFault xsi:type="sf:LoginFault">
               <sf:exceptionCode>INVALID_LOGIN</sf:exceptionCode>
               <sf:exceptionMessage>malformed id: organizationId '?'</sf:exceptionMessage>
            </sf:LoginFault>
         </detail>
      </soapenv:Fault>
   </soapenv:Body>
</soapenv:Envelope>

 

digamber.prasaddigamber.prasad

Hi,

 

Could you please post the code snappet from SoapUI of Request which you are trying to send to salesforce for login? Looks like you are missing organizationID.

 

Happy to help you!

 

Regards,

Digamber Prasad

krish99krish99

@

 

Below code is my sample webservice class

 

global class MainClass
{
     global class RequestClass
    {
     webservice String errorMessage;
     //Account Releated Fields
      webservice String accName;
      webservice Integer accNumber; 
      webservice String bStreet;
      webservice String bCity;
      webservice String bState;
      webservice String bZip;
      webservice String bCountry;
      webservice Id  accId;     
  
    }

    global class ResponseClass
    {
     webservice String errorMessage;
       webservice String resId;
       webservice String resName; 
   
    } 
   
    webservice static ResponseClass behaviourOfWebService(RequestClass req)
     {
     
       ResponseClass res = new ResponseClass();
   List<Account> lstAccount = [Select Id from Account where Name =: req.accName];

    if(lstAccount.size() > 0)
    {
    res.errorMessage = 'Account already exists!';
    return res;
    }

     Account a = new Account();
    a.Name = req.accName;
    a.AccountNumber =String.valueOf(req.accNumber);
    a.BillingStreet = req.bStreet;
    a.BillingCity = req.bCity;
    a.BillingState= req.bState;
    a.BillingPostalCode=req.bZip;
     a.BillingCountry = req.bCountry; 
     insert a;
     a.Id =req.accId;
      
    // ResponseClass res = new ResponseClass();
        res.resId = a.Id;
        res.resName = a.Name;
        return res; 
       
          
     } 
     
     

}

 

digamber.prasaddigamber.prasad

Hi,

 

Looking into it. Will get back to you ASAP.

 

Happy to help you!

digamber.prasaddigamber.prasad

Hi,

 

I have run this and can't see any problem. Do you have skype id, where we can do screen share to see exactly what you are trying to do?

 

Happy to help you!

krish99krish99

@

 

    ok thanq for reply, i will also run again my program and inform you.

krish99krish99

@digamberlucky

 

    Hi,

when i use below webserivce means just account is creating,not contact may i know how can i insert contact into particular

account

 

 

   

global class MainClass
{
     global class RequestClass
    {
     webservice String errorMessage;
     //Account Releated Fields
      webservice String accName;
      webservice Integer accNumber; 
      webservice String bStreet;
      webservice String bCity;
      webservice String bState;
      webservice String bZip;
      webservice String bCountry;
     webservice Id  AccountId;    
    
     webservice String cfName;
      webservice String clName; 
      
    }

    global class ResponseClass
    {
     webservice String errorMessage;
       webservice String resId;
       webservice String resName;
       
        webservice String rescId;
        webservice String cresName;
   
    } 
   
    webservice static ResponseClass behaviourOfWebService(RequestClass req)
     {
     
       ResponseClass res = new ResponseClass();
   List<Account> lstAccount = [Select Id from Account where Name =: req.accName];

    if(lstAccount.size() > 0)
    {
    res.errorMessage = 'Account already exists!';
    return res;
    }

     Account a = new Account();
    a.Name = req.accName;
    a.AccountNumber =String.valueOf(req.accNumber);
    a.BillingStreet = req.bStreet;
    a.BillingCity = req.bCity;
    a.BillingState= req.bState;
    a.BillingPostalCode=req.bZip;
     a.BillingCountry = req.bCountry; 
     insert a;
     a.Id =req.AccountId;
      
    // ResponseClass res = new ResponseClass();
        res.resId = a.Id;
        res.resName = a.Name;
        return res; 
       
         Contact c = new Contact();
          c.FirstName = req.cfName;
          c.LastName = req.clName;
          c.AccountId = a.Id;
          insert c;
       
       res.cresName = c.FirstName;
       return res;
          
     } 
     
     

}

 

digamber.prasaddigamber.prasad

Hi,

 

Happy that it atleast now you can insert account. You can use below code snippet to insert both contact & opportunity:-

 

Contact c = new Contact()
c.Name = req.contactName;
c.AccountId = a.Id;	//Id from account created above
insert c;	//you can populate other fields too, as done above for account

Opportunity oppty = new Opportunity();
//populate other fields as per your requirement
Oppt.AccountId = a.Id;
insert oppt;

//same for quote and other objects

 Let me know if you have any specific question.

 

Happy to help you!

 

 

krish99krish99

@DigamberLucky

 

     Hi, 

 

          your skype id is not visiable in skype directory once check and send me exact id.

 

 

 

digamber.prasaddigamber.prasad

Hi,

 

You need to insert contact before sending response. Something like below code snippet:-

 

global class MainClass
{
     global class RequestClass
    {
     webservice String errorMessage;
     //Account Releated Fields
      webservice String accName;
      webservice Integer accNumber; 
      webservice String bStreet;
      webservice String bCity;
      webservice String bState;
      webservice String bZip;
      webservice String bCountry;
     webservice Id  AccountId;    
    
     webservice String contactName;
      webservice String clName; 
      
    }

    global class ResponseClass
    {
     webservice String errorMessage;
       webservice String resId;
       webservice String resName;
       
        webservice String rescId;
        webservice String cresName;
   
    } 
   
    webservice static ResponseClass behaviourOfWebService(RequestClass req)
	{
     
		ResponseClass res = new ResponseClass();
		List<Account> lstAccount = [Select Id from Account where Name =: req.accName];

		if(lstAccount.size() > 0)
		{
			res.errorMessage = 'Account already exists!';
			return res;
		}

		Account a = new Account();
		a.Name = req.accName;
		a.AccountNumber =String.valueOf(req.accNumber);
		a.BillingStreet = req.bStreet;
		a.BillingCity = req.bCity;
		a.BillingState= req.bState;
		a.BillingPostalCode=req.bZip;
		a.BillingCountry = req.bCountry; 
		insert a;
	
		Contact c = new Contact();
		c.AccountId = a.Id; 
		c.FirstName = req.contactName;		//check if you need other fields of contact too
		insert c;
	   
		  
		// ResponseClass res = new ResponseClass();
		res.resId = a.Id;
		res.resName = a.Name;
		res.cresName = c.FirstName;
		return res; 
		return res;
          
	} 
     
     

}

 Let me know if you have any question.

 

Happy to help you!

 

 

 

digamber.prasaddigamber.prasad

Hi,

 

I assume you have already inserted quote record and name of variable is 'quote'. Also, on QuoteLineItem, only mandatory field is Name. You can use below code snippet to insert quotelineitem record with association of quote.

 

QuoteLineItem__c quoteLineItem = new QuoteLineItem__c(Name = 'Test Quote Line Item', Quote__c = quote__c);
insert quoteLineItem;

  You can modify it to meet your requirement.

 

 

 

digamber.prasaddigamber.prasad

Hi,

 

Probably below will help you:-

 

String productId = '';
Product2 prod = [Select Id, ProductCode from Product2 where ProductCode =: req.productCode limit 1];

if(prod != null){
	productId = prod.Id;
}else{
	Product2 prod = new Product2(ProductCode = req.productCode, Name = req.productName);	//you can add more fields as per your requirement
	insert prod;
	productId = prod.Id;
}

Quote__c q = new Quote__c(Product__c = productId);	//you can set other fields as per your requirement

 Please let me know if you stil lhave any question.

 

 

digamber.prasaddigamber.prasad

Hi,

 

Please try below code, now product record will be active.

String productId = '';
Product2 prod = [Select Id, ProductCode from Product2 where ProductCode =: req.productCode limit 1];

if(prod != null){
	productId = prod.Id;
}else{
	Product2 prod = new Product2(ProductCode = req.productCode, Name = req.productName, IsActive = true);	//you can add more fields as per your requirement
	insert prod;
	productId = prod.Id;
}

Quote__c q = new Quote__c(Product__c = productId);	//you can set other fields as per your requirement

Let me know if you have any question.

 

krish99krish99

@

 

   

HI,

 

      first time i created a account name----Google

     for that account contact have --->> user1

 

    insert successfully,

 

   again same acount is send  by third party at that time it shows that account already exist,,,,but in this case the we are sending same account but USER2 details

   but when test this at that time USER2 details have to map to GOOGLE but it is not ...MAY i know how can i achieve this..

digamber.prasaddigamber.prasad

Hi,

 

I have modified code below and you can modify it as per your requirement:-

 

global class MainClass
{
     global class RequestClass
    {
     webservice String errorMessage;
     //Account Releated Fields
      webservice String accName;
      webservice Integer accNumber; 
      webservice String bStreet;
      webservice String bCity;
      webservice String bState;
      webservice String bZip;
      webservice String bCountry;
     webservice Id  AccountId;    
    
     webservice String contactName;
      webservice String clName; 
      
    }

    global class ResponseClass
    {
     webservice String errorMessage;
       webservice String resId;
       webservice String resName;
       
        webservice String rescId;
        webservice String cresName;
   
    } 
   
    webservice static ResponseClass behaviourOfWebService(RequestClass req)
	{
     
		ResponseClass res = new ResponseClass();
		List<Account> lstAccount = [Select Id from Account where Name =: req.accName];
		
		Account a = new Account();
		if(lstAccount.size() > 0)
		{
			a = lstAccount[0];
		}else{		
			a.Name = req.accName;
			a.AccountNumber =String.valueOf(req.accNumber);
			a.BillingStreet = req.bStreet;
			a.BillingCity = req.bCity;
			a.BillingState= req.bState;
			a.BillingPostalCode=req.bZip;
			a.BillingCountry = req.bCountry; 
			insert a;
		}
	
		Contact c = new Contact();
		c.AccountId = a.Id; 
		c.FirstName = req.contactName;		//check if you need other fields of contact too
		insert c;
	   
		  
		// ResponseClass res = new ResponseClass();
		res.resId = a.Id;
		res.resName = a.Name;
		res.cresName = c.FirstName;
		return res; 
		return res;
          
	} 
     
     

}

 Let me know if you have any question.

 

krish99krish99

HI,

 

          How can i insert multiple products at a time into sfdc.

digamber.prasaddigamber.prasad

Hi,

 

For error related to Product record, can you please replace your code with below code snippet & let me know if you still see any issue:-

 

String productId = '';
List<Product2> lstProduct= [Select Id, ProductCode from Product2 where ProductCode =: req.productCode]; // error occuring point
if(lstProduct.size()>0)
{
	//  productId = p.Id;
	res.errorMessage = 'pexist!';
	return res;
 
}

else 
{

	Product2 p2 = new Product2(ProductCode = req.productCode, Name = req.productName);    
	insert p2;
	productId = p2.Id;

}

 

digamber.prasaddigamber.prasad

Hi,

 

I have modified your code base for product part

 

String productId = '';
		List<Product2> lstProduct= [Select Id, ProductCode from Product2 where ProductCode =: req.productCode]; // error occuring point
		if(lstProduct.size()>0)
		{
			Product2 p2 = lstProduct[0];
			//set other field of product if you want to
			upsert p2;
			productId = p2.Id;
		}

		else 
		{

			Product2 p2 = new Product2(ProductCode = req.productCode, Name = req.productName);    
			insert p2;
			productId = p2.Id;

		}

 Let me know if you have any question.

 

digamber.prasaddigamber.prasad

Hi,

 

You can refer below code for inserting quotelineitem:-

 

global class MainClass
{
	global class RequestClass
    {
		webservice String errorMessage;
		//Account Releated Fields
		webservice String accName;
		webservice Integer accNumber; 
		webservice String bStreet;
		webservice String bCity;
		webservice String bState;
		webservice String bZip;
		webservice String bCountry;
		webservice Id  AccountId;    

		//Contact Releated Fields
		//webservice String contactName;
		webservice String clName;
		webservice String cfName; 

		// Opportunity Releated Fields
		webservice String oName;
		webservice String ocDate;
		webservice String oStage;

		// Quote Realeted Fields 
		webservice String qName;


		// Product fields

		webservice String productcode; 
		webservice String productName;
		// boolean active;
	}

	global class ResponseClass
	{
		webservice String errorMessage;
		webservice String resId;
		webservice String resName;

		webservice String rescId;
		webservice String cresName;
		webservice String respId;

	} 

	webservice static ResponseClass behaviourOfWebService(RequestClass req)
	{

		ResponseClass res = new ResponseClass();
		List<Account> lstAccount = [Select Id from Account where Name =: req.accName];

		if(lstAccount.size() > 0)
		{
			res.errorMessage = 'Account already exists!';
			return res;
		}

		Account a = new Account();
		a.Name = req.accName;
		a.AccountNumber =String.valueOf(req.accNumber);
		a.BillingStreet = req.bStreet;
		a.BillingCity = req.bCity;
		a.BillingState= req.bState;
		a.BillingPostalCode=req.bZip;
		a.BillingCountry = req.bCountry; 
		insert a;


		List<Contact> lstContact = [Select Id from Contact where FirstName =: req.cfName and LastName =: req.clName];
		if(lstContact.size() > 0)
		{

			res.errorMessage = 'FirstName and lastName already exists!';
			return res;
		}

		Contact c = new Contact();
		c.AccountId = a.Id; 
		c.FirstName = req.cfName;
		c.LastName = req.clName;      
		insert c;


		Opportunity o = new Opportunity();
		o.AccountId  = a.Id;
		o.Name = req.oName;
		//  Date dt = Date.today().addDays(7)
		//    o.CloseDate =dt;
		//  o.CloseDate = system.today();
		o.CloseDate = system.today().addDays(-1);
		o.StageName =req.oStage;  
		insert o; 

		String productId = '';
		List<Product2> lstProduct= [Select Id, ProductCode from Product2 where ProductCode =: req.productCode];  
		if(lstProduct.size()>0)
		{
			Product2 p2 = lstProduct[0];
			//set other field of product if you want to
			upsert p2;
			productId = p2.Id;
		}

		else 
		{

			Product2 p2 = new Product2(ProductCode = req.productCode, Name = req.productName,IsActive = true);    
			insert p2;
			productId = p2.Id;

		}

		Quote q = new Quote();
		o.AccountId = a.Id;
		q.OpportunityId = o.Id;
		q.ContactId = c.Id;
		q.Name =req.qName;
		insert q;  
		
		QuoteLineItem qtLineItem = new QuoteLineItem();
		qtLineItem.Quote__c = q.Id;
		//Any other field if you want to have
		insert qtLineItem;

		  
		// ResponseClass res = new ResponseClass();
		res.resId = a.Id;
		res.resName = a.Name;
		res.cresName = c.FirstName;
		// res.respId = p.Id;
		return res; 
		return res;
        
      
          
    } 
     
     

}

 Let me know if you have any question.

digamber.prasaddigamber.prasad

Hi,

 

Updated code for QuoteLineItem will look like:-

 

QuoteLineItem qtLineItem = new QuoteLineItem();
qtLineItem.Quote__c = q.Id;
qtLineItem.Product__c = productId;
//Any other field if you want to have
insert qtLineItem;

 Let me know if you have any question.

 

 

digamber.prasaddigamber.prasad

Hi,

 

Could you please try below code snippet to insert QuoteLineItem:-

 

QuoteLineItem qtLineItem = new QuoteLineItem();
qtLineItem.Quote__c = q.Id;
qtLineItem.Product2Id = productId;
qtLineItem.Quantity = Integer.valueOf(request.quantity);
qtLineItem.UnitPrice = Integer.valueOf(request.unitPrice);
qtLineItem.PricebookEntryId = request.pricebookEntryId;
insert qtLineItem;

 Let me know if you still see any problem.