I am using Doctrine 2.4.8 in my Symfony 2.8.34 project.
My Problem is that custom changes I made in the preUpdate function preventing the expected changes from the admin form. Dependent on a choice field a new event should been created or deleted.
class CustomAdmin extends AbstractAdmin {
public function preUpdate($object) {
...
if ($object->isDetached()) {
$event = new Event();
...
$object->setDetachedEvent($event);
} else {
$object->setDetachedEvent(null);
}
}
}
Now, the line $object->setDetachedEvent(null); turns everything crazy. The first time I'm saving the form my custom changes in that preUpdate function are executed, but the changes in the fields of the admin form are ignored. The second time the changes from the admin form were executed. If I remove the line above, everythings works fine.
So, anybody has an idea how to handle this problem?
Related
So I'll start by saying I'm a C# .Net/Javascript developer with a lot of experience, but I have zero experience with Salesforce. Never ever seen it before today. So, I've been asked by another team to add a custom button to a Contact object, which when clicked calls an external API and updates a custom field in the Contact with the response. It was pitched as "just write some Javascript that calls an API when a button is clicked, it's literally embedded into the page, 15 minute job...".
Following what appears to be quite an outdated document, I've ended up in the Object Manager, selected the Contact object and I'm into the Buttons, Links and Actions page. I'm assuming before this was done using the Execute Javascript behaviour, which in the Lightning version I'm advised against using. So after much Googling I've read about APEX classes, Visualforce Components, Lightning Components, the Salesforce REST API, etc, etc. Not a 15 min job.
Essentially the requirements are to embed a button (or action, or..?) into this Contact page, so that when the sales guy clicks it, it gathers some of the Contact's details and uses them to form an API call to an external service. The data will be used to form a response, which must then be read (as JSON, then parsed) and written into a custom field on the Contact.
What would be the best approach for developing a solution? In the Apex Debug environment I've put together the code to call the API and parse the JSON response, I'm assuming I need to wrap this in an Apex class, with a method that calls this code and returns the response. What I'm not sure of is how to call into this from the button, and update the field in the Contact.
Do I need to do all that from the Apex Class method? passing in a reference to the Contact, or is there another component that needs to sit in between and do this.
Am I right in assuming I'll need to use the Salesforce API to update the Contact?
Any pointers appreciated.
Oh man. It can be a 15 min job but it's definitely "easy when you know how" or have some examples ;)
What's your user interface, Classic or Lightning? Do they have plans to migrate to Lightning soon? I'm assuming it's Lightning if you figured out the "Execute JavaScript" hacks are passé.
Do you care where the button/action will be? Is the top right corner with all other buttons fine or do you want it to be droppable to pretty much any area in the page?
Does the API callout need username, password, maybe certificate? It'll determine whether you need just to whitelist the endpoint on firewall (Setup -> Remote Site Settings) or you'll need something more advanced (Setup -> Named Credentials).
Do you have SFDX command line (CLI), VSCode / are determined to install some tooling? The Lightning Web Components are cutting edge, most sleek etc but you can't create them straight in the browser (at least not yet), you need tooling. Visualforce is OK but nothing special for this use case, Aura components are bit clunky to write - but you can do both in Developer Console without extra tooling.
Parsing the JSON response - depends how complex it is, you can hand-craft parser with JSON.deserializeUntyped() but life's too short for this. Here's nice apex code generator similar to what you'd get from parsing WSDL: https://json2apex.herokuapp.com/
We'll try to do Aura component way. It's ugly, LWC is future but hey, it'll get you started.
Go to Setup -> Remote Site Settings and add new entry with https://en.wikipedia.org/
Create new Apex class:
public with sharing class Stack63364119 {
static final String endpoint = 'https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=';
#AuraEnabled
public static String doCallout(Id contactId){
if(contactId == null){
throw new MyException('Missing record id');
}
List<Contact> contacts = [SELECT MailingCountry FROM Contact WHERE Id = :contactId];
if(contacts.isEmpty() || String.isBlank(contacts[0].MailingCountry)){
throw new MyException('Could not find the contact');
}
Contact c = contacts[0];
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint + c.MailingCountry);
req.setMethod('GET');
HTTPResponse res = new Http().send(req);
System.debug(res.getStatus());
System.debug(res.getBody());
// no special parsing, just chuck it into Description field
// no error handling
if(res.getStatusCode() == 200){
c.Description = res.getBody().abbreviate(32000);
update c;
}
return res.getBody();
}
public class MyException extends Exception{}
}
Make new "Lighning Component" in developer console (it'll be Aura, not LWC). You can tick the last checkbox about "lightning quick action". Name can be same as class but doesn't have to be.
For component (~ html part) paste this
<!-- Loosely based on https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm -->
<aura:component controller="Stack63364119" implements="force:hasRecordId,force:lightningQuickAction" >
<!-- in the name of all that is holy do not name the JS function same as the Apex class function, it'll give you very cryptic errors to debug -->
<aura:handler name="init" value="{!this}" action="{!c.runCallout}"/>
</aura:component>
For controller (~ JavaScript) paste this
({
runCallout : function(cmp) {
let action = cmp.get('c.doCallout');
action.setParams({contactId : cmp.get('v.recordId')});
action.setCallback(this, function(response){
let state = response.getState();
if (state === "SUCCESS") {
alert('Saved OK: ' + response.getReturnValue());
$A.get("e.force:closeQuickAction").fire(); // if you want to self-close
} else if (state === "ERROR") {
var errors = response.getError();
if (errors) {
if (errors[0] && errors[0].message) {
console.log("Error message: " + errors[0].message);
}
} else {
console.log("Unknown error");
}
}
});
$A.enqueueAction(action);
}
})
Finally go Object Manager -> Contact -> Buttons Links and Actions. Create new Quick Action.
And add it to page layout(s)!
It should get you started. Maybe you'll decide to split it a bit, Apex would only do the callout, return results to UI and if user is happy - updating the contact can be done with one of these: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/data_service_save_record.htm. Separation of concerns blah blah (but will the user be happy with 2 clicks).
P.S. If you tweak it and it dies but it's hard to see any JavaScript errors - it'll be because default is to run in release mode, SF rewrites your source code a bit, optimises, polyfills for "browsers" like IE11... Go to Setup -> Debug Mode and enable for your user.
P.P.S. In sandbox / dev org it's good idea to go Setup -> Session Settings -> and untick "Enable secure and persistent browser caching to improve performance". Your component will be always fresh, saves some frantic hitting Ctrl+R. Don't do it in prod ;)
I will write to you my approach
Button on the record page/page layout -> lighting component or flow -> Apex class for collecting data -> apex class request & response API(don't forget to add the endpoint to remote site settings)-> parse response and update contact (you can use queries and DML operations inside Apex)
lighting component it will be very simple just have a the apex class as the controller example
<aura:component implements="force:appHostable,lightning:isUrlAddressable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" controller="contactController" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.fetchContact}"/>
</aura:component>
controller
({
fetchContact : function(component, event, helper) {
helper.fetchContactHelper(component, event, helper);
}
})
helper
({
fetchAccHelper : function(component, event, helper) {
var action = component.get("c.fetchContacts");
action.setParams({
});
action.setCallback(this, function(response){
var state = response.getState();
if (state === "SUCCESS") {
}
});
$A.enqueueAction(action);
}
})
assuming is apex function is fetchContacts and class contactController
I've recently upgraded my joomla site to 3.9.1, and now my custom components publish/unpublish buttons aren't working and I can't figure out why. The database column is called "state" and this is the code which was working up until the upgrade:
JToolbarHelper::publish('items.publish', 'JTOOLBAR_PUBLISH', true);
JToolbarHelper::unpublish('items.unpublish', 'JTOOLBAR_UNPUBLISH', true);
These buttons used to work and they would publish / unpublish the items, now I get this message:
0 items successfully published
I'm not sure where exactly the code is which tells it to update the status of the item, but for some reason it's not working anymore.
Try to add following line to the __construct function within "table" file:
$this->setColumnAlias('published', 'state');
so it looks like
public function __construct(&$db)
{
parent::__construct('#__your_custom_table', 'id', $db);
$this->setColumnAlias('published', 'state');
}
Works for my custom component.
I'm creating about 500 pages in a new Umbraco v6.1.6 website using an import script I've coded. I'm using the ContentService api to create the new pages. They are created and seem to save fine. However if I request the value of a checbox list from one of the new pages, I get an empty string.
I've verified that the property is empty in the umbraco.config file however If I manually save the page from the Umbraco back office. the cache will update with the correct value and I suddenly get the correct value returned.
Is there a way to force a cache update or some other form of fix for this issue?
This is the CreateContent method I'm using:
public static IContent CreateContent(string name, string documentTypeAlias, int parentId, Dictionary properties, bool publish = false, int author = 0)
{
IContent document = null;
ContentService contentService = new ContentService();
document = contentService.CreateContent(
name, // the name of the document
parentId, // the parent id should be the id of the group node
documentTypeAlias, // the alias of the Document Type
author);
foreach (string property in properties.Keys)
{
document.SetValue(property, properties[property]);
}
// If publish is true, then save and publish the document
if (publish)
{
contentService.SaveAndPublish(document);
}
// Else, just save it
else
{
contentService.Save(document);
}
return document;
}
Edit:
After looking into the database, I can see that cmsContentXml has the property but the data within it is the same as umbraco.config. I looked into cmsPropertyData and the data is present. So I guess the question is how do I get the data from cmsPropertyData to cmsContentXml?
My Question is simelar to this one: https://stackoverflow.com/questions/17722347/umbraco-6-1-1-when-i-publish-content-via-the-content-service-tags-type-property however it has no replies.
The data from cmsContentXml gets dumped directly into the umbraco.config file so thankfully these are the same.
To make your code a bit more DRY (you're always saving the document in both the if and the else, try this and update the SaveAndPublish method to Publish (and in v7 you can use PublishWithResult to get a detailed result of the publish action):
contentService.Save(document);
// If publish is true, then save and publish the document
if (publish)
{
contentService.Publish(document);
}
In v7 you could then have a look at the publishResult. If there's something wrong then this will tell you what it is. Most likely it'll just work fine this way though (which could mean that SaveAndPublish is broken, but let's figure out if publishResult has errors).
I am using a simple form to submit some new document info, and would like the user to be able to hit a 'save and new document' button so he can easily add multiple documents.
I've tried to add a button to the form which works the first time I hit the 'save&new' button, and pops up another form over the existing one, but after that the button stops working.
possibly because I haven't properly closed the previous one, when spawning the new one?
how would I do that?
I'v tried using closeDialog() before calling the new dialogURL() but that obviously does not work...
(simplified) code sample follows:
class page_informa_documento extends Page {
function init(){
parent::init();
$f=$this->add('Form');
$f->setModel('Document');
$f->addSubmit('Save');
$f->addButton('Save and new document')->js('click',$f->js()->atk4_form('submitForm','otro'));
if($f->isSubmitted() )
{
// save document info we just got here
$doc->save();
if ($f->isClicked('otro'))
$f->js()->univ()->dialogURL('New Document',$this->api->getDestinationURL('/informa/documento'))->execute();
else $f->js()->univ()->closeDialog()->execute();
}
If you need form data, you must add multiple Submit buttons.
$a=$f->addSubmit('Save');
$b=$f->addSubmit('Save and Add More');
if($f->isSubmitted()){
// save your document here
if($f->isClicked($a)){
$this->js()->univ()->location($this->api->url('index'))->execute();
// go to index page, we are done
}
if($f->isClicked($b)){
$this->js()->univ()->location($this->api->url()->execute();
// stay on the same page, just reload
}
}
Here is a demo: http://test-suite.agiletoolkit.org/?page=form
Source is taken from here: https://github.com/atk4/atk4-testsuite/blob/master/page/form.php#L16
In my CakePHP 2 application i have a problem with beforeFilter.
In this thread it worked well. Because of old version of CakePHP.
In my code, If user is not authorized, I want to show him "anotherview.ctp".
I don't want to redirect visitor to another page. (because of adsense issues)
When i use "this->render" in beforeFilter, the code in my "index" action is also run.
I want to stop the execution after the last line of "beforeFilter".
When I add "exit()" to beforeFilter, it broke my code.
How can I stop execution in beforeFilter without breaking code?
class MyController extends AppController {
function beforeFilter() {
if ( $authorization == false ) {
$this->render('anotherview');
//exit();
}
}
}
function index() {
// show authorized staff
}
}
Try:
$this->response->send();
$this->_stop();
I stumbled across this thread while trying to do the same thing. The accepted answer works but omits an important detail. I'm trying to render a layout (not a view) and I had to add an additional line to prevent the original request's view from throwing errors.
Inside AppController::beforeFilter():
$this->render(FALSE, 'maintenance'); //I needed to add this
$this->response->send();
$this->_stop();
or alternatively - redirect to another view:
if ( $authorization == false ) {
$this->redirect('/users/not_authorized');
}
For CakePHP 3.5 this is what worked for me:
$event->setResult($this->render('anotherview'));
This will also enable you to use the debugger. CakePHP Debugger stopped working when I used the exit; statement.