Use new inserted id for another field in sugarcrm - logic

I want to use new inserted id for another field after insert.
I want to use it for my document ID, example when new record inserted, the auto increase id will have a prefix and follow with the auto increase id, it will stored in the account custom table (account_cstm) with a new field call "document_id_c".
Example the auto increase id is 18, the data will be stored in document_id_c is "DOC18".
I tried smtg in logic hook:
$bean->account_number_c = 'DOC'.$bean->account_number;
$bean->save();
But it seems like not working.
Another problem is, i need to show immediately after record has been saved.
Need some advice.
Thanks!!

First use $bean->id = create_guid(); after that create custom id accordingly and assigned in your field and call $bean->save();

Related

How to undo database after update in laravel?

I have two buttons, updates and undo. For example, after I update the new database, I want to undo it back to the original. I can only execute the update button.
function update(Request $req)
{
$id = $req->contact_id;
$contract = Contacts::find($id);
$contract->area_id = $req->areas;
$contract->code = $req->code;
$contract->created_at = $req->sign_in;
$contract->value = $req->total;
$contract->save();
return redirect('contact/'.$id)->with('message', 'Update successful !!');
}
To do that you must keep somewhere your old record before you update.
There is no "undo" so don't think it like that. You basically want to update the table with the old values.
Since as you said you have 2 buttons which one of them is the update which updates the current record what you can do, to make undo possible is that you have another table like records which you are going to fill in every update.
So according to your code the field contact_id seems like the unique identifier for each contract. Before you update then with the new values you can use a select like:
$oldRecord = DB::table('my_main_table')
->where('id', $req->contact_id)->first();
Inside your $oldRecord variable is stored all the info of your record that you want to update but is not yet updated , so it's your old record. Having that you can just hit an insert query to the records table i mentioned before so you can keep your "older version" there in case you want to undo.
So the undo functionality will just be the same logic. Select the record with the specific contract_id but this time from the records table and just update your main table.
I don't know if I fully got your question, but as long as you haven't called the save() method on your model, you can use
$eloquentModel->getChanges()
to list all attributes you did already change.
You can retrieve original attribute values by calling
$eloquentModel->getOriginal();

Generating unique IDs for new records and existing records

I'm basically trying to create a primary ID between CRM and QuickBooks. Figured I'd just use the existing PK in CRM for the lookup. I'd like the PK to visible to the user, but not editable in CRM.
This has presented several problems in that you can't do that out of the box. I thought I read somewhere you could either via business rule or calculated field, but I haven't had luck with that.
It sounds like it would require web resources if I were to go this route.
The other option would be to just generate unique values for every record in Accounts and Contacts.
Does this automatically populate existing records or just new records? How do I get it to populate existing records?
You can use Auto number manager for configuring an auto-number attribute in every entity. This seeds a number based on configured format for new records. Uniqueness assured by SQL sequence feature & no need of any extra plugin/workflow.
For existing records - you can design a workflow along with a temp entity to assign auto-number. Read more.
Otherwise you can use SSIS + Kingswaysoft package to generate auto-number & assign for existing records.
I am suggesting you to create a new text field on the entity and create a pre plugin that will get the record primary GUID id from context and will set this GUID into the newly added attribute. You can set this field as read-only of form as well.
OR you can generate new GUID as well into the plugin.

How to save the new record using existing record?

For Instance I have an Model called Demo Model with 100 fields, I have queried and got one particular record and stored in variable called demo_values and want to create a new record using demo_values dynamically.
How can we create it?
demo_object = DemoModel.objects.first()
demo_object.pk = None
demo_object.save()
This should create a copy of the model object with exact same data.

Servicenow - Service Catalog Reference Field - Insert different column than the display value

Let me describe my problem:
I have a table for all my IT-Services. I reference to this table more than once, for different purposes. Most of the time I need to reference to the name of the service. That's why I keep the name as displayed value.
One Column of that table is a service_id (custom field) which is for example "Service_004". Now in a catalog request Item the User has to fill in the service_id in a reference field.
But since I have the name as displayed value, and I need it in other forms, I am unable to reference to the service_id.
Using the variable attributes field I managed to let the service be found using the autocomplete function. But in the reference field I still get the servicename. I know that I can change the display value in the dictionary, but this breaks other functions. So what I need is to change the display value just for one reference field.
Also I tried to create a new table called IT-Services2 with reference to my table IT-Services. Then I switched the display to true in the new table for my service_id, but this will even change it in the parent table.
Perhaps an onChange client script utilizing g_form.setLabelOf() ?
http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#setLabelOf
Maybe I'm not fully understanding your question...
I ran into this issue before, what you can do is create select box variable and use an on load client script to populate the list with the service_id(s) from the table you are referencing.
I would write a script include to pull the data from the table and call it from the client script via GlideAjax.

KendoUI grid create : how to deal when server response only the id of the new created item

I want to use the KendoUI grid widget with a rest API (manage by dreamfactory); when i create a new item with the grid the server response contains only the new id and NOT the fully new item (aka all the fields) as I read in this forum http://www.telerik.com/forums/request-for-support-on-editable-grid#2098471.
"When you add a new item in the Grid its ID should be generated on the server and the newly inserted item (as an array) back to the client. This way the DataSource can update its internal data and the Grid widget will update the column for this field. In case the server does not return the result, the inserted item will be treated as a new one every time you sync the dataSource. The same applies for destroy and update functionality."
My question is how to deal with if we cannot modify the contains of the server response.
here is the response i get from the server : {"record":[{"id":26}]}
Any idea?
According to the DreamFactory documentation, only the id of the created record is returned by default. The docs also mention that you may select the returned fields. Using fields=* will return all created fields. You should be sure to use the record wrapper to force the return of an array rather than a single, unwrapped id field.

Resources