function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Emmanuel CoronaEmmanuel Corona 

compare two related list from one object

greetings!

 

Is possible to compare to related list from the opp fi they are made with lookup fields? and if the result is false then reject the save or send a message to the user that the values are not the same?

 

Thank you"!!

Avidev9Avidev9
Compare with related list ? What do you mean by that ?
Something like if the value exists in the lookup then reject ?
Emmanuel CoronaEmmanuel Corona
Hello Avidev!

Let me put this way...

In the opp i have a related list for a custom object called SLA and other to the custom object Buildings... so when the user click on NEW SLA the building related list copy to that new sla or let the user add buildings and then compare that buildings list from SLA with the opp building list, if they are not the same, then reject the creation of the new SLA.

Is that possible?
Avidev9Avidev9
Sorry not very clear.
What I understand that.
There are two child objects SLA and Building. After the new thing am completely lost. :(
Emmanuel CoronaEmmanuel Corona
Lol! ok let me be more clear...

In the opp page i have to related list one for SLA and other for buildings.
When the user click on new sla i need to copy the related list (building) to that new request... or if that is not possible, then when the user click on new sla request, add buildings using another related list and then compare the new sla request building related list with the building related list in the opp and if are not the same then reject the creation or do something that don't let add that new sla request to the opp
Val ValinoVal Valino

Create a trigger for when you create a new SLA and then in the trigger, query all the Buildings for the matching the selecting buildings to the list of buildings then reject with an error if it doesn't match.

Emmanuel CoronaEmmanuel Corona
hello Val! that sounds good... but do you have an example??? i'm new in the salesforce management
Avidev9Avidev9
The problem is I am unable to understand relation between SLA and Building. How can I mve all the Buildings to SLA?
Is there any relation ship between SLA and Building ?

What I feel like it can be done using VF page and Controller or even using a Trigger
Val ValinoVal Valino

Okay, create a trigger for SLA. When a new SLA is created, automatically create a relationship between SLA and all Buildings that is related to the Opps.

 

refer to this as guide.

 

http://www.salesforce.com/us/developer/docs/apexcode/index.htm

Emmanuel CoronaEmmanuel Corona
Hi Avidev!

On the SLA Request page i have another related list to add buildings... in my case the SLA apply depending on the building classification
Val ValinoVal Valino

I think there's an Opportunity record with SLAs related to it and Buildings related to it.

 

They want it so that when you create a new SLA, all Buildings related to the same Opportunity will be related to the SLA.

Avidev9Avidev9

As Val Valino Said

You need to create a trigger on SLA and whenever a new record is inserted.

Query all the Building related to the Currentt Opportunity and Clone/Update them with the New SLA Id.

 

It seems like it will be a After trigger

Emmanuel CoronaEmmanuel Corona
That's correct Val Valino!!! but the relation is not master detail, are just with lookup fields
Emmanuel CoronaEmmanuel Corona
Thank you Avidev!... do you have some references or examples i can look to do it???
Val ValinoVal Valino

Here is something to get you started.

 

trigger myTrigger on SLA(after insert){
	if(trigger.isInsert){
	
		List<SLA> slaList = new List<SLA>();
		Set<ID> oppID = new Set<ID>();
		
	
		for(SLA s: trigger.New){
			If(oppID.contains(s.Opp) == false){
				oppID.add(s.Opp);
			}
			slaList.add(s);
		}
		
		Map<string, List<Building>> oppBuildMap = new Map<string, List<Building>>();

		for(Building b:[select fields here from Building where Opp IN : oppID]){
			if(oppBuildMap.get(b.oppID) == false){
				List<Building> bList = new List<Building>();
				bList.add(b);
				oppBuildMap.put(b.oppID, b);
			}else{
				oppBuildMap.get(b.oppId).add(b);
			}
		}
		
		List<SLA> updateSLAList = new List<SLA>();
		
		for(SLA s: slaList){
			for(integer = 0; x < oppBuildMap.get(s.Opp).size(); x++){
				here add the logic to create new relationships between SLA and Building (I am thinking you are using a junction object?)
				
				updateSLAList.add(s);
			}
		}
		
		update updateSLAList;
	
	}
}

 

Emmanuel CoronaEmmanuel Corona
That correct Val i'm using ajunction object, so that my change the code???
Emmanuel CoronaEmmanuel Corona
Hello Val!

I tried to change the code as:

trigger myTrigger on SLAyEdif__c(after insert){
if(trigger.isInsert){

List<SLAyEdif__c> slaList = new List<SLAyEdif__c>();
Set<ID> oppID = new Set<ID>();


for(SLAyEdif__c s: trigger.New){
If(oppID.contains(s.Opp) == false){
oppID.add(s.Opp);
}
slaList.add(s);
}

Map<string, List<edificio__c>> oppBuildMap = new Map<string, List<edificio__c>>();

for(edificio__c b:[select name, Clasificacion_comercial__c from edificio__c where Opp IN : oppID]){
if(oppBuildMap.get(b.oppID) == false){
List<edificio__c> bList = new List<edificio__c>();
bList.add(b);
oppBuildMap.put(b.oppID, b);
}else{
oppBuildMap.get(b.oppId).add(b);
}
}

List<SLAyEdif__c> updateSLAList = new List<SLAyEdif__c>();

for(SLAyEdif__c s: slaList){
integer x;
for(slaList = 0; x < oppBuildMap.get(s.Opp).size(); x++){
// here add the logic to create new relationships between SLA and Building (I am thinking you are using a junction object?)

updateSLAList.add(s);
}
}

update updateSLAList;

}
}

But i get Error: Error de compilación: Invalid field Opp for SObject SLAyEdif__c en la línea 10 columna 27
Avidev9Avidev9
Probably you have to replace "s.Opp" with "s.Opportunity__c" i.e the api name of the field that points to Opportunity