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
Naren9Naren9 

Sample Account Update Webservice

Hi All,
We are trying to integrate the External ERP system to Salesforce through Webservices as external ERP is not supported with REST API.
I wrote the below Apex code in Salesforce and Tested the Webservice through Soap UI.Everything is working fine, but I may need to add some error handler.
I am new to Salesforce, please can you review this and provide your suggestions.
Note: I can't use the Enterprise and Partner WSDL update method to update the Account as I have some custom fields and i am using that as Unique Identifier.So I have to write teh Custom Webservice in Salesforce.

global class GlobalAccountUpdate {


    global class requestWs{
        webservice String AccountName;
        webservice String AccountNumber;
        webservice Decimal YTD;
        webservice Decimal MTD;
       Account Acnt;
    }
    global class responseOfWs{
        webservice ID id;
        webservice String AccountNumber;
        webservice string errorFound;
        webservice string success;
        webservice string resErrorCode;
    }
     
     webservice static responseOfWs UpdateAccount(requestWs Info)
    {
        responseOfWs a = new responseOfWs();
        Account accountToUpdate;
         if((String.isNotBlank(Info.AccountName)) && (String.isNotBlank(Info.AccountNumber)))
       
        {
        accountToUpdate = [Select AccountName From Account Where Name = :Info.AccountName and AccountNumber=:Info.AccountNumber limit 1];
        accountToUpdate.Month_to_Date_Sales__c=Info.MTD;
         accountToUpdate.Year_to_Date_Sales__c=Info.YTD;
        
        try
        {
         update accountToUpdate;   
        }
        catch(DmlException e) {
            responseOfWs c = new responseOfWs();
            c.resErrorCode='An unexpected error has occurred: ' + e.getMessage();
            return c;
        //System.debug('An unexpected error has occurred: ' + e.getMessage());
        }
        
        
        a.id=accountToUpdate.Id;
        a.AccountName=Info.AccountName;
        a.success='True';
        a.errorFound='False';
        a.resErrorCode='False';
        return a;
        }
        else
        {
            responseOfWs b = new responseOfWs();
            b.resErrorCode='ERROR-003';
            b.errorFound='Required Fields Not sent';
            return b;
           
        }
    }

}

Thanks,
Naren




 
Best Answer chosen by Naren9
Raj VakatiRaj Vakati
Try like this .. you code is good
 
global class GlobalAccountUpdate {


    global class requestWs{
        webservice String AccountName;
        webservice String AccountNumber;
        webservice Decimal YTD;
        webservice Decimal MTD;
       Account Acnt;
    }
    global class responseOfWs{
        webservice ID id;
        webservice String AccountNumber;
        webservice string errorFound;
        webservice string success;
        webservice string resErrorCode;
    }
     
     webservice static responseOfWs UpdateAccount(requestWs Info)
    {
		        responseOfWs a = new responseOfWs();

		try{
       // responseOfWs a = new responseOfWs();
        Account accountToUpdate;
         if((String.isNotBlank(Info.AccountName)) && (String.isNotBlank(Info.AccountNumber)))
       
        {
        accountToUpdate = [Select AccountName ,AccountNumber From Account Where Name = :Info.AccountName and AccountNumber=:Info.AccountNumber limit 1];
        accountToUpdate.Month_to_Date_Sales__c=Info.MTD;
         accountToUpdate.Year_to_Date_Sales__c=Info.YTD;
        
        try
        {
         update accountToUpdate;   
        }
        catch(DmlException e) {
            responseOfWs c = new responseOfWs();
            c.resErrorCode='An unexpected error has occurred: ' + e.getMessage();
            return c;
        //System.debug('An unexpected error has occurred: ' + e.getMessage());
        }
        
        
        a.id=accountToUpdate.Id;
        a.AccountName=Info.AccountName;
        a.success='True';
        a.errorFound='False';
        a.resErrorCode='False';
        return a;
        }
        else
        {
            responseOfWs b = new responseOfWs();
            b.resErrorCode='ERROR-003';
            b.errorFound='Required Fields Not sent';
            return b;
           
        }
    }
	}catch(Exception e){
				            c.success='Failed';
   c.errorFound='An unexpected error has occurred: ' + e.getMessage();
      c.AccountNumber='accountToUpdate.AccountNumber;
		            c.resErrorCode='An unexpected error has occurred: ' + e.getMessage();
return c ; 
	}

}

 

All Answers

Raj VakatiRaj Vakati
Try like this .. you code is good
 
global class GlobalAccountUpdate {


    global class requestWs{
        webservice String AccountName;
        webservice String AccountNumber;
        webservice Decimal YTD;
        webservice Decimal MTD;
       Account Acnt;
    }
    global class responseOfWs{
        webservice ID id;
        webservice String AccountNumber;
        webservice string errorFound;
        webservice string success;
        webservice string resErrorCode;
    }
     
     webservice static responseOfWs UpdateAccount(requestWs Info)
    {
		        responseOfWs a = new responseOfWs();

		try{
       // responseOfWs a = new responseOfWs();
        Account accountToUpdate;
         if((String.isNotBlank(Info.AccountName)) && (String.isNotBlank(Info.AccountNumber)))
       
        {
        accountToUpdate = [Select AccountName ,AccountNumber From Account Where Name = :Info.AccountName and AccountNumber=:Info.AccountNumber limit 1];
        accountToUpdate.Month_to_Date_Sales__c=Info.MTD;
         accountToUpdate.Year_to_Date_Sales__c=Info.YTD;
        
        try
        {
         update accountToUpdate;   
        }
        catch(DmlException e) {
            responseOfWs c = new responseOfWs();
            c.resErrorCode='An unexpected error has occurred: ' + e.getMessage();
            return c;
        //System.debug('An unexpected error has occurred: ' + e.getMessage());
        }
        
        
        a.id=accountToUpdate.Id;
        a.AccountName=Info.AccountName;
        a.success='True';
        a.errorFound='False';
        a.resErrorCode='False';
        return a;
        }
        else
        {
            responseOfWs b = new responseOfWs();
            b.resErrorCode='ERROR-003';
            b.errorFound='Required Fields Not sent';
            return b;
           
        }
    }
	}catch(Exception e){
				            c.success='Failed';
   c.errorFound='An unexpected error has occurred: ' + e.getMessage();
      c.AccountNumber='accountToUpdate.AccountNumber;
		            c.resErrorCode='An unexpected error has occurred: ' + e.getMessage();
return c ; 
	}

}

 
This was selected as the best answer
Naren9Naren9
Hi Raj,
In this Step:
 accountToUpdate = [Select AccountName ,AccountNumber From Account Where Name =:Info.AccountName and AccountNumber=:Info.AccountNumber limit 1];
With the above code, if record is not found in Salesforce, then I am getting this error.

<faultstring>System.QueryException: List has no rows for assignment to SObject

Class.GlobalAccountUpdate.UpdateAccount: line 26, column 1</faultstring>

I want to put a Condition as 
if (accountToUpdate != null), but before checking this conditin I am getting the above error
Any help
Raj VakatiRaj Vakati
try this code

 
global class GlobalAccountUpdate {


    global class requestWs{
        webservice String AccountName;
        webservice String AccountNumber;
        webservice Decimal YTD;
        webservice Decimal MTD;
       Account Acnt;
    }
    global class responseOfWs{
        webservice ID id;
        webservice String AccountNumber;
        webservice string errorFound;
        webservice string success;
        webservice string resErrorCode;
    }
     
     webservice static responseOfWs UpdateAccount(requestWs Info)
    {
		        responseOfWs a = new responseOfWs();

		try{
       // responseOfWs a = new responseOfWs();
        Account accountToUpdate = new Account();
         if((String.isNotBlank(Info.AccountName)) && (String.isNotBlank(Info.AccountNumber)))
       
        {
        accountToUpdate = [Select AccountName ,AccountNumber From Account Where Name = :Info.AccountName and AccountNumber=:Info.AccountNumber limit 1];
        accountToUpdate.Month_to_Date_Sales__c=Info.MTD;
         accountToUpdate.Year_to_Date_Sales__c=Info.YTD;
        
        try
        {
if(accountToUpdate.id !=null){
         update accountToUpdate;   
}
        }
        catch(DmlException e) {
            responseOfWs c = new responseOfWs();
            c.resErrorCode='An unexpected error has occurred: ' + e.getMessage();
            return c;
        //System.debug('An unexpected error has occurred: ' + e.getMessage());
        }
        
        
        a.id=accountToUpdate.Id;
        a.AccountName=Info.AccountName;
        a.success='True';
        a.errorFound='False';
        a.resErrorCode='False';
        return a;
        }
        else
        {
            responseOfWs b = new responseOfWs();
            b.resErrorCode='ERROR-003';
            b.errorFound='Required Fields Not sent';
            return b;
           
        }
    }
	}catch(Exception e){
	<b>			            c.success='Failed';
   c.errorFound='An unexpected error has occurred: ' + e.getMessage();
      c.AccountNumber='accountToUpdate.AccountNumber;
		            c.resErrorCode='An unexpected error has occurred: ' + e.getMessage();
return c ; 
	}</b>

}

 
Naren9Naren9
I already tried by adding the Account accountToUpdate = new Account();but same error.

 
Raj VakatiRaj Vakati
give me you latest code 
Naren9Naren9
Hi Raj,
Thanks for your help.
Isssue has been resolved after using this code:
List<Account> lstAccount = [Select Id,Name From Account Where Name = :AccountInfo.AccountName and AccountNumber=:AccountInfo.AccountNumber limit 1];
                      if (lstAccount.size() > 0)
                        {
                             Account accountToUpdate = lstAccount.get(0);
                            accountToUpdate.Year_to_Date_Sales__c=AccountInfo.YTD;


Thanks,
Naren