You need to sign in to do that
Don't have an account?
Web Service & Exception question
Hello
I'm trying to create a Web Service and it's test class in order to upsert Accounts from another system.
I'm encoutering a problem when trying to generate exception : my test class will fail and complete with errors :
Here is my Web Service Class :
global class ReceiveAccountWS {
// Receive Account Web Service
// Receives an Account and upsert it based on External ID : CIEID__c
webService static Id UpdateAccount(String extID, Account acc) {
Account a = new Account();
system.debug('*** Debug acc: ' + acc);
a = acc;
system.debug('*** Debug a : ' + a);
a.CIEID__c = '' + extID;
try {
upsert a CIEID__c;
system.debug('*** Debug upsert OK : ' + a);
return a.id;
} catch (StringException s) {
System.debug('*** Debug Error String : ' + s.getMessage());
string Ers = s.getMessage();
throw s;
return Ers;
} catch (DmlException d) {
System.debug('*** Debug Error DML : ' + d.getMessage());
string Erd = d.getMessage();
return Erd;
} catch (Exception e) {
System.debug('*** Debug Error Exception: ' + e.getMessage());
string Ere = e.getMessage();
return Ere;
}
}
}
And here is my Test Class : for acc2, the Account name is intentionaly left blank :
public class Z_TestReceiveAccountWS {
static testMethod void TestReceiveAccountWS() {
Account acc = new Account();
Account acc2 = new Account();
acc.Name = 'TEST Account';
String MyID = '007';
String MyID2 = '008';
ReceiveAccountWS.UpdateAccount(MyID, acc);
ReceiveAccountWS.UpdateAccount(MyID2, acc2);
}
}
The test result is the following :
Test Class | Z_TestReceiveAccountWS |
Tests Run | 1 |
Test Failures | 1 |
Code Coverage Total % | 72 |
Total Time (ms) | 106.0 |
System.StringException: Invalid id: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]
Class.ReceiveAccountWS: line 8, column 23 Class.Z_TestReceiveAccountWS.TestReceiveAccountWS: line 11, column 3 External entry point
How may I avoid to receive such an exception that I thought I had catched ??
Any help is appreciated.
Thank you
Dimitri
You thew it, that's why you always receive that such exception.
} catch (StringException s) {
System.debug('*** Debug Error String : ' + s.getMessage());
string Ers = s.getMessage();
throw s;
return Ers;
} catch (DmlException d) {
Cheers,
Ouuuuuuuups, sorry.
But I have removed this line (throw s;) and I still receive the same error (I added the line in order to see if it made any differences).
You can try catch Exception instead of StringException.
Cheers,
Still same error while running the Test :
System.StringException: Invalid id: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]
I think u should have to pass a name or in the webservce method insert the account name. i think it will work
Account a = new Account();
system.debug('*** Debug acc: ' + acc);
a = acc;
system.debug('*** Debug a : ' + a);
a.CIEID__c = '' + extID;
a.name = 'test';
upsert a;
Of course it will work. I have added :
if (a.Name == null) {a.Name = 'Test';}
And it works.
But if I don't want to create an account in this case (name empty or null) and return a nice error message, what kind of catch do I have to use ??
Obviously, DMLExecption is not enough !
Thx