Can't update Boolean value in Apex - salesforce

I cannot update update this Boolean value in Apex. What is going wrong? The if statement, and the fact that the front end representation is a checkbox, proves that it is indeed a boolean value. I am new to Apex so I feel its a basic misunderstanding of how it works. Can anyone help me out?
Here is the code that I'm executing in an Anonymous Window.
Account acc = new Account(Name='Test Name');
if (acc.Do_Not_Contact__pc == false) {
System.debug('DNC is false');
} else {
System.debug('DNC is true');
}
insert acc;
acc.Do_Not_Contact__pc = true;
update acc;
It fails on the second to last line, displaying the following message:
System.DmlException: Update failed. First exception on row 0 with id 001W000000fFiVbIAK; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Account: bad field names on insert/update call: Do_Not_Contact__pc: [Do_Not_Contact__pc]
What's particularly frustrating is that when I change the second to last line to
acc.Do_Not_Contact__pc = 'true';
I get an error stating that I cannot assign a String to a Boolean value

Remove the single quotes and I assume you typed the field name wrong. Try acc.Do_Not_Contact__c = true;

Related

Check string value in salesforce test class

Below is the code for my test class.
Opportunity opp = [select Deal_Type__c from opportunity where Id: = <some id>];
Case objCase = new Case();
objCase.standard_or_nonstandard__c = 'Yes';
if(objCase.standard_or_nonstandard__c = 'Yes'){ // this if is getting tested
opp.Deal_Type__c = 'Standard';
}
else{ // else part is getting skipped
opp.Deal_Type__c = 'Not Standard';
}
And only first if condition is getting tested and other is skipping which is why the code is not reaching 75% off code coverage.
the field standard_or_nonstandard__c is picklist having two values Yes & No.
And if the value if Yes, the deal type should be standard, and if No, the deal type is not standard.
Any suggestion on this?
Because you're setting the field objCase.standard_or_nonstandard__c to 'Yes' before the condition. This means that there is no way for the field to be equal to 'No' when the condition is evaluated. So the first 'if' block is always entered and the else condition never is. You'll need to remove that line ( objCase.standard_or_nonstandard__c = 'Yes';) before the If statement in order for test data to be able to enter the else block.
You need to make some changes in your code to cover 75% code coverage.
you should try this one:
Opportunity opp = [select Deal_Type__c from opportunity where Id: = <some id>];
Case objCase = new Case();
objCase.standard_or_nonstandard__c = 'Yes';
opp.Deal_Type__c = 'Not Standard';
if(objCase.standard_or_nonstandard__c == 'Yes'){ // this if is getting tested
opp.Deal_Type__c = 'Standard';
}
Thanks,
Ajay Dubedi

Perform Action Based on Checking Box in Adobe using Javascript

I'm attempting to change a textbox value whenever a user checks a checkbox in Adobe Acrobat Pro XI using Javascript, and am inexperienced in it. I am getting an error of Syntax Error: Illegal Character 7: at line 9 based on the below code:
//Checked
if (this.getField("myCheckBox").value != "Off") {
this.getField("myTextBox").value = util.printd("mm/dd/yyyy HH:MM:ss", new Date());
//Not Checked
} else {
this.getField("myTextBox ").value = “”;
}
I have the feeling I need to change the brackets somehow, can anyone clarify?
Thanks for any help!
the quotation marks in the else clause are not the "correct" quotation marks
this.getField("myTextBox ").value = “”; // <-- change these to ""
also, probably need to remove the white-space after "myTextBox "
try...
this.getField("myTextBox").value = "";

Wordpress - get_results() - How to know if failed or empty?

I use the Wordpress function $wpdb->get_results()
https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
It says:
"If no matching rows are found, or if there is a database error, the return value will be an empty array."
Then how can I know if the query failed OR if it's empty?
Use
$results=$wpdb->get_results($yoursql);
if (count($results)> 0){
//do here
}
But if you want to know if query failed
$wpdb -> show_errors ();
$wpdb -> get_results ($wpdb -> prepare($sql));
$wpdb -> print_error ();
Bit late to the party here but I'm just looking for the same thing. I've had a browse through the wp-db.php code on version 4.4.2.
On line 1422, inside the method flush() there's a bit of code which resets the last_error property:
$this->last_error = '';
This flush() method is called in the query() method on line 1693:
$this->flush();
The get_results() method calls query() on line 2322:
if ( $query ) {
$this->query( $query );
} else {
return null;
}
With this we can be pretty sure that more or less every time get_results() (Or get_row() too for that matter) is called, query() and flush() are both called, which ensures that last_error is set to the empty string before the query is executed.
So assuming the query runs (If it doesn't, null is returned - if the query is empty for example), last_error should contain an error message if the query was to fail for some reason.
Since last_error is flush()ed/reset each time, it should only contain an error for the last query that was run, rather than the last error for any query that had been run previously. With this in mind it should be safe to rely on last_error to determine whether something went wrong with the query.
$results = $wpdb->get_results($sql);
if (is_null($results) || !empty($wpdb->last_error)) {
// Query was empty or a database error occurred
} else {
// Query succeeded. $results could be an empty array here
}
Not the most intuitive in my opinion, but it seems to be sufficient.
Personally, I've written my own class around wpdb for my own benefit. This is my getResults() method.
public function getResults($query, $bindings = [])
{
// Prepare the statement (My prepare method inspects $query and just returns it if there's no bindings, otherwise it uses $wpdb->prepare()
$prepared = $this->prepare($query, $bindings);
// Execute the statement
$rows = $this->db->get_results($prepared, ARRAY_A);
// If an array was returned and no errors occurred, return the result set
if (is_array($rows) && empty($this->db->last_error)) {
return $rows;
}
// On failure, return false
return false;
}
Hope this helps.
Wpdb->get_results function from wordpress returns the result if successful otherwise it will return null. There can be many reasons if a query get failed.Refer in-depth article on debugging get_results() returning empty results here
Although you can use functions like wpdb->show_error() to check what was the last error after executing the sql query. sometimes this error returns empty
then try to use wpdb->last_query to check the final query that get formed.

CakePHP saveField NULL value in a INT column

I want to update one column which is an INT with a NULL value. I'm using CakePHP 2.3 with the saveField('field', 'value') method.
Here is my code :
$this->Customer->id = $customer_id;
if ($this->Customer->saveField('account_id', 'NULL')) {
//Do some stuff
}
So when I put 'NULL', the row is updated to 0. I tried NULL without quotes and the row is not updated.
EDIT :
The field in the db accepts NULL value.
Do you have any idea ?
Thanks
Do not pass 'NULL' as a string but as null value:
if($this->Customer->saveField('account_id', null))
{
//do some stuff
}
else
{
//do you get anything here ?
debug($this->Customer->validationErrors);
}
I've had a similiar problem caused by a Upload Behaviour that had validations preventing the saving. Check your models, it might help. I assume saveField returns true even when validation rules prevent saving.

Is there a way to link Save Results (for an insert DML operation) back to sObjects?

Background
I have a list of sObjects I need to insert, but I must first check if the insert will be successful. So, I'm setting a database save point before performing the insert and checking the save results (for the insert statement). Because, I don't want to process if any errors occurred, if there were any errors in the insert results, the database is rolled back to the save point.
Problem & Question
I need to collect the errors for each save (insert) result and associate each error to the specific sObject record that caused the error. According to the documentation Save results contain a list of errors, the Salesforce ID of the record inserted (if successful), and a success indicator (boolean).
How do I associate the Save Result to the original sObject record inserted?
Code/Example
Here's an example I put together that demonstrates the concept. The example is flawed, in that the InsertResults don't always match the sObjectsToInsert. It's not exactly the code I'm using in my custom class, but it uses the same logic.
Map<Id,sObject> sObjectsToInsert; // this variable is set previously in the code
List<Database.SaveResult> InsertResults;
Map<String,sObject> ErrorMessages = new Map<String,sObject>();
System.SavePoint sp = Database.setSavepoint();
// 2nd parameter must be false to get all errors, if there are errors
// (allow partial successes)
InsertResults = Database.insert(sObjectsToInsert.values(), false);
for(Integer i=0; i < InsertResults.size(); i++)
{
// This method does not guarantee the save result (ir) matches the sObject
// I need to make sure the insert result matches
Database.SaveResult ir = InsertResults[i];
sObject s = sObjectsToInsert.values()[i];
String em = null; // error message
Integer e = 0; // errors
if(!ir.isSuccess())
{
system.debug('Not Successful');
e++;
for(Database.Error dbe : ir.getErrors()) { em += dbe.getMessage()+' '; }
ErrorMessages.put(em, s);
}
}
if(e > 0)
{
database.rollback(sp);
// log all errors in the ErrorMessages Map
}
Your comment says the SaveResult list is not guaranteed to be in order, but I believe that it is. I've used this technique for years and have never had an issue.

Resources