I'm trying to update one record with
$this->Model->id = 1;
$this->Model->save($this->request->data);
and to use validation rule 'on'=>'update'.
Is that possible and how :) ?
Solution to my problem was to remove validation rule for the field that i don't want to validate when i update record.
$this->Model->validator()->remove('fieldName');
$this->Model->id = 1;
$this->Model->save($this->request->data);
Related
Hello please help my friend to write code, his task is
No contacts should be inserted or updated if email does not has " com. Net" email is a custom filed on contact. I want you to do it by trigger this trigger should only fire on insert and change of email
code he write
Code he write :
trigger InsertedUpdated on Contact (before insert,before update){
for(contact con:trigger.new)
if( con.Email=='xyz#gmail.com'){
// con.Last='.com /.net';
}
for( contact con:trigger.new ){
if(con.Email!=trigger.Oldmap.get(con.Id).Email || con.Email!=trigger.Oldmap.get(con.Id).Email){
if( con.Email=='xyz#gmail.com' ){
con.adderror('Email does not correct ');
}
x
}
}
}
Is it necessary to use a trigger? A validation rule seems better suited to this use case, and will provide friendlier errors when violated.
You can find information on using regex in a validation rule here
If it is absolutely necessary to use a trigger, this page has an example of how to write a regex in apex.
i am working on a cakephp 2.x .want to add a condition into my save query .. for example i want to implement this query
INSERT INTO 'this' where condition is 'this'
right now i am doing this
$count = $this->Message->find('count', array(
'conditions' => array('Message.mobileNo' => $mobileNo,
'Message.body'=>$body
)));
if($count>0){
echo "already have a message";
}else
{
$this->Message->create();
$this->Message->save($this->request->data);
}
at times now i am first checking through count and then saving into the database ... can i add condition into my save so i dont have to query two times into database just to accomplish one task
This is not really CakePHP question rather than MySQL. But you can't do this since the INSERT query doesn't have conditional query.
There are 2 ways of doing it:
As Mark in the comment said use validation. The validation although apply to single field, so it will be quite tricky to do it.
Use beforeValidate() or beforeSave() callbacks in your model to check this and if they return false the save operation wont be executed.
You can put UNIQUE index to your table so it won't allow insertion of phone+message together.
I would go with the method 2.
try the beforeSave methode in your Model
You can use the following code in a better way:
$conditions = array('Message.mobileNo' => $mobileNo,
'Message.body'=>$body);
if ($this->Message->hasAny($conditions)){
echo "already have a message";
}
else{
$this->Message->create();
$this->Message->save($this->request->data);
}
Here is the problem I'm trying to solve:
When a new TASK/EVENT is created, if the user is a certain profile - we want to update a field on CONTACT with the day the T/E was created.
I tried doing a workflow rule and field update- but I couldn't get it to work... I think since this is a Standard -Standard object relationship via lookup, it might have a problem doing a field update. Any other ideas?? I'd prefer to use the platform for this one...
HALP!
Thanks
We have done exactly what you are talking about via an Apex trigger. Something like this...
//I'm sure this doesn't compile, but it gives you the idea
trigger taskTrigger on Task( after insert, after update ){
Task t = trigger.new ;
Contact contact = [Select Id from Contact where Id = :t.whoId] ;
contact.yourfield = t.AcitivityDate ;
update contact ;
}
I have custom object KeywordAccountAssociation__c. This object has three fields
Account__c - Master-Detail(Account)
Keyword__c - Master-Detail(Keyword)
Compositecp__c - Text(255) (External ID) (Unique Case Sensitive)
I have a custom field in Account
DD_Segment__c - multi-picklist
Now I want to update (Insert is fine too) values of DD_Segment__c whenever KeywordAccountAssociation__c is updated. I could write trigger for this but I don't know how? I am new to Salesforce Development and my background is ruby (so getting accustomed to apex is bit difficult for me).
KeywordAccountAssociation__c has multiple rows of Account__c which has same account_id and those account_id are related to a record of custom object Keyword__c. I want to get all keywords related to one account and update in its (account's) multi-picklist. How can I achieve this? If you have doubts about this please do ask. Thanks!
One issue is related to learning to work with triggers in general, which can be started with Salesforce Apex Developer Documents on Triggers
but to answer your actual question, you would essentially need to build a trigger against your custom object that would update the related account. It might look something like this:
trigger keywordAccountUpdate on KeywordAccountAssociation__c (after insert, after update){
set<id> = new set<id>();
for (KeywordAccountAssociation__c a : Trigger.new)
accountIds.put(a.Account__c);
map<id,Account> accountMap = new map<id,Account>([select id, DD_Segment__c from Account where id in :accountIds]);
for (KeywordAccountAssociation__c kaa : Trigger.new){
if (AccountMap.containskey(kaa.Account__c)){
Account thisAccount = AccountMap.get(kaa.Account__c);
String s = thisAccount.DD_Segment__c + ';new value'; // Always add value
if ((thisAccount.DD_Segment__c).contains('second value')
s += ';second value';
AccountsToUpdate.add(new Account(id=thisAccount.id, DD_Segment__c = s));
}
}
}
Please keep in mind that I don't have the structure to test this trigger, I just free handded it, so YMMV.
I have a selection drop down menu, and I want the user to select two options for a field in the database. The problem is how do I make it to disallow duplication of select options, currently it's saving all options even when they are the same.
Code in my add.ctp for the select options is:
echo $this->Form->select("ProgrammeChoice.programme_code.0",$finals);
echo $this->Form->select("ProgrammeChoice.programme_code.1",$finals);
And the variable $finals is bringing the select options from another table in the database, it's in the controller and the code is:
$finals = array_merge($filtered_programs,$non_preq_programs);
So please, I want help to validate my select menu to deny duplicate selections on submission.
Create a custom validation rule and compare the values as described here: http://book.cakephp.org/2.0/en/models/data-validation.html#adding-your-own-validation-methods
In the validation method your data is stored in $this->data.
It should look similar to this:
public function compare($field1) {
if($field1 === $this->data['ProgrammeChoice']['programm_code']['1']) {
return false;
}
return true;
}