How to if {!id} has relationship with R108123321?

Posted by matman on 03-Sep-2014 02:40

Hey guys, yesterday I set a trigger on one of my Employee objects. I set it to 'before update' when relationship with the Employer (One to Many relationship) object changes. Since I have access to the API documentation it's a smooth ride. Unfortunately I did bump into a little problem I haven't found a fix to yet.

I'm trying to run some code to check if the Employee is the default contact person for the Employer. If this is true, the Employee cannot be detached from the relationship.

I have used rbv_api.isFieldEmpty() and rbv_api.getFieldValue() to somehow check if the Employee ID ( {!id} ) is in the relationship field. This didn't work out for me though. Does anyone know how I could check this?

Thanks a bunch!

Posted by matman on 03-Sep-2014 05:34

I found the answer to my problem, so now I can also explain why it didn't work for me. Do understand: I'm not the best programmer, my explanation is based on the little experience I have in the programming field.

I believe Rollbase uses a virtual database inside a database. This means that when you create an object, a row will be added to the object table, definining that object. When fields are added to that object, rows will be added to the fields table, defining the fields, their values and to which object they link. When a field is actually filled in by the user, the value will be stored in a value table, linking the value to that field (and the field links to the object).

Now.. Once you create a relation between two objects (Employer and Employee), a row will be added to the relations table, defining that relation.
When you define a 1-1 relation, a field will be added to the field table of ONE of the two objects. Now value Object ID will be linked to that field.
When you define a 1-M, M-1, M-M relationship, the Object ID's of these two objects will be stored and linked to that relationship.

That's the theory at least. If you actually want to check if an {!id} has a relationship with R............, you must ask yourself first which side of the relationship you are checking. I'll explain this by using my scenario:

We have an Employer. This Employer has relationships with his Employees (One Employer to Many Employees). If we want to get all Employees of an Employer, we must ask the Employer for his relationships. If we want to get the Employer of an Employee, we must ask the Employee for his relationship.

Now my second scenario: one of the Employees is a default contact person. This means that I had to add a 1-1 relationship (One Employer to One Employee). Now which side are we going to ask? In this case it will be the side of te Employer. You see: the Employer has a default contact person, the Employee does not have a default contact person.

So you now only have to make sure that when you create the relationship, you should do so from the correct side. If you create a 1-1 relationship in the Employee object setup, you will create it FROM Employee TO Employer. In my case it is created in the Employer object setup, which means it is created FROM Employer TO Employee.

Secondly, we want to check if the Employee has a relationship with the Employer. (See my first post to see why I would want that.) Because the relationship is defined FROM Employer, we need to ask it to the Employer. But first we need to get the Employer.

1. Is Employee of Employer.
2. Is default contact person of Employer.

The Employee's trigger runs because it's relationship with the Employee is planned for removal. But it's relationship can only be removed if the Employee is not the Employer's default contact person.
I will first need to ask the Employee who his Employer is. Once I know this, I will need to ask the Employer who the default contact person is. And now for the last part of my code: If the default contact person is the Employee, the relationship with the Employee cannot be deleted.

Let's translate it to psuedo code:

if(Employee has Employer) {
	if(Employer's Default Contact Person IS NOT Employee) {
		Employee relationship gets deleted
	} else {
		Employee relationship cannot be deleted
	}
}

Now let's translate it to Object Script:

var employer = rbv_api.getFieldValue("medewerker", {!id}, "R108244227")[0];
if(employer) {
	if({!id} != rbv_api.getFieldValue("klant", employer, "R108244245")[0]) {
		return true;
	} else {
		return false;
	}
}

As you perhaps noticed, I use an index [0] after getFieldValue(). getFieldValue() returns a reference to an array. In this array all the values are stored that were retrieved from that field. Since both times we will only get one index, I will always use index 0.

I hoped I could help some people with this!

All Replies

Posted by Rob Debbage on 03-Sep-2014 05:30

Hi there,

As a preliminary suggestion, you might want to check out these articles in the Progress Knowledge Base. They don't apply directly to your use-case but the code / constructs referenced in them might be something that you could try / use in your scenario:

How to setup validation rules for hierarchy relationships
http://knowledgebase.progress.com/articles/Article/000046556

How to Identify changes in a Many-To-Many relationship?
http://knowledgebase.progress.com/articles/Article/000052307

Have a look and let us know if they help a bit. If not, please could you also mention whether you are on private or public cloud? If private, which version of Rollbase are you currently using?

Thanks and kind regards,

Rob

Posted by matman on 03-Sep-2014 05:34

I found the answer to my problem, so now I can also explain why it didn't work for me. Do understand: I'm not the best programmer, my explanation is based on the little experience I have in the programming field.

I believe Rollbase uses a virtual database inside a database. This means that when you create an object, a row will be added to the object table, definining that object. When fields are added to that object, rows will be added to the fields table, defining the fields, their values and to which object they link. When a field is actually filled in by the user, the value will be stored in a value table, linking the value to that field (and the field links to the object).

Now.. Once you create a relation between two objects (Employer and Employee), a row will be added to the relations table, defining that relation.
When you define a 1-1 relation, a field will be added to the field table of ONE of the two objects. Now value Object ID will be linked to that field.
When you define a 1-M, M-1, M-M relationship, the Object ID's of these two objects will be stored and linked to that relationship.

That's the theory at least. If you actually want to check if an {!id} has a relationship with R............, you must ask yourself first which side of the relationship you are checking. I'll explain this by using my scenario:

We have an Employer. This Employer has relationships with his Employees (One Employer to Many Employees). If we want to get all Employees of an Employer, we must ask the Employer for his relationships. If we want to get the Employer of an Employee, we must ask the Employee for his relationship.

Now my second scenario: one of the Employees is a default contact person. This means that I had to add a 1-1 relationship (One Employer to One Employee). Now which side are we going to ask? In this case it will be the side of te Employer. You see: the Employer has a default contact person, the Employee does not have a default contact person.

So you now only have to make sure that when you create the relationship, you should do so from the correct side. If you create a 1-1 relationship in the Employee object setup, you will create it FROM Employee TO Employer. In my case it is created in the Employer object setup, which means it is created FROM Employer TO Employee.

Secondly, we want to check if the Employee has a relationship with the Employer. (See my first post to see why I would want that.) Because the relationship is defined FROM Employer, we need to ask it to the Employer. But first we need to get the Employer.

1. Is Employee of Employer.
2. Is default contact person of Employer.

The Employee's trigger runs because it's relationship with the Employee is planned for removal. But it's relationship can only be removed if the Employee is not the Employer's default contact person.
I will first need to ask the Employee who his Employer is. Once I know this, I will need to ask the Employer who the default contact person is. And now for the last part of my code: If the default contact person is the Employee, the relationship with the Employee cannot be deleted.

Let's translate it to psuedo code:

if(Employee has Employer) {
	if(Employer's Default Contact Person IS NOT Employee) {
		Employee relationship gets deleted
	} else {
		Employee relationship cannot be deleted
	}
}

Now let's translate it to Object Script:

var employer = rbv_api.getFieldValue("medewerker", {!id}, "R108244227")[0];
if(employer) {
	if({!id} != rbv_api.getFieldValue("klant", employer, "R108244245")[0]) {
		return true;
	} else {
		return false;
	}
}

As you perhaps noticed, I use an index [0] after getFieldValue(). getFieldValue() returns a reference to an array. In this array all the values are stored that were retrieved from that field. Since both times we will only get one index, I will always use index 0.

I hoped I could help some people with this!

Posted by matman on 03-Sep-2014 06:22

Thanks for the help and articles! I did find a solution to my issue. I'm running Rollbase on a public cloud, using the latest version.

This thread is closed