Create new record based on Old record on app maker - google-app-maker

I have a table:
Table: Client Name
- id(number) Primary
- Client_Name(string)
- Client_Code(string)
- POC(string)
I want to create many POC with the same Client_Name and Client_Code. It should working
Its Datasource is POCQuery (create).
OnClick, I chose "create New Item", it failed to create new POC. ---> failed
OnClick: var record = app.models.Person.newRecord();
app.saveRecords([record]); ---> Failed
Actualy what I want is, everytime + button clicked. It will create new POC record bellow #POC.
Are there any suggestion to fix this problem?

Related

how to get navigated to a custom record from a button with some field sourced?(suitescript 2.0)

i m doing this in my Client script which is being called by the function of UE script button.
var createEstimatorURL = url.resolveRecord({
recordType: 'customrecord_awt_estimator_hdr',
recordId: '',
isEditMode: true,
params: {
'project': project,
'customer': customer,
'createdBy': createdBy,
'projectStatus': projectStatus,
'subsid': subsid,
'awtEstRef': awtEstRef
}
});
After getting the URL from resolve record, I m doing newWindow = window.open(createEstimatorURL);
The record is getting opened in create mode (not saved yet), but how to source the fields that I sent as params? please help
They are in the request parameters of your context on the new page that opens.
You can use this bit
context.request.parameters['*'],
where * is the name of your parameter, to get them.

How to clone a record and create a new records in app maker?

I have a table. I want to clone a record. I added a button and in the OnClick event I have tried
var rowDataSource = widget.datasource;
// Instead of using the row datasource for create, explicitly use the data source of your list.
var listDatasource = app.datasources.Change;
var createDataSource = listDatasource.modes.create;
createDataSource.item.Systems.SystemName = rowDataSource.item.Systems.SystemName;
createDataSource.item.changeType = rowDataSource.item.changeType;
widget.datasource.createItem();
widget.datasource.saveChanges();
But this gave me error
can't associate Data with Draft Records since I am using relational Database and my databases are in manual save mode.
How to clone all field from one record and create a new record??
Here Author name comes from the relational database and Timestamp is from the tables database.

Firebase database .on('value') method called even if child added in same level

I am a newbie in using Firebase database. I am trying to store user login time in Firebase database. Sample code below.
const USER_LOGIN_TIME = "UserLoginTime";
const userId = localStorage.getItem("userId")!;
const spaceId = localStorage.getItem("spaceId")!;
const loginTime = database.ref(`${spaceId}/${USER_LOGIN_TIME}/${userId}`);
loginTime.set(time);
loginTime.on('value', (snapshot: any) => {
let latestTime = snapshot.val();
// This is getting called even if some new user logs in
// some logic goes here
});
loginTime is stored properly.
I am assuming .on('value') will be called only if the value changes in ${spaceId}/${USER_LOGIN_TIME}/${userId}.
But the issue is .on('value') method is getting called whenever there is new entry in ${spaceId}/${USER_LOGIN_TIME}. New entry will be added when new user logs in a different browser.
Not sure what I am doing wrong here! Any help is appreciated.
Update
My Firebase DB schema:
- <spaceID>
- UserLoginTime
- <UserID>
- time: <timestamp>
- <UserID>
- time: <timestamp>
The issue is not related to Firebase, but it is my code. During the initial load, spaceID and userId is null for all users and time is set for null key. This gets updated whenever a new user logs in and old users in the same session will get the updates and hence logged out.

Is there a way to update a record after it's been created?

I'm trying to update the id (not pk) of a record onAfterCreate in auto save mode. The createContractNum_SOW() is a server script that returns a number. I have also tried using
app.datasources.MyDatasource.item.MyField = 'My new value';
but that does not work either.
Is there something that I am missing?
if (contract_type === "Statement of Work"){
var next_id = createContractNum_SOW();
record.contract_num = next_id;
}
You probably need to save the record after you modify it.
Since the datasource events are server side you can do that with app.saveRecords([record]);

Google App maker record key value

In App maker we enabled the manual save mode. On button click a new form will open and we will create an empty record, when user fills the fields and clicks the save button saveChanges function will save all the values.
In documentation and sample projects I can see after a record creation _key value is updated in the data source and we can use that key value to query record from its child model.
But in our case key value is not returned. But after save changes function when we open that record key value is coming, what could be the issue.
You don't have record key on the client, until you save it, since record key is generated by the server. This applies both to Auto and Manual save modes.
Here is code snippet from App Maker documentation:
var myCreateDatasource = app.datasources.MyDatasource.modes.create;
var draft = myCreateDatasource.item;
draft.Name = "Name";
draft.Age = 21;
// Create the new item
myCreateDatasource.createItem(function(newRecord) {
// Callback is called after the record is saved, so it now has a key.
var key = newRecord._key;
// Do something with the key here.
}

Resources