how create new Employee in hybris programmetcally - sap-commerce-cloud

I am unable to create new user with below code :
final EmployeeModel empl=new EmployeeModel();
empl.setUid("test");
modelService.save(empl);

Also, don't do this: new EmployeeModel();
You should always use the ModelService to create new instance of ItemModel:
modelService.create(EmployeeModel.class)
Using modelService, it will go through any Interceptor that's define for this type and then, could initialize default value for your item. When your using the new operator. It won't set the default value, and can result in the kind of error you are facing.

This code works in an hybris out of the box.
You have probably customized the employee type and added mandatory fields.
Check the error detail, you will probably find out the field you missed.

Related

How to get localized name of an attribute of an item type in Hybris?

I need localized name of the field of a new itemtype I created, can someone please point out if there are any existing service/utility available for achieving the same.
Thanks in advance.
You can use below util class:
de.hybris.platform.util.localization.Localization.getLocalizedString("type.MyItemType.myProperty.name");
Above function using jalo session locale. If you want to get for any translation, you need to create your own function. You can check above function implementation and create your own function.

Is a newly added field available for BQL to query in database?

Hopefully it is not a stupid question - can I use BQL to query a new field that I just added through customization in database?
I just added a new field to "contract" table through "System->Customization" - I created a project there and added a new field called "ProductCode" (it automatically became "UsrProductCode" in database), and the field does show in "contract" table as well as the "contract template" screen (CT202000) after successful compilation and publish, exactly as I expected, however, I got error:
The type name 'UsrProductCode' does not exist in the type 'PX.Objects.CT.Contract' in file: Code#SOOrderEntry(80)
when I then tried to use this field in a BQL as below:
// Lookup contract template ID
Contract template = PXSelect<Contract,
Where<Contract.isTemplate, Equal<boolTrue>, And<Contract.UsrProductCode, Equal<Required<Contract.UsrProductCode>>>>>
.Select(Base, inventoryCD);
I thought adding new field through customization would automatically make it available for BQL query but it seems I was wrong - what would I need to do in order to make it be able to be used in BQL?
Thanks for your help.
Edited:
Following suggestion from #Jeff Williams, I tried to find out the class definition file related to my customization - the only file I found is "PX_Objects_CT_Contract_extensions.cs", which is under "C:\Program Files (x86)\Acumatica ERP\AcumaticaERP\App_Code\Caches" and the code is very simple as below:
public class PX_Objects_CT_Contract_Extension_AddColumn: PXCacheExtension<PX.Objects.CT.Contract>{
#region UsrProductCode
[PXDBString(30)]
[PXUIField(DisplayName="Product Code")]
public virtual string UsrProductCode{get;set;}
public abstract class usrProductCode : IBqlField{}
#endregion
I also tried to use "PX_Objects_CT_Contract_Extension_AddColumn.UsrProductCode" in BQL and got error during compilation:
'PX_Objects_CT_Contract_Extension_AddColumn.UsrProductCode' is a 'property' but is used like a 'type' in file:
Can somebody tell me what else I need to do to make this new field available for BQL?
It does add it however you cannot reference it as "Contract.Usr..." it would be under something like "ContractExtension.Usr...".
Look at the data class where the DAC extension is and see what that name is. If your BQL is in a different name space you will need to add a reference to the codeclass namespace as well.

Orchard Content Parts that Don't Store Information in the Database

I'm trying to create a content part in Orchard that will perform a request to a web server and display the results of the request. The problem I'm running into is that my part requires no user input and thus I have left the part and part record classes empty. I have a Migrations.cs file that adds a description to the part and makes it attachable as well as creates a content item with the part attached to it. When I go to create a new instance of my content type it tries writing to the database and fails. How do you create a content part in orchard that doesn't try to save to the database? Thank you.
The actual error I receive is:
null id in Orchard.ContentManagement.Records.ContentTypeRecord
I'm pretty sure you don't need to create new table since there are many parts which don't have any in Orchard. Try to remove MyCustomPartRecord.cs and change MyCustomPart.cs
public class MyCustomPart : ContentPart<MyCustomPartRecord>
to
public class MyCustomPart : ContentPart
Then just add driver and view and you should be good without extra tables ... In theory :D
The answer to my problem is even when your part ins't actually saving anything in the database you still need to include a table for the part so it can store the id. I just added the following to my Migrations.cs file and it fixed the problem.
SchemaBuilder.CreateTable("MyCustomPartRecord",
table => table
.ContentPartRecord()
);

New ContentItem from code, when is it saved?

I can't get my head around this.
If I have a ContentType called Contacts. The ContentType has two fields attached to it.
FirstName (textfield) and LastName (textfield).
If I want to create a new contentitem of this type then I can write code like this.
dynamic contact = _services.ContentManager.New("Contacts");
contact.Contacts.FirstName.Value = "John";
contact.Contacts.LastName.Value = "Doe";
_services.ContentManager.Create(contact, VersionOptions.Published);
This does not work. The Contentitem gets created but the fields are empty.
However, if I write it like this it works. Why is that? Must I set the fields values after ContentManager.Create is called?
dynamic contact = _services.ContentManager.New("Contacts");
_services.ContentManager.Create(contact, VersionOptions.Published);
contact.Contacts.FirstName.Value = "John";
contact.Contacts.LastName.Value = "Doe";
What you observed is indeed the intended behaviour and is by design. I've come across this before too and also created an issue about it. As you can see there it was closed by Sebastien, the lead developer stating that this is by design but unfortunately not explaining why.
FYI the standard workflow for managing content items is the following:
Instantiate item.
Create it.
Update its values.
If the update happened through the model binder then check if the ModelState is valid. If not, cancel the transaction and return.
If everything's OK and the content type is set to be draftable, publish the item.
You can see an explained example of this in the Orchard Training Demo module (ContentsAdminController.PersonListDashboardPost()).

How to create a lookup column that targets a Doc Lib and uses the 'Name' of the document?

How do you create a lookup column to a Document Library that uses the 'Name' of the document as the lookup value?
I found a blog post that recommends adding another custom field like "FileName" and then using a item reciever to populate the custom field with the value from the Name field but that seems cheesy.
Link to the blog in case people are interested:
http://blogs.msdn.com/pranab/archive/2008/01/08/sharepoint-2007-moss-wss-issue-with-lookup-column-to-doc-lib-name-field.aspx
I've got a bunch of custom document content types that I dont want to clutter with a work around that should really work anyway.
I created a one step workflow to set the title from the name, fired on modify and created. Seems to work and took seconds to create.
One way you can do this (although not the easiest way) is by creating a custom field type that extends the SPFieldLookup class. SharePoint's field editor for Lookup fields purposefully hides any columns types that aren't supported by Lookup fields, but you can create a field editor for your custom field type that shows them.
However, I have created a Lookup column that points to a Name column in a Document Library before, and it probably doesn't work like you'd expect. While the value stored in the lookup column is valid, it doesn't show up right in List view or on the View Properties form.
The solution you posted may actually be the best way to handle this. Lookup fields require some kludges if you want to handle more complex scenarios, but that's because they're not meant to provide the same functionality as a foreign key relationship in a database.
Coding in any form always scares me. So Here's what I did: I simply renamed the Stupid "Title" Field to something else, say "Keywords", since you cant do anything with that field: cant even make it mandatory.
Then I created another Single line field called "Title" and used this field for the Lookups
Well there is a simple solution to that and in might work in some case.
In the nutshell if you make the Title field Mandatory, this will force the user to enter a title. In that manner we can use title field as a lookup field.
Now How to do that?
One you are done create a document library go to the library setting. Select Advance Setting and Select Yes for the option "Allow management of content types?".
Then go back to the Library setting and Under content types select the "Document" Content type. THen Select Title Column and then Select "Required (Must contain information)" and say OK.
Now try uploading a document to this document library. You will see Title field in the form.
Hope this helps
Cheers
Vaqar
You have to add the field as XML with the ShowField as 'FileLeafRef'
var XmlFieldDefinition = "<Field DisplayName='myLookupColumn' Type='LookupMulti' StaticName='myLookupColumn' Name='myLookupColumn' Required='FALSE' List='THE LOOKUP ID HERE' WebId='THE WEB ID HERE' UnlimitedLengthInDocumentLibrary='TRUE' Mult='TRUE' Sortable='FALSE' ShowField='FileLeafRef' />"
Field fld = fieldCollection.AddFieldAsXml(XmlFieldDefinition, true, AddFieldOptions.DefaultValue);
ClientContext.Load(fld);
ClientContext.ExecuteQuery();

Resources