2
    purchaserOpeSeniorStaff = new Roles__c();
    purchaserOpeSeniorStaff.RoleID__c = 14.0;
    purchaserOpeSeniorStaff.Corporation_Type__c =2.0;
    purchaserOpeSeniorStaff.RoleName__c = 'Operations Senior Staff';
    insert purchaserOpeSeniorStaff;

On Line 1 it is showing error System.QueryException, Where Roles__c() is a Custom Object with three fields --> RoleId,RoleName,IsActive

This is the line where i am getting error --> *usm.getCompanyCFOUser(supplierCompany.Id,CRConstants.COMPANYTYPE_SUPPLIER);*

and this is the method that above line is calling -->

public UserRoles__c getCompanyCFOUser (ID companyID,Double companyType) {

    Roles__c role=new Roles__c();   

    role = getRoleIDForCFO(companyType);  

    //System.debug('Company Code :' + companyID);  
    //System.debug('Company Type :' + companyType);  
    //System.debug('Role  :' + role.Id);

    UserRoles__c userRoles = new UserRoles__c();
    userRoles=[Select u.FK_UserId__r.Email__c, u.FK_UserId__r.FirstName__c,u.FK_UserId__r.LastName__c,
              u.FK_UserId__r.Title__c,  u.FK_UserId__r.Telephone__c, u.FK_UserId__r.Password__c,
              u.FK_UserId__r.IsActive__c, u.FK_UserId__r.id, u.FK_UserId__r.UserID__c
              from UserRoles__c u where u.FK_RoleID__c=:role.Id and  
              u.FK_UserId__r.Fk_CompanyID__c =:companyID and u.IsDeleted__c=false limit 1]; 

    return userRoles;

 }

How will i fix it ?

4
  • Can you show some more lines before those lines? Because it seams that you have error on previous lines, where you are using results from SOQL query.
    – MarosSitko
    Commented Jan 7, 2014 at 8:27
  • purchaserOpeSeniorStaff = new Roles__c(); check this line;If Roles is an object then change line 1 to Roles__c PupurchaserOpeSeniorStaff = new Roles__c();
    – Sindoora
    Commented Jan 7, 2014 at 8:48
  • 1
    Hi Amit, if possible please share your complete code for creating roles. Commented Jan 7, 2014 at 9:17
  • Is this an unit test? If you've posted complete body of the test method - I suspect the error comes from some trigger that fires during insert. If you have no triggers - you've posted irrelevant code, select something that has queries in them ;)
    – eyescream
    Commented Jan 7, 2014 at 9:56

2 Answers 2

2

Use List instead of single sObject when you are querying your data. for e.g. List<Account> lstAcc=new List<Account>([select id from Account]);

2

The line

userRoles=[Select u.FK_UserId__r.Email__c...

is causing the problem. Try running that query in Developer console to see if you get any results back.

To catch the error, wrap the select inside a try...catch. Something like this:

try {
    userRoles=[Select u.FK_UserId__r.Email__c...];
} catch (Exception e) {
    return null;
}
return userRoles;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .