Field Integrity Exception on LinkedEntityID - salesforce

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.

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.

Salesforce AssertEquals not working as expected

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.

Getting “java.sql.SQLException: Values not bound to statement” exception

I recently upgraded from sqlite-jdbc-3.7.2.jar to sqlite-jdbc-3.26.0.jar. I observed that the insert query in my code are failing with “java.sql.SQLException: Values not bound to statement” exception.
Below is the piece of code which works fine for previous version of sqlite :
String sqlStatement = "INSERT INTO table_name(id,name,type,author,size) VALUES (?,?,?,?,?)";
try
{
Connection conn = this.connect(<name>);
PreparedStatement pstmt = conn.prepareStatement(sqlStatement))
{
pstmt.setInt(1, rs.getInt(<value>));
pstmt.setString(2, rs.getString(<value>));
pstmt.setInt(3, rs.getInt(<value>));
if (somecondition)
{
pstmt.setString(4, rs.getString(<Value>));
pstmt.setInt(5, rs.getInt(<value>));
}
pstmt.executeUpdate();
}
}
catch(Exception e)
{
//handling of exception goes here
}
I have read the release notes https://www.sqlite.org/changes.html and checked that as a part of Query planner enhancements, few things were changed (The query planner examines the values of bound parameters to help determine if a partial index is usable.
).
But I am still not clear what was the enhancement and how it is affecting my code.
Also can someone guide me on how to fix the above code?
Thanks,
Ketaki
In the above program, the insert query was expecting 5 values to be populated by user.
However, if(somecondition) is not satisfied then values for 4th and 5th were not getting inserted in query. This was the issue since sqlite-jdbc-3.26.0 expects all values to be populated before executing the query. In the fix, I ensured that all values are populated even if "somecondition" is false by inserting else block.
Fix :
if (somecondition)
{
pstmt.setString(4, rs.getString(<Value>));
pstmt.setInt(5, rs.getInt(<value>));
}
else
{
pstmt.setString(4, rs.getString(<default-value>));
pstmt.setInt(5, rs.getInt(<defaul`enter code here`t-value>));
}

Can someone help me add a new column in Google cloud Search Index using Java

I tried adding a new column to an existing search index but it is throwing an error, the error as stated below:-
Field docname is present 0 times; expected 1
java.lang.IllegalArgumentException: Field docname is present 0 times; expected 1
I can see that the new column has been added to the search index but cannot retrieve the index.
By my observation i can see that the existing records in the index dont have the new column data and hence it is giving a this error, but the new records will be having this column values. Can anyone help me with this.
Upon having this problem myself today, I searched a bit in the documentation. It was a rather frustrating error as it didn't actually pin point where the problem was in my code.
It appears that when you use getOnlyField("something") on a Document (in this case on one of many returned from a search query), if that field does not actually exist yet in that specific document it throws the java.lang.IllegalArgumentException.
Because that can often be the case when you update an index with new columns, I'm using something like this to get around it:
public static Long getNumberField(ScoredDocument d, String name, Long defaultValue) {
try {
return d.getOnlyField(name).getNumber().longValue();
} catch (IllegalArgumentException e) {
return defaultValue;
}
}
Which is called in the search results code:
Long numberValue = SearchUtils.getNumberField(scoredDocument, "featuredOrder", -1L)
This allows me to catch that error and return a default value when it doesn't exist.
You can find the documentation here:
https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/search/Document.html#getOnlyField-java.lang.String-

Resources