Hello everyone
I want to know how can i block creation 2 contracts with the same NAME, DEPARTEMENT and YEAR(of the start_date) ?
Hi,
One option would be to add a before_save
logic hook which checks for another contract and, if it finds it redirects with an error.
I.e.
SugarApplication::appendErrorMessage('Cannot create a duplicate contract');
SugarApplication::redirect('index.php?module=theModule&record=theId&action=DetailView');
sugar_die('');
You can tweak this to take different actions if need be.
Thanks,
Jim
Thank you for your answer
for my case: to create one contract , i must select
Dept (because a relationship between DEPT and CONTRACT is one to many)
And to check if the contract already exist i did a QUERY which returns me the number of contracts which DEPT is DEPT selected when creating.
The query is as follows :
$sql_query = “SELECT *
FROM p_asw_contrat
c
JOIN p_asw_dept_p_asw_contrat_1_c
dc
ON c.id=dc.p_asw_dept_p_asw_contrat_1p_asw_contrat_idb
WHERE dc.=’$bean->p_asw_dept_id_c’
AND c.name=’$bean->name’
AND YEAR(start_dateC) =YEAR(’$bean->p_asw_start_dateC’)
AND c.deleted=0”;
$res_query = mysql_query($sql_query,$link);
$num_of_record = mysql_num_rows($res_query);
if ($num_of_record != 0){
SugarApplication::appendErrorMessage('Cannot create a duplicate contract');
SugarApplication::redirect('index.php?module=theModule&record=theId&action=DetailView');
sugar_die('');
}
I problem I encounter is that I can not get the id of the selected DEPT,
I used : $bean->p_asw_dept_id_c , but it doesnt work
You could do this a little simpler using the bean instead of a query. Check out the following: Using the Bean
I think there are a few errors in your query to be honest.
WHERE dc.='$bean->p_asw_dept_id_c'
Should be something like
WHERE dc.id='$bean->p_asw_dept_id_c'
Also I would try to be a little more internally consistent just for readability later.
P.S. I usually open up PHP MyAdmin (or whatever you use) and test out these queries rather than writing them up directly in the logic hook, makes it easier to identify issues.
sorry for the typo, the where the condition is as follows
WHERE dc.p_asw_dept_p_asw_contrat_1p_asw_dept_ida=’$bean->p_asw_dept_id_c’
I had already tested the query on PHPMYADMIN , using the hard-codes (ie using an existing DEPT_ID), and it works, but when I use the variable $bean within the query that does not work
Weird, I would write a little bit of code which fetches all of the values using the bean and returns them just to make sure that your calls are working properly.
Also just double check that you have your concatenation working properly, if you return $sql_query see what you get.
When I made an echo $sql_query, I obteint:
SELECT *
FROM p_asw_contrat
c
JOIN p_asw_dept_p_asw_contrat_1_c
dc
ON c.id=dc.p_asw_dept_p_asw_contrat_1p_asw_contrat_idb
WHERE dc.= ‘’
AND c.name=‘Contract_test’
AND YEAR(start_dateC) =YEAR(‘2015’)
AND c.deleted=0
I can get all values of $bean except that of DEPT_ID
Hi Mr JIM ,
and thanks u for ur response
Concerning the solution you proposed to me, I had tested for two custom modules, except that here it works for redirecting a module and it does not work for the other