How to get the id after insert (Auto PK= True) | Community
Skip to main content
July 19, 2018
Solved

How to get the id after insert (Auto PK= True)

  • July 19, 2018
  • 2 replies
  • 1647 views

xtk.session.Write(<tempOptout xtkschema="sam:tempOptout" _operation="insert" rId={vrId} blId={vblId} oldEmail={vOldEmail} optOut={vOptOut}  />);

In the above I want to get back the id value for the above schema after the insert. Schema has autopk=true

Please help

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by amritak94288453

I was able to achieve it as below . Problem with inserting and then selecting is it does not guarantee the right id because of parallel transactions.   So I had to do the below

var sql4 =   "WITH upd AS (INSERT INTO SamTempOptout (sOldEmail,iBlId,iRId,iOptOut) values ('"+ vOldEmail + "'," + vblId  + "," +vrId + "," + vOptOut + ") RETURNING iTempOptoutId) SELECT iTempOptoutId FROM upd ";

document.write("sql4 = " + sql4 );

ctx.vars.TempOptoutId =  String(sqlGetInt(sql4))

2 replies

vraghav
Adobe Employee
Adobe Employee
July 20, 2018

Hi amritak94288453 ,

You will have to fire a queryDef using proper unique conditions, probably rId={vrId}, blId={vblId} and oldEmail={vOldEmail}

Regards,

Vipul

amritak94288453AuthorAccepted solution
July 21, 2018

I was able to achieve it as below . Problem with inserting and then selecting is it does not guarantee the right id because of parallel transactions.   So I had to do the below

var sql4 =   "WITH upd AS (INSERT INTO SamTempOptout (sOldEmail,iBlId,iRId,iOptOut) values ('"+ vOldEmail + "'," + vblId  + "," +vrId + "," + vOptOut + ") RETURNING iTempOptoutId) SELECT iTempOptoutId FROM upd ";

document.write("sql4 = " + sql4 );

ctx.vars.TempOptoutId =  String(sqlGetInt(sql4))