• Daniel_Tefera
  • 0 Points
  • Member since 2015

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 2
Hello Everyone,

How do I get object id inserted through test cases in salesforce.

//Method which i need to test 

 public static Product__c addProduct(Product__c product){
          Product__c productToPersist = product;
        Product__c  insertedProduct;
                insert productToPersist;
                insertedProduct=[select Id, Short_Material_Number__c from Product__c where UPC_Code_for_Consumer_Units__c = :productToPersist.Short_Material_Number__c];  // When I am calling this method I am getting System.QueryException: List has no rows for assignment to SObject in test method.

//This is my testcase for above method

@isTest static void save()
Product__c insertedProduct;
// here I am inserting addProduct through json.

Note: As per my understanding the data is not persisted in test case then how do I get the object id from the object when I am trying to insert in salesforce. As we can see that the reason why I am getting this exception because it is returning 0 rows and unable to persist the data and cdnt get the object id also.



I am trying to deploy some code from a sandbox to production but I'm getting the error message that my companys code coverage is 74%.

When I check one of the apex classes in my change set - it has the code coverage of 44% but it doesn't have any test class associated with it.
Is it possible to increase this code coverage for the apex class without creating a test class (this deployment has been the only one to return the error)? Can I add something into the class so that I could increase the coverage to 55% - does commenting count or using system.debug?