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
HamptonHampton 

Field Update on Unrelated Object

Hello:

 

I have to unrelated custom objects - Penetration_Summary__c and Release__c.

 

Penetration_Summary__c and Release__c will have the same name.

 

I need to update a field on Penetration_Summary__c using a formula that is:

 

(Penetration_Summary__c.CBW_Rev__C * Release__c.CBW_Rev_Share__r.Tier_One__c)

 

Below is the trigger I have written but am getting an error around the map. Any thoughts on how to proceed?

 

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);
    }
} 

Map<ID, Name> releaseSumMap = new Map<ID, Name>();
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.Name);
}


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

for(Penetration_Summary__c c:[Select Id, CBW_Rev__c, CBW_Rev_Share__c from Penetration_Summary__c where Name In : releaseSumMap.Name ]) {
    
    (c.CBW_Rev_Share__c) == ((releaseSumMap.get.(c.ID).CBW_Rev_Share__r.Tier_One__c)*c.CBW_Rev__c);
}
update penForUpdate;    
}

 Thanks,

 

Hampton

s_k_as_k_a

Here is the modified code

 

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<ID, Release__c> releaseSumMap = new Map<ID, 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, release);
    releaseNameStrs.add(release1.Name);
}


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

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

if(penForUpdate.size()>0)
     update penForUpdate;    
}

 

HamptonHampton

s_k_a:

 

Thank's for the quick reply. The good news is the code saved without any errors. It is still not putting a value into the Penetration_Summary__c.CBW_Rev_Share__c

 

Any other thoughts?

 

Thanks,

 

Hampton

s_k_as_k_a

Check the  data type of fields  CBW_Rev_Share__c, CBW_Rev__C on Penetration_Summary__c and Tier_one__c on Release__c. if all field data types are number s, add condition to check null values.

 

Here is the modified  block code

 

for(Penetration_Summary__c c:[Select Id, 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);
}
}

 

 

 

 

HamptonHampton

So I think the issue here is still around the map. The query is looking in releaseSumMap for c.ID, which does not exist. The objects are unrelated and their Name value is the same. I've tried playing with it, changing ID to name, etc but it still won't update.

 

Any other thoughts?

 

trigger RevShareUpdate on Penetration_Summary__c (after insert, after 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<ID, Release__c> releaseSumMap = new Map<ID, 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.Name);
}


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

for(Penetration_Summary__c c:[Select Id, 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.Name).CBW_Rev_Share__r.Tier_One__c * c.CBW_Rev__c;
else
c.CBW_Rev_Share__c = 0; 
penForUpdate.add(c);
}
}
}

 

sushant sussushant sus

hi change this

 

for(Penetration_Summary__c c:[Select Id, CBW_Rev__c, CBW_Rev_Share__c from Penetration_Summary__c where Name In : releaseSumMap.Name ]) {
    
    (c.CBW_Rev_Share__c) == ((releaseSumMap.get.(c.ID).CBW_Rev_Share__r.Tier_One__c)*c.CBW_Rev__c);
}
update penForUpdate;    
}

to

for(Penetration_Summary__c c:[Select Id, CBW_Rev__c, CBW_Rev_Share__c from Penetration_Summary__c where Name In : releaseSumMap.value() ]) {
    
    (c.CBW_Rev_Share__c) == ((releaseSumMap.get.(c.ID).CBW_Rev_Share__r.Tier_One__c)*c.CBW_Rev__c);
penForupdatea.add(c); }
update penForUpdate; }
HamptonHampton

Appreciate the changed code, still did not get a value in the field. I am lost at this point.

 

Hampton

sushant sussushant sus
try this i think it will work ..


Penetration_Summary__c c1=releaseSumMap.get(c.Name);

c.CBW_Rev_Share__c = (c1.CBW_Rev_Share__r.Tier_One__c) * (c.CBW_Rev__c);