cancel
Showing results for 
Search instead for 
Did you mean: 

Creating Records in MDM by Java API.

Former Member
0 Kudos

Hi,

I am trying to create Records in MDM using Java API.

There are two methods setFieldValue and setAttribute.

To use setFieldValue(), I need to know the FildId fotr the field.

Now, what is FieldId and how could I retrive it?

Is it justiciable to use FieldId fdid = repositorySchema.getFieldId(String, String);?

What to give as the two String parameters?

Regards

Kaushik Banerjee

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Kaushik,

After setting the fields value, you need to execute the CreateRecordCommand. Check the below link

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/70a7afe4-9e3e-2b10-de8d-b105d0b8...

Regards,

Jitesh Talreja

Answers (3)

Answers (3)

Former Member
0 Kudos

MDM Java API Record Creation Successfully done

nitin_mahajan2
Contributor
0 Kudos

Good Job, now give 100 points to Jitesh:-)

~Nitin

Former Member
0 Kudos

Hi,

I am reopening same question.

Please look at the code below.

Record rd = RecordFactory.createEmptyRecord(tableId);

FieldId fdid1 = repositorySchema.getFieldId("MDM_BUSINESS_PARTNERS","MDM_MDM_PARTNER_ID");

FieldId fdid2 = repositorySchema.getFieldId("MDM_BUSINESS_PARTNERS","MDM_PARTNER_NUMBER");

rd.setFieldValue(fdid1,new IntegerValue(86783));

rd.setFieldValue(fdid2,new IntegerValue(86783));

rd.setFieldValue(fdid2,new IntegerValue(86783))};

CreateRecordCommand cd = new CreateRecordCommand(connections);

cd.execute();

Just look at the last two lines.

Are they positioned at right locations.

Also, is it advisable to use rd.setFieldValue() or to use MdmValues[]{} ?

Even if the code is ruuning but there is no record creation in MDM.

Regards

Kaushik Banerjee

+91-9836855466

Former Member
0 Kudos

Hi Kaushik,

You have placed the code at right location, but it seems to be incomplete. You need to add the below lines after creating the CreateRecordsCommand object and before execution.

cd.setRecord(rd); // attach the new record that you created

cd.setSession(USER_SESSION_ID); // Set the user session ID

Let me know if this esolves the issue.

Cheers,

Arafat

Former Member
0 Kudos

Hi,

I need to read the Java docs properly.

Regards

Kaushik Banerjee

Former Member
0 Kudos

Thats a good idea and it would help you a lot.

Regards,

Arafat

Former Member
0 Kudos

Hi Kaushik,

You can use the below constructor as well

public CreateRecordCommand(UserSessionContext ctx)

Just pass the user session object and then execute the command.

Always put the execute command in Try-Catch block so that you can check the exact error if any exception has occured.

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi,

There is no exception.

But the below code is not giving the desired result.

No record being created in kaushikb repository.

Record rd = RecordFactory.createEmptyRecord(tableId);
		
		
		FieldId fdid1 = repositorySchema.getFieldId("MDM_BUSINESS_PARTNERS","MDM_MDM_PARTNER_ID");
		FieldId	fdid2 = repositorySchema.getFieldId("MDM_BUSINESS_PARTNERS","MDM_PARTNER_NUMBER");
	//MdmValue mv = MdmValueFactory.createIntegerValue("200000");
	//Record.setFieldValue(FieldId, MdmValue); 
	rd.setFieldValue(fdid1,new IntegerValue(86783));
	rd.setFieldValue(fdid2,new IntegerValue(86783));
	//MdmValue[]={ rd.setFieldValue(fdid1,new IntegerValue(86783)), rd.setFieldValue(fdid2,new IntegerValue(86783))};
	UserSessionContext ctx= new UserSessionContext(serverName,reposId,userName);
	CreateRecordCommand cd = new CreateRecordCommand(ctx);
	cd.setRecord(rd); 
	cd.setSession(sessionId);
	try{
		cd.execute();
	}
	catch(Exception e)
	{
		e.printStackTrace();
		wdComponentAPI.getMessageManager().reportWarning("Failure");
	}

Regards

Kaushik Banerjee

nitin_mahajan2
Contributor
0 Kudos

Kaushik, follow a logical approach to it, which is pretty simple.

1. Create an instance of record.

2. Get all the field Ids and field types(you should know the field types)

3. Set the field values as respective MDM Values. you will have to convert all the values to MDM Values to be able to set to the fields. I see this is where you might be doing wrong.

4. Set all the fields to Record using the respective Field Ids.

5. Create an instance of CreateRecordCommand using the connection pool / connection accessor command.

6. Create and Authenticate Your user session and set it to the command. If it is already available, use it and set it to the command.

7. set the record to the command.

8. Execute the command and get the record Id from the command.

If you want the complete code, i can send you that but you should try it yourself.

Regards,

Nitin

Former Member
0 Kudos

Hi,

Please tell me the compatible type for Auto Id in Java.

Regards

Kaushik Banerjee

Former Member
0 Kudos

Hi Kaushik,

What exactly you are trying to do. Yoy cannot insert or update value of Auto Id field.

Are you able to insert records now?

Regards,

JItesh Talreja

Former Member
0 Kudos

Hi Jitesh,

Yes you are right,.

Now, the code have changed.

Record rd = RecordFactory.createEmptyRecord(tableId);
		
		
		//FieldId fdid1 = repositorySchema.getFieldId("MDM_BUSINESS_PARTNERS","MDM_MDM_PARTNER_ID");
		FieldId	fdid2 = repositorySchema.getFieldId("MDM_BUSINESS_PARTNERS","MDM_PARTNER_NUMBER");
	//MdmValue mv = MdmValueFactory.createIntegerValue("200000");
	//Record.setFieldValue(FieldId, MdmValue); 
	//rd.setFieldValue(fdid1,new IntegerValue(86783));
	rd.setFieldValue(fdid2,new StringValue("86783"));
	//MdmValue[]={ rd.setFieldValue(fdid1,new IntegerValue(86783)), rd.setFieldValue(fdid2,new IntegerValue(86783))};
	UserSessionContext ctx= new UserSessionContext(serverName,reposId,userName);
	CreateRecordCommand cd = new CreateRecordCommand(ctx);
	cd.setRecord(rd); 
	cd.setSession(sessionId);
	try{
		cd.execute();
	}
	catch(Exception e)
	{
		e.printStackTrace();
		wdComponentAPI.getMessageManager().reportWarning("Failure");
	}

Regards

Kaushik Banerjee

Former Member
0 Kudos

Hi Kaushik,

Your program is working now ? If not then you are missing the Authentication part in whhich it is required to paas the login credentials of the user accessing the repository.

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi,

Authentication is done alreafy.

This type of problem happens when you leave the code for several months andf again

start coding.

I am pasting the full code.

I have to check what is going wrong.

It is not creating any record but I am able to login to my repository using below code.


public void getRepositoryConnection( )
  {
    //@@begin getRepositoryConnection()
   
		//create connection pool to a MDM server
         String serverName = "kolapon.HCLT.CORP.HCL.IN";
         ConnectionPool connections = null;
         try{
         	connections=ConnectionPoolFactory.getInstance(serverName);
         }
         catch(ConnectionException e)
         { e.printStackTrace();
         	return;
         }
		//specify the repository to use
		//			   alternatively, a repository identifier can be obtain from the GetMountedRepositoryListCommand
				String repositoryName = "KaushikRepo";
         String dbmsName ="MDM";
		RepositoryIdentifier reposId = new RepositoryIdentifier(repositoryName, dbmsName, DBMSType.ORACLE);
//		get list of available regions for the repository
		GetRepositoryRegionListCommand regionListCommand = new GetRepositoryRegionListCommand(connections);
		regionListCommand.setRepositoryIdentifier(reposId);
		try{
			regionListCommand.execute();
		}
		catch(CommandException e)
		{
			e.printStackTrace();
			return;
		}
     RegionProperties[] regions = regionListCommand.getRegions();
//	 create a user session
	 CreateUserSessionCommand sessionCommand = new CreateUserSessionCommand(connections);
	 sessionCommand.setRepositoryIdentifier(reposId);
	 sessionCommand.setDataRegion(regions[0]); // use the first region
	
	try {
						 sessionCommand.execute();
					 } catch (CommandException e) {
						 e.printStackTrace();
						 return;
					 }
     String sessionId = sessionCommand.getUserSession();
//	 authenticate the user session
	 String userName ="kaushikb";
	 String userPassword ="taton";
	 AuthenticateUserSessionCommand authCommand =
						  new AuthenticateUserSessionCommand(connections);
	 if(authCommand!=null)
	 {
	 	wdComponentAPI.getMessageManager().reportSuccess("Success");					  
	 }
	 else{
	 	wdComponentAPI.getMessageManager().reportWarning("Failure");
	 }
	 authCommand.setSession(sessionId);
	 authCommand.setUserName(userName);
	 authCommand.setUserPassword(userPassword);
	 try {
						  authCommand.execute();
					  } catch (CommandException e) {
						  e.printStackTrace();
						  return;
					  }
 
					  // the main table, hard-coded
	  try{
		GetRepositorySchemaCommand getRepositorySchemaCommand = new GetRepositorySchemaCommand(connections);
		getRepositorySchemaCommand.setSession(sessionId);
		getRepositorySchemaCommand.execute();
		RepositorySchema repositorySchema;
		repositorySchema = getRepositorySchemaCommand.getRepositorySchema();
		TableId tableId= repositorySchema.getTable("MDM_BUSINESS_PARTNERS").getId();
	if(tableId!=null)
		{
			wdComponentAPI.getMessageManager().reportSuccess("It is success");
		}
		else{
		wdComponentAPI.getMessageManager().reportWarning("Failure");	
		}
		Record rd = RecordFactory.createEmptyRecord(tableId);
		
		
		//FieldId fdid1 = repositorySchema.getFieldId("MDM_BUSINESS_PARTNERS","MDM_MDM_PARTNER_ID");
		FieldId	fdid2 = repositorySchema.getFieldId("MDM_BUSINESS_PARTNERS","MDM_PARTNER_NUMBER");
	//MdmValue mv = MdmValueFactory.createIntegerValue("200000");
	//Record.setFieldValue(FieldId, MdmValue); 
	//rd.setFieldValue(fdid1,new IntegerValue(86783));
	rd.setFieldValue(fdid2,new StringValue("Jinga La "));
	//MdmValue[]={ rd.setFieldValue(fdid1,new IntegerValue(86783)), rd.setFieldValue(fdid2,new IntegerValue(86783))};
	UserSessionContext ctx= new UserSessionContext(serverName,reposId,userName);
	CreateRecordCommand cd = new CreateRecordCommand(ctx);
	cd.setRecord(rd); 
	cd.setSession(sessionId);
	try{
		cd.execute();
	}
	catch(Exception e)
	{
		e.printStackTrace();
		wdComponentAPI.getMessageManager().reportWarning("Failure");
	}
	

	
}
catch(Exception ce)
{				  
ce.printStackTrace();	
}


//  catch(SessionException se)
//{
//se.printStackTrace();
//}	
					  						  
//wdComponentAPI.getMessageManager().reportSuccess("Hi I entered here");
    //@@end
  }

  /*
   * The following code section can be used for any Java code that is 
   * not to be visible to other controllers/views or that contains constructs
   * currently not supported directly by Web Dynpro (such as inner classes or
   * member variables etc.). </p>
   *
   * Note: The content of this section is in no way managed/controlled
   * by the Web Dynpro Designtime or the Web Dynpro Runtime. 
   */
  //@@begin others
  //@@end
}

Regards

Kaushik Banerjee

Edited by: Kaushik Banerjee on Jun 4, 2009 3:33 PM

Former Member
0 Kudos

Not possible to edit it using edit....

So, the post is jumbled.

Please paste it in notepad before looking on it.

Regards

Kaushik Banerjee

Former Member
0 Kudos

Hi Kaushik,

Just modify the lines as shown

UserSessionContext ctx= new UserSessionContext(serverName,reposId,userName);

CreateRecordCommand cd = new CreateRecordCommand(ctx);

CreateRecordCommand cd = new CreateRecordCommand(sessionId);

cd.setRecord(rd);

cd.setSession(sessionId);

Comment the lines in Italics and add the line in Bold and try to execute again it should work

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi,

The variable sessionId is of the type String.

There is no method as such

CreateRecordCommand cd = new CreateRecordCommand(sessionId);

Constructor Summary

CreateRecordCommand(ConnectionAccessor connection)

Constructor

CreateRecordCommand(UserSessionContext ctx)

Creates a new instance of this command by context.

Regards

Kaushik Banerjee

Edited by: Kaushik Banerjee on Jun 4, 2009 4:14 PM

Former Member
0 Kudos

Hi Kaushik,

CreateRecordCommand crRec = new CreateRecordCommand(connections);

crRec.setSession(sessionId);

crRec.setRecord(rd);

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi Jitesh,

I commented the authentication as it is running twice.

Record rd = RecordFactory.createEmptyRecord(tableId);
		
		
		//FieldId fdid1 = repositorySchema.getFieldId("MDM_BUSINESS_PARTNERS","MDM_MDM_PARTNER_ID");
		FieldId	fdid2 = repositorySchema.getFieldId("MDM_BUSINESS_PARTNERS","MDM_PARTNER_NUMBER");
	//MdmValue mv = MdmValueFactory.createIntegerValue("200000");
	//Record.setFieldValue(FieldId, MdmValue); 
	//rd.setFieldValue(fdid1,new IntegerValue(86783));
	rd.setFieldValue(fdid2,new StringValue("86783"));
	//MdmValue[]={ rd.setFieldValue(fdid1,new IntegerValue(86783)), rd.setFieldValue(fdid2,new IntegerValue(86783))};
	//UserSessionContext ctx= new UserSessionContext(serverName,reposId,userName);
	//CreateRecordCommand cd = new CreateRecordCommand(ctx);
	CreateRecordCommand cd = new CreateRecordCommand(connections);
	cd.setSession(sessionId);
	rd.getFieldValue(fdid2);
	try{
		cd.setRecord(rd);
	}
	catch(Exception e)
	{
		e.printStackTrace(); 
	}
	Record rd2=cd.getRecord();
	if(rd2!=null)
	{
		wdComponentAPI.getMessageManager().reportWarning("No Record created");
	}
	else{
		wdComponentAPI.getMessageManager().reportSuccess("Record created");
	}
//	authCommand.setSession(sessionId);
//		authCommand.setUserName(userName);
//		authCommand.setUserPassword(userPassword);
//		try {
//							 authCommand.execute();
//						 } catch (CommandException e) {
//							 e.printStackTrace();
//							 return;
//						 }
	try{
		cd.execute();
	}
	catch(Exception e)
	{
		e.printStackTrace();
		wdComponentAPI.getMessageManager().reportWarning("Failure");
	}

The wdComponentAPI.getMessageManager.reportSucess("");

is not working.

I have to check why the record is not getting created.

Regards

Kaushik Banerjee

Former Member
0 Kudos

Hi Kaushik,

Remove the lines starting from rd.getFieldValue(fdid2); till the end

just write

cd.setRecord(rd);

try

{

cd.execute();

}

catch(Exception e)

{

e.printStackTrace();

}

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi ,

Problem is here.

RepositorySchema repositorySchema;
		repositorySchema = getRepositorySchemaCommand.getRepositorySchema();
		TableId tableId= repositorySchema.getTable("MDM_BUSINESS_PARTNERS").getId();

Regards

Kaushik Banerjee

Former Member
0 Kudos

Hi Jitesh,

I have found out the root cause of the problem.

wdComponentAPI.getMessageManager.reportSuccess("String");

The output is:

Before function call

Success

Success

Field Id created

CreateRecord Object created

Record created

After the call

The Record is getting created.

Problem is resolved.

Regards

Kaushik Banerjee

Edited by: Kaushik Banerjee on Jun 5, 2009 7:59 AM

Greg_Austin
Active Participant
0 Kudos

Hi Kaushik,

You are correct using the getFieldId method from a RepositorySchema object. The two Strings in the method are table code and field code. The table and field codes can be seen in the MDM Console, note it is the field code, not the field name.

-Greg

Former Member
0 Kudos

Hi Greg,

you are correct.

FieldId fdid = repositorySchema.getFieldId("MDM_BUSINESS_PARTNERS","MDM_PARTNER_NUMBER");
	MdmValue mv = MdmValueFactory.createIntegerValue("200000");
	//Record.setFieldValue(FieldId, MdmValue); 
	rd.setFieldValue(fdid,mv);

Code above is conceptually correct but it is not creating any MDM_PARTNER_NUMBER by 20000

or am I doing any silly mistake?

Regards

Kaushik Banerjee

Edited by: Kaushik Banerjee on Jun 2, 2009 3:37 PM

Greg_Austin
Active Participant
0 Kudos

Looks like the code you have has created a Record in code, but not in MDM. Check out the javadocs for CreateRecordCommand. Javadocs can be found here Link: [http://help.sap.com/javadocs/MDM/index.html]