Salesforce AssertEquals not working as expected - salesforce

Why is this error thrown? I'm expecting it to pass.
Note : Without this assertion the code works perfectly fine.

I would try explicitly requesting the Last Name field in the SELECT clause as seen below:
for(Contact contact : [SELECT Id LastName, Name FROM Contact]){
System.assertEquals(lastName, contact.LastName);
}
FYI, it would have been easier to answer this question if the code was in text and not an image.

Related

Can somebody explain the error 'System.QueryException: List has no rows for assignment to SObject'?

everyone. Im new to apex, I was trying for code coverage of atleast 75% on my batch apex code through test class. But im getting the error " System.QueryException: List has no rows for assignment to SObject ". Execute method is not covering in the code. Will anyone point out whats wrong with the testclass?
Batch Apex Class:-
Global class BatchApexOnAccount implements Database.Batchable<sObject> {
Global Database.QueryLocator start (Database.BatchableContext Bc){
return Database.getQueryLocator('Select Id, Name from Account');
}
Global void Execute(Database.BatchableContext Bc, List<Account> AccList){
List<Account> AccEmpty = new List<Account>();
for(Account Acc : AccList){
Acc.Name = 'Salesforce';
Acc.Description = 'Update Account ' + system.today();
AccEmpty.add(Acc);
}
Update AccEmpty;
}
Global void Finish (Database.BatchableContext Bc){
}
}
Test class:-
Test code
In your test class you have initiated Account but didn't insert, and in Line 21 you are querying the fields based on that account id which doesn't exists. Because of this you are getting the QueryException.
Your test is weird. You select existing accounts. There are none. You have a piece of batch copy-pasted - why? Even if it'd work - how would you distinguish between what the test did and what the batch did?
You'd have to insert some accounts first. I think the error you get is from test's line 21 but the problems (including "Execute method is not covering in the code") are earlier.
Next time include the test's code too, screenshots are painful to retype.
I think you need something like this (there might be typos!)
private static void testBatch(){
List<Account> accs = new List<Account>{
new Account(Name = 'Unimportant'),
new Account(Name = 'Whatever', Description = 'blah blah...')
};
insert accs;
Test.startTest();
Database.executeBatch(new BatchApexOnAccount());
Test.stopTest();
for(Account a : [SELECT Name, Description FROM Account]){
System.assertEquals('Salesforce', a.Name);
System.assert(a.Description.startsWith('Update account'));
}
}
Edit: another user suggested following edit to my post. I don't agree with it but I'll put it here to preserve the idea and he can always post his own answer with improvements/reasoning.
https://stackoverflow.com/users/6718505/waldemar-liedle
All Assets (sic) in a Loop is a Bad Idea. When no Accounts are Inserted, the Test would still succeed. I would also Check something Outside Loop
List<Account> testAccs = [SELECT Name, Description FROM Account];
System.assertEquals(accs.size(), testAccs.size());
for(Account a : testAccs){
System.assertEquals('Salesforce', a.Name);
System.assert(a.Description.startsWith('Update account'));
}
So...
I don't think it matters because accounts are inserted earlier in the test. You'd need to completely mess the batch job up so it'd start deleting accounts for this query to fail finding any.
normally tests see only data that was inserted during test so it's not like it'll query thousands here (you'd run into other problems like "in test there can be only 1 execute()" so 200 accounts max)
in extreme cases querying all records to a List instead of looping through query (silently calling queryMore() when needed) will eat lots of memory

How can we create test record for "WebStoreNetwork" object in test class?-

I need to create record of WebStoreNetwork in my test class.
SELECT WebStoreId
FROM WebStoreNetwork
WHERE NetworkId = :communityId
WITH SECURITY_ENFORCED
This is the query which is not getting covered in my test class. I am getting value in communityId variable in test class. Facing error "List has no rows to assignment".
Does anyone have any idea? Thanks.
I suggest you post to Salesforce StackExchange.
But a direct answer is that as of Dec-2022 is not possible.
As of the next release, it should be: see Spring'23 release notes.

Handling elements which returns String value in two lines

I'm new to perfecto mobile device automation. I came across a name field which returns value in two lines in the application. Like below
School Name : ABCD
INTERNATIONAL
I found the locator which is Xpath as //*[text()="ABCD INTERNATIONAL"], this returns value in two lines which I confirmed when I ran the code and asserted the value. So basically the error is like below
expected [ABCD INTERNATIONAL] but found [ABCD
INTERNATIONAL]
My feature file is like
Feature: Verify the school Details
Scenario : Verify the school Name
Given User logs into Application
When User verifies the school name as "ABCD INTERNATIONAL"
Then user logs of
Step Defenition:
#When("User verifies the school name as {string}")
public void a_User_verifies_the_school_name_as (String Name) throws Exception {
String School_Name = utility.getText(appiumDriver, "//*[text()="ABCD INTERNATIONAL"]);
Assert.assertEquals(School_Name, Name);
}
Your help in sorting out this will mean a lot.
I think you have to modify your actual and expected string by using replaceAll() method.
Like replaceAll("\n",""); and replaceAll(" ",""). then compare both in assertion.
If it is not working then you have to use pattern matching by regular expression.

How to order by date defined on context model wagtail

i am getting and error trying to order by a fecha_inicio_evento date field on my model https://gist.github.com/9d3e1b3874eb43e6932699b7ea4b13c6 this is the screenshot of my error https://i.imgur.com/ifJzKc0.jpg ,
can you give me a hand folks not sure what i do wrong.
You have to use .specific() at line 23 of your models.py like this:
talleres = self.get_children().live().specific().order_by('-fecha_inicio_evento')

Field Integrity Exception on LinkedEntityID

I am running into a field integrity exception when trying to change the share type of ContentDocumentLink in After insert This is the screenshot of the error I am running into. Here is a snippet of my code my code. Please Help.
for(ContentDocumentLink cV2ValidateTitle: Trigger.new){
for(ContentVersion cvData : titlefromCV){
for(String rName: mapOfRoleNameUser.keySet()){
for(ContentDocumentLink CDLProjMilid : allReqRecords){
if(CDLProjMilid.ContentDocumentId == cV2ValidateTitle.ContentDocumentId){
if(cvData.Title.contains(rName)){
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = cV2ValidateTitle.ContentDocumentId;
cdl.LinkedEntityId = cV2ValidateTitle.LinkedEntityId;
cdl.ShareType = 'V';
system.debug('cdl: '+cdl);
listOfCDL.add(cdl);
}
}
}
}
}
}
You're not sharing all of your code for that trigger. which makes it hard as some of the code relies on the previous lines of code. Your code is also not well indented for readability.
I am assuming for the code provided and the screenshot that you're simply creating another ContentDocumentLink with the exact same LinkedEntityId and ContentDocumentId, try removing that code. As the error suggests you can't do that.

Resources