You need to sign in to do that
Don't have an account?
Rafael.Martins.Santos
Get parents Id's using Nested SOQL in child object.
Hi
I have a child object classed OpportunityItem__c that the parent is Opportunity, and in the OpportunityItem__ I have a relationship field that get the Id from an Object called Goal__c.
In the OpportunityItem__c I have a trigger, in these trigger I have to get the Opportunity Id and get the Goal Id too.
My code is like this:
for(OpportunityItem__c item : trigger.new)
{
opportunityId = item.OpportunityId; //(relationship field Name in OpportunityItem__c)
year = item.CloseDate__c;
}
list<Goal__c> goalsList = [SELECT Id, Year__c FROM Goal__c WHERE Year__c =: year];
list<Opportunity> opportunities = [SELECT Id, Name FROM Opportunity WHERE Id=: opportunityId ];
Until here ok.
But When I test, both list get a exception of SOQL Limit 101.
I want some manner to get the data of Goal and Opportunity that have relationship with the current item that fire a trigger.
Someone knows how I do that?
Best Regards
Rafael
I have a child object classed OpportunityItem__c that the parent is Opportunity, and in the OpportunityItem__ I have a relationship field that get the Id from an Object called Goal__c.
In the OpportunityItem__c I have a trigger, in these trigger I have to get the Opportunity Id and get the Goal Id too.
My code is like this:
for(OpportunityItem__c item : trigger.new)
{
opportunityId = item.OpportunityId; //(relationship field Name in OpportunityItem__c)
year = item.CloseDate__c;
}
list<Goal__c> goalsList = [SELECT Id, Year__c FROM Goal__c WHERE Year__c =: year];
list<Opportunity> opportunities = [SELECT Id, Name FROM Opportunity WHERE Id=: opportunityId ];
Until here ok.
But When I test, both list get a exception of SOQL Limit 101.
I want some manner to get the data of Goal and Opportunity that have relationship with the current item that fire a trigger.
Someone knows how I do that?
Best Regards
Rafael
I think you should provide more of your trigger's code to better see what is going wrong, from what you've provided the trigger will loop over all of the OpportunityItem__c records in the trigger updating your opportunityId and year variables, then it will make 2 queries, and then end.
You also said in your first sentance, you have a relationship field on OpportunityItem__c to the Goal__c object however you are trying to use the Year to find the Goal__c record rather than the Id that would be available from the lookup field, you are also trying to use item.OpportunityId, this is an unexpected field name on a custom object it should be something like OpportunityId__c or Opportunity_Id__c?
There is another way to ai attach an file?
Now I'm facing the SOQL Limit on the line 043.
There is some way to I attach my entire code here?
I will try post again
if(trigger.isInsert || trigger.isUpdate){
for(item_da_Oportunidade__c item : trigger.new){
//Pega os dados do objeto oportunidade
for(Opportunity op : listaOportunidade){
if(item.Oportunidade__c == op.Id){
regional = op.Regional__c;
ano = op.CloseDate.year();
fase = op.StageName;
}
}
//-------------------------------- ESSA PARTE EXECUTA OS CALCULOS VALOR DE VENDA, VALOR DE COMPRA PARA ARGENTINA E CHILE -----------------------------------------
//INICIO DOS CALCULOS DE MARGEM, MVS E COMISSOES
if(item.Sale_Amount__c == null || item.Purchase_Amount__c == null || item.Extra_Expenses__c == null){
item.addError(' Verifique se algum desses campos esta em branco: Despesas extras, Valor de Venda, Valor de Compra. Caso não use, preencha com valor 0');
}else{
}
if(item.Sale_Amount_Dollar__c > 0){
if(item.Extra_Expenses__c == null || item.TC_Venta__c == null || item.TC_Compra__c == null || item.Sale_Amount_Dollar__c == null || item.Valor_de_Compra_en_Dolares__c == null){
item.addError(' Verifique se algum desses campos esta em branco: Despesas extras, Cambio de venda, Cambio de compra, Valor de Compra en Dolares, Valor de Venta en Dolares. Caso não use, preencha com valor 0');
}else{
//Efetua o calculo valor da venda em dolares * taxa e Valor da compra * taxa
item.Sale_Amount__c = item.Sale_Amount_Dollar__c * item.TC_Venta__c;
item.Purchase_Amount__c = item.Valor_de_Compra_en_Dolares__c * item.TC_Compra__c;
}
}
//----------------------------------------- ESTA PARTE EFETUA OS CALCULOS DE MARGEM, MVS E COMISSÕES ----------------------------------------------------------------
//Margem
if(regional == 'AR'){
if(item.Sale_Type__c == 'RESALE'){
item.Margin__c = (item.Sale_Amount__c * 0.956) - item.Purchase_Amount__c;
}
}
if(regional == 'CL' || regional == 'US'){
if(item.Sale_Type__c == 'RESALE'){
item.Margin__c = item.Sale_Amount__c - item.Purchase_Amount__c;
}
}
//MVS
if(regional == 'AR'){
if(item.Sale_Type__c == 'FEE'){
item.MVS__c = (item.Margin__c * 0.956) - item.Extra_Expenses__c;
}else{
if(item.Sale_Type__c == 'RESALE'){
item.Margin__c = item.Margin__c;
item.Margin__c = (item.Sale_Amount__c * 0.956) - item.Purchase_Amount__c;
item.MVS__c = item.Margin__c - item.Extra_Expenses__c;
}
item.MVS__c = item.Margin__c - item.Extra_Expenses__c;
}
}
if(regional == 'CL' || regional == 'US'){
if(item.Sale_Type__c == 'RESALE'){
item.Margin__c = item.Sale_Amount__c - item.Purchase_Amount__c;
item.MVS__c = item.Margin__c - item.Extra_Expenses__c;
}else{
item.MVS__c = item.Margin__c - item.Extra_Expenses__c;
}
}else{
if(item.Sale_Type__c == 'FEE'){
item.MVS__c = (item.Margin__c * 0.8725)-item.Extra_Expenses__c;
}else{
item.MVS__c = item.Margin__c - item.Extra_Expenses__c;
}
}
//Comissões Vendedores, Arquitetos e Diretores
if(ano >= 2017){
//Calcula 25% do valor da venda
bonusMVS = item.Sale_Amount__c * 0.25;
if(item.Categoria__c == 'IT SERVICES'){
if(item.MS_Customer__c == true){
item.Fee_Sales_Rep__c = item.MVS__c * 0.03;
item.Fee_Pre_Sales__c = item.MVS__c *0.015;
item.Fee_Sales_Manager__c = item.MVS__c * 0.0075;
item.Fee_BU_Manager__c = 0;
}else{
item.Fee_Sales_Rep__c = item.MVS__c * 0.06;
item.Fee_Pre_Sales__c = item.MVS__c *0.03;
item.Fee_Sales_Manager__c = item.MVS__c * 0.015;
item.Fee_BU_Manager__c = 0;
}
}
if(item.Categoria__c == 'MANAGED SERVICES'){
//Quando o Cliente for MS
if(item.MS_Customer__c == true){
item.Fee_Sales_Rep__c = item.MVS__c * 0.03;
item.Fee_Sales_Manager__c = item.MVS__c * 0.0075;
item.Fee_BU_Manager__c = item.MVS__c * 0.01;
item.Fee_Pre_Sales__c = item.MVS__c * 0.015;
}else{
// Calcula bonus quando o mvs for maior ou igual a 25% do valor da venda do item da oportunidade.
if(item.MVS__c >= bonusMVS){
item.Fee_Sales_Rep__c = item.MVS__c * 0.08;
item.Fee_Pre_Sales__c = item.MVS__c * 0.05;
item.Fee_Sales_Manager__c = item.MVS__c * 0.015;
item.Fee_BU_Manager__c = item.MVS__c * 0.01;
}else{
item.Fee_Sales_Rep__c = item.MVS__c * 0.06;
item.Fee_Pre_Sales__c = item.MVS__c *0.03;
item.Fee_Sales_Manager__c = item.MVS__c * 0.015;
item.Fee_BU_Manager__c = item.MVS__c * 0.01;
}
}
}
if(item.Categoria__c == 'PRODUCTS'){
if(item.MS_Customer__c == true){
item.Fee_Sales_Rep__c = item.MVS__c * 0.03;
item.Fee_Pre_Sales__c = item.MVS__c *0.015;
item.Fee_Sales_Manager__c = item.MVS__c * 0.0075;
item.Fee_BU_Manager__c = 0;
}else{
item.Fee_Sales_Rep__c = item.MVS__c * 0.04;
item.Fee_Pre_Sales__c = item.MVS__c *0.02;
item.Fee_Sales_Manager__c = item.MVS__c * 0.01;
item.Fee_BU_Manager__c = 0;
}
}
}//FIM DOS CALCULOS DE MARGEM, MVS E COMISSOES
if(fase == 'Fechado e ganho'){
if(item.Arquitetos__c != null){
arquitetoId = item.Arquitetos__c;
}
if(opportunityId != null || opportunityId != ''){
for(User user : users){
if(opportunityId == user.Id && user.L_der_Imediato__c != null){
lider_imediato = user.L_der_Imediato__c;
}
if( arquitetoId != null && arquitetoId == user.ContactId){
userId = user.Id;
}
}
//Caso exista uma meta criada, será relacionado os itens com as metas.
for(Metas__c meta : metas){
if(meta.OwnerId == opportunityId && meta.Ano__c == ano){
item.Meta_Vendedor__c = meta.Id;
}
if(lider_imediato != null){
if(meta.OwnerId == lider_imediato && meta.Ano__c == ano){
item.Meta_Diretor__c = meta.Id;
}
}
if(userId != null){
if(userId == meta.OwnerId && meta.Ano__c == ano){
item.Meta_Arquiteto__c = meta.Id;
b = Boolean.valueOf(true);
}else{
if(userId != null && userId != meta.OwnerId){
idsArquitetos.add(userId);
}
}
}
}
if(idsArquitetos != null && b == false){
for(String idArquiteto : idsArquitetos){
Metas__c novaMetaArquiteto = new Metas__c();
novaMetaArquiteto.OwnerId = idArquiteto;
novaMetaArquiteto.Ano__c = ano;
insert novaMetaArquiteto;
if(novaMetaArquiteto != null){
item.Meta_Arquiteto__c = novaMetaArquiteto.Id;
}
}
}
}
}
}
}
}//------------------------------------------------------------------- FIM BEFORE ------------------------------------------------------------
I share my all code here.
3 triggers
But I think if I did some import with multiple data, maybe this error occur.
And if I try publish this code, maybe I will cannot publish because will occur the same error.
All the update I'm doing is out of an loop, to avoid multiple updates.