customize the prospects exports of a prospectlist in SugarCrm - sugarcrm

I using SugarCRM and when i exports prospects I can get all the fields, including the custom filed I added. but when i try to download the prospects from prospect list then only a few fields are exporting from the database. I need to get all the fields like the prospect download
I understand that Prospect_list.php file contains the code for exporting this one and I changed this one but database failure happening please help me to do this one.
$prospects_query = "SELECT p.id AS id, 'Prospects' AS related_type, '' AS \"name\", p.first_name AS first_name, p.last_name AS last_name,p.title AS title, p.salutation AS salutation,
p.primary_address_street AS primary_address_street,p.primary_address_city AS primary_address_city, p.primary_address_state AS primary_address_state, p.primary_address_postalcode AS primary_address_postalcode, p.primary_address_country AS primary_address_country,
p.account_name AS account_name,
ea.email_address AS email_address, ea.invalid_email AS invalid_email, ea.opt_out AS opt_out, ea.deleted AS ea_deleted, ear.deleted AS ear_deleted, ear.primary_address AS primary_address,
p.do_not_call AS do_not_call, p.phone_fax AS phone_fax, p.phone_other AS phone_other, p.phone_home AS phone_home, p.phone_mobile AS phone_mobile, p.phone_work AS phone_work , p.description As Description
FROM prospect_lists_prospects plp
INNER JOIN prospects p ON plp.related_id=p.id
LEFT JOIN email_addr_bean_rel ear ON ear.bean_id=p.id AND ear.deleted=0
LEFT JOIN email_addresses ea ON ear.email_address_id=ea.id
WHERE plp.prospect_list_id = $record_id AND plp.deleted=0
AND p.deleted=0
AND (ear.deleted=0 OR ear.deleted IS NULL)";
here the sugar team specified the fields to export but i need to get all the fields in the table[prospects] how to do this one.

Trying changing the query to this...
$prospects_query = "SELECT p.id AS id, 'Prospects' AS related_type, '' AS 'name', p.first_name AS first_name, p.last_name AS last_name,p.title AS title, p.salutation AS salutation,
p.primary_address_street AS primary_address_street,p.primary_address_city AS primary_address_city, p.primary_address_state AS primary_address_state, p.primary_address_postalcode AS primary_address_postalcode, p.primary_address_country AS primary_address_country,
p.account_name AS account_name,
ea.email_address AS email_address, ea.invalid_email AS invalid_email, ea.opt_out AS opt_out, ea.deleted AS ea_deleted, ear.deleted AS ear_deleted, ear.primary_address AS primary_address,
p.do_not_call AS do_not_call, p.phone_fax AS phone_fax, p.phone_other AS phone_other, p.phone_home AS phone_home, p.phone_mobile AS phone_mobile, p.phone_work AS phone_work , p.description As Description
FROM prospect_lists_prospects plp
INNER JOIN prospects p ON plp.related_id=p.id
LEFT JOIN email_addr_bean_rel ear ON ear.bean_id=p.id AND ear.deleted=0
LEFT JOIN email_addresses ea ON ear.email_address_id=ea.id
WHERE plp.prospect_list_id = '$record_id' AND plp.deleted=0
AND p.deleted=0
AND (ear.deleted=0 OR ear.deleted IS NULL)";

Related

Getting duplicate column ERROR while trying to insert same column with two different datatypes using SELECT INTO clause in PostgreSql

I need to insert createdate column twice with two different datatypes one with the datatype defined in the table itself and another in char datatype.
I can insert it by changing the alias name of createdate column but can't insert with same alias name which i need.
so help me out to get correct way of doing it.
My query:
SELECT DISTINCT TE.id, T.debatchqueuelink, TE.transactionlink,
EC.errorclassification, TE.errorvalue,
EC.errorparameter, TE.classificationlink, TE.description,
TE.createdate AS createdate, TO_CHAR(TE.createdate, 'MM/dd/yyyy') AS createdate,
TE.status, TE.rebutt, TE.rebuttedstatus, BQ.appbatchnumber,
BQ.scanbatchnumber, BQ.clientlink, BQ.locationlink, T.patientid,
(DEUD.firstname|| ' ' ||DEUD.lastname) AS deusername, DEUD.email AS deuseremail,
(QCUD.firstname|| ' ' ||QCUD.lastname) AS qcusername, TE.inactive,
TE.decomment
INTO table373
FROM qctransactionerror TE
INNER JOIN errorclassification EC ON EC.id = TE.classificationlink
INNER JOIN qctransaction T ON T.id = TE.transactionlink
INNER JOIN batchqueue BQ ON T.debatchqueuelink = BQ.id
INNER JOIN batchqueue QCBQ ON T.qcbatchqueuelink = QCBQ.id
INNER JOIN userdetail QCUD ON QCBQ.assignedto = QCUD.id
INNER JOIN userdetail DEUD ON BQ.assignedto = DEUD.id
WHERE TE.inactive='t'
AND TE.status IN ('ERROR','QCCORRECTED')
LIMIT 0
The actual error message I am getting is:
Duplicate column:column "createdate" specified more than once

Keycloak - Get all Users mapped to roles

I know keycloak has exposed below api,
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-services</artifactId>
<version>2.0.0.Final</version>
</dependency>
With complete documentation here. I cannot find the required api here to fetch all users with specific role mapped to them.
Problem Statement - I need to pick all users from keycloak server who have a specific role. I need to send email to all users with role mapped to them.
Based on the documentation it appears to be this API:
GET /{realm}/clients/{id}/roles/{role-name}/users
It is there for a while. In this older version however it was not possible to get more than 100 users this way. It was fixed later and pagination possibility was added.
There is an outstanding feature request asking for this function via the API.
In the meantime if your requirement is once-off you could obtain the user names (or email addresses) by interrogating the database joining KEYCLOAK_ROLE to USER_ROLE_MAPPING to USER_ENTITY
Something like:
SELECT username
FROM keycloak_role kr
JOIN user_role_mapping rm ON kr.id = rm.role_id
JOIN user_entity ue ON rm.user_id = ue.id
WHERE kr.name = 'your_role_name';
This should be now possible with the updated rest endpoint.
Set<UserRepresentation> usersOfRole = realmResource.roles().get(roleName).getRoleUserMembers();
Here is another interesting query, which would also display other useful fields.
SELECT kr_role.REALM_ID 'Realm', cl.CLIENT_ID 'Realm Client',
kr_role.NAME 'Role Name',
kr_role.DESCRIPTION 'Role Description',
user_ent.USERNAME 'Domain ID', user_ent.EMAIL 'Email'
FROM keycloak_role kr_role, user_role_mapping role_map,
user_entity user_ent, client cl
WHERE role_map.USER_ID = user_ent.ID
AND kr_role.ID = role_map.ROLE_ID
AND kr_role.CLIENT = cl.ID
AND cl.REALM_ID = '<realm_name>'
AND cl.CLIENT_ID = '<client_name>'
ORDER BY 1, 2, 3;
If anyone is still searching for a Postgres Query to find information regarding users/roles/groups in keycloak database, I came up with this one lately.
It uses two CTEs to have the groups and roles straight (recursing for groups in groups, because they can nest in arbitrary depth and fetching composite roles with their parents) and a simple UNION for group and direct assignments.
Please note the WHERE clause, where you can limit the realm and different aspects. You can search for
all roles from a specific user (just matching username)
all users, that have a particular role assigned (matching role_name)
everything coming from a specific group (I sometimes use it without the username column in the projection to just see, what roles a group has. Please note the prefix in the group column)
-- flat out GROUPS in GROUPS
WITH RECURSIVE groups AS (
SELECT
g.id,
g.id AS parent_group,
g.name,
g.name AS parent_name,
g.realm_id,
1 AS iter
FROM
keycloak_group g
UNION
SELECT
groups.id,
parents.parent_group,
groups.name,
parents.name,
groups.realm_id,
groups.iter + 1
FROM
groups
INNER JOIN keycloak_group parents ON groups.parent_group = parents.id
),
-- Collect roles and composite roles
roles AS (
SELECT
r.id,
r.name AS role_name,
null AS base_role,
c.client_id
FROM
keycloak_role r
LEFT JOIN client c ON r.client = c.id
UNION
SELECT
r.id,
r2.name,
r.name,
c.client_id
FROM
keycloak_role r
JOIN composite_role cr ON r.id = cr.composite
JOIN keycloak_role r2 ON r2.id = cr.child_role
LEFT JOIN client c ON r.client = c.id
)
SELECT DISTINCT
username,
role_name,
base_role, -- for composite roles
client_id,
source,
realm_name
FROM
(
-- Roles from Groups
SELECT
ue.username,
roles.role_name,
roles.base_role,
roles.client_id,
ue.realm_id,
'group ' || g.name AS source,
realm.name AS realm_name
FROM
user_entity ue
JOIN realm ON ue.realm_id = realm.id
JOIN user_group_membership ugm ON ue.id = ugm.user_id
JOIN groups g ON g.id = ugm.group_id
JOIN group_role_mapping grm ON g.parent_group = grm.group_id
JOIN roles roles ON roles.id = grm.role_id
UNION
-- direct role assignments on User
SELECT
ue.username,
roles.role_name,
roles.base_role,
roles.client_id,
ue.realm_id,
'direct',
realm.name
FROM
user_entity ue
JOIN realm ON ue.realm_id = realm.id
JOIN user_role_mapping urm ON ue.id = urm.user_id
JOIN roles roles ON roles.id = urm.role_id
) AS a
WHERE
realm_name = 'realm_name'
AND (
-- username = 'username'
role_name IN ('roleName')
-- source = 'group GROUPNAME'
)
ORDER BY
username,
role_name
;
This query works from keycloak 9 to 16.1.1 (the last jboss/keycloak version I got from docker hub).
SELECT username,
kr.NAME,
kr.REALM_ID
FROM KEYCLOAK_ROLE kr
JOIN USER_ROLE_MAPPING rm ON kr.id = rm.role_id
JOIN USER_ENTITY ue ON rm.user_id = ue.id
ORDER BY USERNAME,
NAME,
REALM_ID;

SBO Layout bring random record incase of draft or unadded documents

am having a layout which is working fine , except for one part ,
If we use this layout to view or print any added document (AR Invoice) it will ring the write data and records.
but if the user click view or print before adding the document to the system , or if the document was saved as draft then : a random records will come into the layout , in a very randomly way , meaning if close the view and open it again another record different than the precious shown one will come .
What am looking for is simple as i thought , if a user tried to view or print a document thats not added into system then show blank page ,
any help ?
here is the query am using in CR
SELECT ohem.[U_Employee_Code] as'Cashier',OUSR.USER_CODE, OUSR.U_NAME,OUDG.[Code],OUDG.Phone1,OUDG.Phone2, OINV.DocEntry,oinv.docnum, OINV.DocType, OINV.DocStatus, OINV.InvntSttus, OINV.DocDate, OINV.DocDueDate, OINV.CardCode, nnm1.seriesname,
OINV.Address2, OINV.Printed, OINV.CardName, OINV.U_Address, OINV.NumAtCard, OINV.VatSum, OINV.DiscPrcnt, OINV.DiscSum, OINV.DocTotal, OINV.DocRate, OINV.u_deldate,
OINV.ReceiptNum, OINV.GroupNum, OINV.OwnerCode,OINV.JrnlMemo, OINV.VatPaid, OINV.Address2 AS Expr2, OINV.U_CUSTNAME1 as 'Customer Name' , OINV.U_PHONE1, OINV.ExepAmnt, OINV.ExepAmntSC, OINV.ExepAmntFC, OINV.VatDate,OINV.TotalExpns,
OINV.PaidSum, OINV.OwnerCode, OINV.BillToOW, OINV.ShipToOW, OINV.RetInvoice, OINV.ReqName, OINV.Requester, INV1.ItemCode, INV1.Dscription, INV1.Quantity, inv1.PriceBefDi,
INV1.SubCatNum, INV1.ShipDate, INV1.Price, INV1.Currency, INV1.Rate, INV1.DiscPrcnt AS Expr1, INV1.LineTotal, INV1.OpenSum, INV1.VatPrcnt, INV1.PriceAfVAT,
INV1.unitMsr, OINV.TaxDate, INV1.GTotal, OINV.Comments, OCTG.PymntGroup, OINV.LicTradNum, INV1.BaseDocNum, INV1.LineVat, OINV.WTSum, oinv.u_cancelled,
OCRN.CurrName, oslp.SlpName,OUDG.Phone1,OUDG.phone2,
oinv.u_empid as'Sales man',OUDG.Phone1 as 'Branch Phone',OUDG.phone2 as 'Customer Support Phone',OINV.U_PHONE1 as 'Customer Phone',inv1.shipdate as 'Item Delivery Date', OINV.TotalExpns AS 'FREIGHT CHARGES',OINV.U_Address as 'Customer Address',oinv.UpdateDate, OINV.DocEntry
FROM OINV INNER JOIN
INV1 ON OINV.DocEntry = INV1.DocEntry INNER JOIN
OUSR ON OINV.UserSign = OUSR.USERID INNER JOIN
OCTG ON OINV.GroupNum = OCTG.GroupNum INNER JOIN
OCRN ON OINV.DocCur = OCRN.CurrCode inner join
oslp on oinv.SlpCode = oslp.SlpCode INNER JOIN
OUDG ON OUSR.[DfltsGroup] = OUDG.[Code] inner join
nnm1 on OINV.series = nnm1.series LEFT JOIN
OHEM on OUSR.[userId] = OHEM.[USERID]
where OINV.DocEntry = {?DocKey#}
the solution is to use ObjectId to determine if the source is system document or draft . this is it
if {?ObjectId#} =13
SELECT ohem.[U_Employee_Code] as'Cashier',OUSR.USER_CODE, OUSR.U_NAME,OUDG.[Code],OUDG.Phone1,OUDG.Phone2, OINV.DocEntry,oinv.docnum, OINV.DocType, OINV.DocStatus, OINV.InvntSttus, OINV.DocDate, OINV.DocDueDate, OINV.CardCode, nnm1.seriesname,
OINV.Address2, OINV.Printed, OINV.CardName, OINV.U_Address, OINV.NumAtCard, OINV.VatSum, OINV.DiscPrcnt, OINV.DiscSum, OINV.DocTotal, OINV.DocRate, OINV.u_deldate,
OINV.ReceiptNum, OINV.GroupNum, OINV.OwnerCode,OINV.JrnlMemo, OINV.VatPaid, OINV.Address2 AS Expr2, OINV.U_CUSTNAME1 as 'Customer Name' , OINV.U_PHONE1, OINV.ExepAmnt, OINV.ExepAmntSC, OINV.ExepAmntFC, OINV.VatDate,OINV.TotalExpns,
OINV.PaidSum, OINV.OwnerCode, OINV.BillToOW, OINV.ShipToOW, OINV.RetInvoice, OINV.ReqName, OINV.Requester, INV1.ItemCode, INV1.Dscription, INV1.Quantity, inv1.PriceBefDi,
INV1.SubCatNum, INV1.ShipDate, INV1.Price, INV1.Currency, INV1.Rate, INV1.DiscPrcnt AS Expr1, INV1.LineTotal, INV1.OpenSum, INV1.VatPrcnt, INV1.PriceAfVAT,
INV1.unitMsr, OINV.TaxDate, INV1.GTotal, OINV.Comments, OCTG.PymntGroup, OINV.LicTradNum, INV1.BaseDocNum, INV1.LineVat, OINV.WTSum, oinv.u_cancelled,
OCRN.CurrName, oslp.SlpName,OUDG.Phone1,OUDG.phone2,
oinv.u_empid as'Sales man',OUDG.Phone1 as 'Branch Phone',OUDG.phone2 as 'Customer Support Phone',OINV.U_PHONE1 as 'Customer Phone',inv1.shipdate as 'Item Delivery Date', OINV.TotalExpns AS 'FREIGHT CHARGES',OINV.U_Address as 'Customer Address',oinv.UpdateDate
FROM OINV INNER JOIN
INV1 ON OINV.DocEntry = INV1.DocEntry INNER JOIN
OUSR ON OINV.UserSign = OUSR.USERID INNER JOIN
OCTG ON OINV.GroupNum = OCTG.GroupNum INNER JOIN
OCRN ON OINV.DocCur = OCRN.CurrCode inner join
oslp on oinv.SlpCode = oslp.SlpCode INNER JOIN
OUDG ON OUSR.[DfltsGroup] = OUDG.[Code] inner join
nnm1 on OINV.series = nnm1.series LEFT JOIN
OHEM on OUSR.[userId] = OHEM.[USERID]
where OINV.DocEntry = {?DocKey#}
else select ' '

Highlighting duplicate values

I have the following script:
SELECT
Reference.ReferenceNumber AS [Reference.ReferenceNumber)
Reference.LastName
Reference.FirstName
Address.ReferenceNumber AS [Address.ReferenceNumber]
Address.Address1
Address.Address2
Address.Address3
Address.Address4
Address.ZipCode
Telephone.ReferenceNumber AS [Telephone.ReferenceNumber]
Telephone.TelephoneNumber
Email.ReferenceNumber AS [Email.ReferenceNumber]
Email.EmailAddress
FROM
Reference
INNER JOIN Address
ON Reference.ReferenceNumber = Reference.ContactNumber
LEFT OUTER JOIN Telephone
ON Reference.ReferenceNumber = Telephone.ReferenceNumber
LEFT OUTER Join Email
ON Reference.ReferenceNumber = Email.ReferenceNumber
This pulls through all customers, plus their addresses, email and phone numbers.
However some have duplicate surnames and addresses.
Please can you advise an amendment to the script that would show which records were duplicates? Ideally I'd like the output to show where the surname, address 1 and the zipcode were identical and restrict the output to the duplicate rows only.
Many thanks.
something like
SELECT
Reference.ReferenceNumber AS [Reference.ReferenceNumber),
case when vrln.c is null then Reference.LastName else 'dup: ' + Reference.LastName end as LastName,
Reference.FirstName,
Address.ReferenceNumber AS [Address.ReferenceNumber],
Address.Address1,
Address.Address2,
Address.Address3,
Address.Address4,
Address.ZipCode,
Telephone.ReferenceNumber AS [Telephone.ReferenceNumber],
Telephone.TelephoneNumber,
Email.ReferenceNumber AS [Email.ReferenceNumber],
Email.EmailAddress
FROM
Reference
INNER JOIN Address
ON Reference.ReferenceNumber = Reference.ContactNumber
LEFT OUTER JOIN Telephone
ON Reference.ReferenceNumber = Telephone.ReferenceNumber
LEFT OUTER Join Email
ON Reference.ReferenceNumber = Email.ReferenceNumber
left join (
select rln.LastName, add.Address1, add.ZipCode, count(rln.LastName) as c
from
Reference rln
INNER JOIN Address add
ON rln.ReferenceNumber = add.ReferenceNumber
group by rln.LastName, add.Address1, add.ZipCode
having count(rln.LastName) > 1
) vrln on Reference.LastName = vrln.LastName and Address.Address1 = vrln.Address1 and Address.ZipCode = vrln.ZipCode
where vrln.c is not null
but, to find duplicates :
select rln.LastName, add.Address1, add.ZipCode, count(rln.LastName) as c
from
Reference rln
INNER JOIN Address add
ON rln.ReferenceNumber = add.ReferenceNumber
group by rln.LastName, add.Address1, add.ZipCode
having count(rln.LastName) > 1
is enough

Need some SQL Stored proc help - joining

So I have this section of my proc:
SELECT
com_contact.rc_name_full as CreatedBy,
capComponent.cm_strike as CapStrike,
floorComponent.cm_strike as FloorStrike,
tq_nominal_notional as Notional,
maxComponent.cm_effective_dt as EffectiveDate,
maxComponent.cm_maturity_dt as MaturityDate,
CAST(CAST(DATEDIFF(mm,maxComponent.cm_effective_dt,maxComponent.cm_maturity_dt) as decimal(9,2))/12 as decimal(9,2)) as term,
(
CASE WHEN se_amort_term_mnth IS NOT NULL THEN se_amort_term_mnth / 12
ELSE CAST(CAST(DATEDIFF(mm,
ISNULL(cmam_amortization_start_dt, maxComponent.cm_effective_dt),
cmam_amortization_end_dt) as decimal(9,2))/12 as decimal(9,2))
END
) AS AmortTermYears,
tq_dd_product as Product,
dh_key_rate as KeyRate,
dh_pv01 as PV01,
dh_val_time_stamp as RateTimeStamp,
re_bnk_le.re_company_name as Company,
rc_contact_id as UserId,
stp_name as NickName,
'' as project,
'' as Borrower,
'' as Lender,
'' as AdditionalInfo,
CASE WHEN tpm_pd_permission_id = 85 THEN 'LLH' WHEN tpm_pd_permission_id = 86 THEN 'ALM' ELSE '' END as Permission,
tr_transaction_id as TransactionId,
NULL as IndicationId
FROM cfo_transaction
The line that says '' as project, we have to actually change to return data now.
The table that next to the FROM, called cfo_transaction has an id on it called tr_transaction_id. We have another table called com_project_transaction_link, that links those id's with project id's, using two two columns called:
pt_tr_transaction_id and pt_pj_project_id, and then we have a table containing all the projects called com_project that has a pj_project_id and a pj_project_name.
GOAL: return the pj_project_name from that projects table where it links with the transactions being pulled.
I really don't know how to do this.
Thanks!
Try this:
SELECT
com_contact.rc_name_full as CreatedBy,
capComponent.cm_strike as CapStrike,
floorComponent.cm_strike as FloorStrike,
tq_nominal_notional as Notional,
maxComponent.cm_effective_dt as EffectiveDate,
maxComponent.cm_maturity_dt as MaturityDate,
CAST(CAST(DATEDIFF(mm,maxComponent.cm_effective_dt,maxComponent.cm_maturity_dt) as decimal(9,2))/12 as decimal(9,2)) as term,
(
CASE WHEN se_amort_term_mnth IS NOT NULL THEN se_amort_term_mnth / 12
ELSE CAST(CAST(DATEDIFF(mm,
ISNULL(cmam_amortization_start_dt, maxComponent.cm_effective_dt),
cmam_amortization_end_dt) as decimal(9,2))/12 as decimal(9,2))
END
) AS AmortTermYears,
tq_dd_product as Product,
dh_key_rate as KeyRate,
dh_pv01 as PV01,
dh_val_time_stamp as RateTimeStamp,
re_bnk_le.re_company_name as Company,
rc_contact_id as UserId,
stp_name as NickName,
PR.pj_project_name as project,
'' as Borrower,
'' as Lender,
'' as AdditionalInfo,
CASE WHEN tpm_pd_permission_id = 85 THEN 'LLH' WHEN tpm_pd_permission_id = 86 THEN 'ALM' ELSE '' END as Permission,
tr_transaction_id as TransactionId,
NULL as IndicationId
FROM cfo_transaction TR
INNER JOIN com_project_transaction_link TL
ON TR.tr_transaction_id = TL.pt_tr_transaction_id
INNER JOIN com_project PR
ON TL.pt_pj_project_id = PR.pj_project_id
The query above assumes that every transaction and project is on the table that joins your tables of projects and transactions (thus the INNER JOIN), but yo can change those to LEFT JOIN if you want
You just add a second join to the other table to the query.
select
yourfields,
p.pj_project_name as project
FROM cfo_transaction t
join com_project_transaction_link tl
on t.tr_transaction_id = tl.pt_tr_transaction_id
join com_project p
on tl.pt_pj_project_id = p.pj_project_id
SELECT ..., cp.pj_project_name
FROM cfo_transaction ct
INNER JOIN com_project_transaction_link cptl
ON ct.tr_transaction_id = cptl.pt_tr_transaction_id
INNER JOIN com_project cp
ON cptl.pt_pj_project_id = cp.pj_project_id