+ Start a Discussion
HamptonHampton 

Apex Help

Hello:

 

I have two unrelated objects that have the same Name value that I am attempting to write a code for to update a field.

 

I am trying to update the CBW_Rev_Share__c value using a formula with a value from Site__c

 

Below is my code. I am not getting a value back in the CBW_Rev_Share__c field.

trigger RevShareUpdate on Penetration_Summary__c (before insert, before update) {

Set<String> penSumName = new Set<String>(); 

for (Penetration_Summary__c pen : Trigger.new) {
    if(pen.Current__c == TRUE) {
        penSumName.add(pen.Name);
    }
} 
Set<String> releaseNameStrs = new Set<String>();
Map<String, Release__c> releaseSumMap = new Map<String, Release__c>();
for(Release__c release1:[Select ID, Name, CBW_Rev_Share__c, CBW_Rev_Share__r.Tier_One__c from Release__c Where Name IN: penSumName ]) {
    releaseSumMap.put(release1.Name, release1);
    releaseNameStrs.add(release1.Name);
}


List<Penetration_Summary__c> penForUpdate = new List<Penetration_Summary__c>();

for(Penetration_Summary__c c:[Select Id, Name, CBW_Rev__c, CBW_Rev_Share__c from Penetration_Summary__c where Name In : releaseNameStrs ]) {
if(releaseSumMap.containsKey(c.Name))
{
if(releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c != null && c.CBW_Rev__c != null)
c.CBW_Rev_Share__c = (releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c * c.CBW_Rev__c);
else
c.CBW_Rev_Share__c = 0; 
penForUpdate.add(c);
}
}
}

 

Thanks,

 

Hampton

Best Answer chosen by Admin (Salesforce Developers) 
s_k_as_k_a

HAmpton,

 

run this below code and post you debug log.

you can open debug logs  

adminstravitve set up  --------->  Deboglog--------->  click " new" button and select your name, once you added your name go and modify any penetration_summary__c the check the debug logs.

trigger RevShareUpdate on Penetration_Summary__c (before insert, before update) {

Set<String> penSumName = new Set<String>(); 

for (Penetration_Summary__c pen : Trigger.new) {
    if(pen.Current__c == TRUE) {
        penSumName.add(pen.Name);
    }
}
system.debug('*****PensumName set NAmes*'+ penSumName);
Map<String, Release__c> releaseSumMap = new Map<String, Release__c>();
for(Release__c release1:[Select ID, Name, CBW_Rev_Share__c, CBW_Rev_Share__r.Tier_One__c from Release__c Where Name IN :penSumName ]) {
    releaseSumMap.put(release1.Name, release1);   
}

system.debug('*****releaseSumMap size****'+ releaseSumMap.size());
List<Penetration_Summary__c> penForUpdate = new List<Penetration_Summary__c>();

for(Penetration_Summary__c c : trigger.new)
{
 if(releaseSumMap.containsKey(c.Name))
 {
   if(releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c != null && c.CBW_Rev__c != null)
        system.debug('*******CBW_Rev_Share__r.Tier_One__c value*******'+c.CBW_Rev_Share__r.Tier_One__c);
	system.debug('*******CBW_Rev__c value*******'+c.CBW_Rev__c);
        c.CBW_Rev_Share__c = (releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c) * c.CBW_Rev__c;
	system.debug('*******CBW_Rev_Share__c value*******'+c.CBW_Rev_Share__c);
   else
        c.CBW_Rev_Share__c = 0; 
   penForUpdate.add(c);
  }
}
}

 

All Answers

Suresh RaghuramSuresh Raghuram
c.CBW_Rev_Share__c = (releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c * c.CBW_Rev__c);

I believe in the above code you are calling a relation ship field CBW_Rev__r with name not with Id.

that may need Id not the name,
So map with ids intead name and get tier_One__c

Change your whole code like this and see

trigger RevShareUpdate on Penetration_Summary__c (before insert, before update) {

Set<String> penSumName = new Set<String>(); 

for (Penetration_Summary__c pen : Trigger.new) {
    if(pen.Current__c == TRUE) {
        penSumName.add(pen.Name);
    }
} 
Set<String> releaseNameStrs = new Set<String>();
Map<String, Release__c> releaseSumMap = new Map<String, Release__c>();
for(Release__c release1:[Select ID, Name, CBW_Rev_Share__c, CBW_Rev_Share__r.Tier_One__c from Release__c Where Name IN: penSumName ]) {
    releaseSumMap.put(release1.Id, release1);
    releaseNameStrs.add(release1.Id);
}


List<Penetration_Summary__c> penForUpdate = new List<Penetration_Summary__c>();

for(Penetration_Summary__c c:[Select Id, Name, CBW_Rev__c, CBW_Rev_Share__c from Penetration_Summary__c where Name In : releaseNameStrs ]) {
if(releaseSumMap.containsKey(c.Id))
{
if(releaseSumMap.get(c.Id).CBW_Rev_Share__r.Tier_One__c != null && c.CBW_Rev__c != null)
c.CBW_Rev_Share__c = (releaseSumMap.get(c.Id).CBW_Rev_Share__r.Tier_One__c * c.CBW_Rev__c);
else
c.CBW_Rev_Share__c = 0; 
penForUpdate.add(c);
}
}
}

I may be not given the exact code the above just an idea how it goes.
If this answers your question make this as a solution and give KUDOS.

HamptonHampton

Thanks for the quick response. I just tried it again and still am not getting a value in CBW_Rev_Share__c.

 

I have $1,000 in CBW_Rev__c and 3.25 in Release__c.CBW_Rev_Share__r.Tier_One__c

 

CBW_Rev_Share__c and CBW_Rev__c are currency fields. Release__c.CBW_Rev_Share__r.Tier_One__c is a percent field. If that is important to this.

 

Any other thoughts?

 

Thanks,

 

Hampton

Suresh RaghuramSuresh Raghuram

Hampton

 

write a system.debug('*************CBW :'+ CBW_Rev__c);

 

Add like this after evey value  and got debug logs 

setup-> monitoring->debuglogs.

 

run your code and check refresh the debuglog page and check in the recent log for the values you were expecting

 

you can learn about the values that are comming .

 

If this answers your question make this as solution and give kudos plz

s_k_as_k_a

HAmpton,

 

run this below code and post you debug log.

you can open debug logs  

adminstravitve set up  --------->  Deboglog--------->  click " new" button and select your name, once you added your name go and modify any penetration_summary__c the check the debug logs.

trigger RevShareUpdate on Penetration_Summary__c (before insert, before update) {

Set<String> penSumName = new Set<String>(); 

for (Penetration_Summary__c pen : Trigger.new) {
    if(pen.Current__c == TRUE) {
        penSumName.add(pen.Name);
    }
}
system.debug('*****PensumName set NAmes*'+ penSumName);
Map<String, Release__c> releaseSumMap = new Map<String, Release__c>();
for(Release__c release1:[Select ID, Name, CBW_Rev_Share__c, CBW_Rev_Share__r.Tier_One__c from Release__c Where Name IN :penSumName ]) {
    releaseSumMap.put(release1.Name, release1);   
}

system.debug('*****releaseSumMap size****'+ releaseSumMap.size());
List<Penetration_Summary__c> penForUpdate = new List<Penetration_Summary__c>();

for(Penetration_Summary__c c : trigger.new)
{
 if(releaseSumMap.containsKey(c.Name))
 {
   if(releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c != null && c.CBW_Rev__c != null)
        system.debug('*******CBW_Rev_Share__r.Tier_One__c value*******'+c.CBW_Rev_Share__r.Tier_One__c);
	system.debug('*******CBW_Rev__c value*******'+c.CBW_Rev__c);
        c.CBW_Rev_Share__c = (releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c) * c.CBW_Rev__c;
	system.debug('*******CBW_Rev_Share__c value*******'+c.CBW_Rev_Share__c);
   else
        c.CBW_Rev_Share__c = 0; 
   penForUpdate.add(c);
  }
}
}

 

This was selected as the best answer
HamptonHampton

I'm haven't looked closely at this code, but whatever changes were made worked. I now have a vaule in the field that is the correct value.

 

Thank you so much for your help.

 

Hampton

HamptonHampton

This working fantastic, thank you again for your help...now I need to write a test class to get the code coverage up to 100%...and I have no idea where to start. Can you at least help me get started and I will take care of the rest?

 

Thanks,

 

Hampton