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
Rajat MajumderRajat Majumder 

Please help me write the SOQL query.

I have a situation where I have two objects Opportunity and a custom Object Contract__c. In both the object I have a field called reference_name__c. Now the situation is that I have to query the objects and return the reference_name__c values. If Opportunity has reference_name__c value and Contract__c don't have reference_name__c value return value. If Opportunity has no reference_name__c value and Contract__c has reference_name__c value then return the value. How could I write the query? The query was written by me just satisfies the 1st condition. Please help me with the query.


Select Id, reference_name__c,(Select ID, reference_name__c from Contract__r where reference_name__c !=null Limit 10) from opportunity where AccountId != null AND reference_name__c != null AND Owner.AccountId =loggedInUser.Contact.AccountId limit 10

Best Answer chosen by Rajat Majumder
CharuDuttCharuDutt
Hii Rajat
Try Below Code
list<oppotunity> lstOpp = [Select Id, Name, reference_name__c,(Select ID, Name, reference_name__c from Contract__r where reference_names__r !=null Limit 10) from opportunity where AccountId != null AND reference_name__c != null AND Owner.AccountId =loggedInUser.Contact.AccountId limit 10];

Map<string,string> mapOpportunity = new Map<string,string>();
for(opportunity oOpp : lstOpp){
	if(oOpp.reference_name__c != null){
	mapOpportunity.put('Oppotunity>> '+oOpp.Name, oOpp.reference_name__c);
	}
	
	for(reference_names__c oReff : oOpp.reference_names__r){

	mapOpportunity.put('Refference Names>> 'oReff.Name, oReff.reference_name__c);
	}

}
system.debug(mapOpportunity);
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Rajat
Try Below Code
list<oppotunity> lstOpp = [Select Id, Name, reference_name__c,(Select ID, Name, reference_name__c from Contract__r where reference_names__r !=null Limit 10) from opportunity where AccountId != null AND reference_name__c != null AND Owner.AccountId =loggedInUser.Contact.AccountId limit 10];

Map<string,string> mapOpportunity = new Map<string,string>();
for(opportunity oOpp : lstOpp){
	if(oOpp.reference_name__c != null){
	mapOpportunity.put('Oppotunity>> '+oOpp.Name, oOpp.reference_name__c);
	}
	
	for(reference_names__c oReff : oOpp.reference_names__r){

	mapOpportunity.put('Refference Names>> 'oReff.Name, oReff.reference_name__c);
	}

}
system.debug(mapOpportunity);
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
Maharajan CMaharajan C
Hi Rajat,

you can try like below:
 
Opportuniy opp = Select Id, reference_name__c,(Select ID, reference_name__c from Contract__r where reference_name__c !=null Limit 1) from opportunity 
where AccountId != null AND Owner.AccountId =loggedInUser.Contact.AccountId limit 10

String refName = '';
if(opp.reference_name__c != null){
	refName = opp.reference_name__c;
}
else if(opp.Contract__r.size() > 0){
	if(opp.Contract__r[0].reference_name__c != null){
		refName = opp.Contract__r[0].reference_name__c;
	}
}

return refName;

Thanks,
Maharajan.C
Robert A. EliasRobert A. Elias
 think my question wasn't as clear as it could have been: Does my company need a "Sandbox (Developer Pro)" subscription in order to create connected apps that are able to interact with said organization's SalesForce account? 

Dinar Detectives Update(https://www.dinarguru.onl/)