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
Matthew HamptonMatthew Hampton 

Code Help Please - Return ID

If anyone can help me with this code before I chuck my mouse and keyboard across the room I would appreciate.

 

I have the following code written, tested and deploye don customer object Subscriber__c:

 

trigger SubUpdate on Subscriber__c (before insert) {
Set<String> AddressSet = new Set<String>();
Set<String> LocSet = new Set<String>();
Set<String> ZipSet = new Set<String>();
                    for(Subscriber__c s: Trigger.New)
                             {
                                      AddressSet.add(s.street_address__c);
                                      LocSet.add(s.loc__c);
                                      ZipSet.add(s.zip_code__c);
                              }
                              
list<Fiber_Qualified_Address__c> gpon = [select id,name, street_address__c, loc__c, zip_code__c from Fiber_Qualified_Address__c where street_address__c IN: AddressSet
and loc__c IN: LocSet
and zip_code__c IN: ZipSet];

        if(gpon.size()>0)

        {
                Map<ID, String> FQAID = new Map<ID, String>();
                for(Fiber_Qualified_Address__c FQAID2: gpon)
                {
                Map<String, Fiber_Qualified_Address__c> FQAMap = new Map<String, Fiber_Qualified_Address__c>();
                for(Fiber_Qualified_Address__c foa: gpon)
                {
                            FQAMAP.put(foa.street_address__c + '' + foa.loc__c + '' + foa.zip_code__c, foa);
                }
                for(Subscriber__c s: Trigger.new)
                {
                            if(FQAMAP.containsKey(s.street_address__c + '' + s.loc__c + '' + s.zip_code__c))
                            s.fiber_address__c = FQAMap.get(s.street_address__c + '' + s.loc__c + '' + s.zip_Code__c).Id;
                            }
             }
}
}

 

Long story short, the code takes three pieces of information from the subscriber record, looks to see if there is a matching record on custom object Fiber_Qualified_Addressess__c and if there is, creates a lookup relationship on subscriber. This is done everytime I update or insert new subscriber records.

 

What I need to do now is take the code and tweak it slightly so that it works on Fiber_Qualified_Addresses__c. What I need is the code to work and create the same relationship after insertion of new Fiber_Qualified_Addresses__c records. I have spent three hours so far today working on this with no luck. Any help would be greatly appreciated.

 

Thanks,

 

Matt

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

This is because you are setting the fiber_address__c field into the FQAmap entry, but you aren't saving the changes back to the database.

 

You'll need something like:

 

 

 if(gpon.size()>0)
 {
       List<Subscriber__c> toUpdate=new List<Subscriber__c>();
       Map<ID, String> FQAID = new Map<ID, String>();
       for(Subscriber__c FQAID2: gpon)
       {
                Map<String, Subscriber__c> FQAMap = new Map<String, Subscriber__c>();
                for(Subscriber__c foa: gpon)
                {
                            FQAMAP.put(foa.street_address__c + '' + foa.loc__c + '' + foa.zip_code__c, foa);
                }
                for(Fiber_Qualified_Address__c s: Trigger.new)
                {
                     if(FQAMAP.containsKey(s.street_address__c + '' + s.loc__c + '' + s.zip_code__c))
                     {
                       Subscriber__c cand = 
                          FQAMap.get(s.street_address__c + '' + s.loc__c + '' + s.zip_Code__c);
                       cand.fiber_address__c = s.id;
                       toUpdate.add(cand);
                }
       }

       update toUpdate;
}

 

 

All Answers

bob_buzzardbob_buzzard

So just to clarify your requirement - do you want the reverse behaviour?  E.g. when a Fiber_Qualified_Address__c is inserted, if there is a subscriber whose street address, location and zip code matches, then a lookup should be created to that subscriber?

 

I assume you've been the route of simply changing all Fiber_Qualified_Address__c references to Subscriber__c and vice versa?  If so, what problems did you encounter? 

kritinkritin

HI

 
Use below logic and see the magic.
 

trigger SubUpdate on Subscriber__c (before insert)​ {
Set<String> AddressSet = new Set<String>();
Set<String> LocSet = new Set<String>();
Set<String> ZipSet = new Set<String>();


String sTempKey='';


                    for(Subscriber__c s: Trigger.N​ew)
                             {
                            
     AddressSet.add(s.street_address__c);
                                        LocSet.add(s.loc__c);
                                        ZipSet.add(s.zip_code__c);

                              }
                             

 

Map<String,Id> MapFQA=new Map<String,Id>();

for(Fiber_Qualified_Address__c FQA:[select id,name, street_address__c, loc__c, zip_code__c from Fiber_Qualified_Address__c  where street_address__c IN: AddressSet
and loc__c IN: LocSet
and zip_code__c IN: ZipSet]){

 sTempKey = FQA.street_address__c +'|' + FQA.loc__c '|' + FQA.zip_code__c;
 MapFQA.put(sTempKey1,FQA.Id);

}

for(Subscriber__c s: Trigger.new){
sTempKey= s.street_address__c +'|' + s.loc__c '|' + s.zip_code__c;
Id FQAId;
 if(MapFQA!=null)
 {
 FQAId= MapFQA.get(sTempKey);
 s.fiber_address__c =FQAId;
 }


}

       

}

Matthew HamptonMatthew Hampton

Yes, that is correct, when a Fiber_Qualified_Address__c is inserted, I want a subscriber who matches the three criteria to update with the corresponding ID of the FIber_Qualified_Address__c

 

I have tried just changing Fiber_Qualified_Addresses__c to Subscriber__c and vice versa and the issue is with the assignment of the s object on the update portion of the code (since subscriber is not variable s, there is an issue).

bob_buzzardbob_buzzard

Have you tried Kritin's suggested code above - it looks like it will satisfy your requirements to me.

Matthew HamptonMatthew Hampton

I did. I am getting an error message that the variable Trigger.new does not exist on line 6.

 

Any thoughts?

bob_buzzardbob_buzzard

That's very strange.  If you are in an insert or update triggers then this should be available.  I've just tried this in my dev org and the capitalisation doesn't make any difference.

 

That said, I'm not sure Kritin's code is going to do what you want - as its still a trigger on subscriber insert, but that doesn't appear to be what you were looking for.

 

I'd suggest you want to start by inverting your existing trigger - the behaviour you are looking for is identical across both triggers, just with the object's roles reversed.  It shouldn't be too difficult to nail any typos etc coming out of that.

 

 

Matthew HamptonMatthew Hampton

Here is the code that I put in on the Fiber_Qualified_Address__c object:

 

trigger FiberUpdate on Fiber_Qualified_Address__c (after insert, after update) {
Set<String> AddressSet = new Set<String>();
Set<String> LocSet = new Set<String>();
Set<String> ZipSet = new Set<String>();
                    for(Fiber_Qualified_Address__c s: Trigger.New)
                             {
                                      AddressSet.add(s.street_address__c);
                                      LocSet.add(s.loc__c);
                                      ZipSet.add(s.zip_code__c);
                              }
                             
list<Subscriber__c> gpon = [select id, name, fiber_address__c, street_address__c, loc__c, zip_code__c from Subscriber__c where street_address__c IN: AddressSet
and loc__c IN: LocSet
and zip_code__c IN: ZipSet];

        if(gpon.size()>0)

        {
                Map<ID, String> FQAID = new Map<ID, String>();
                for(Subscriber__c FQAID2: gpon)
                {
                Map<String, Subscriber__c> FQAMap = new Map<String, Subscriber__c>();
                for(Subscriber__c foa: gpon)
                {
                            FQAMAP.put(foa.street_address__c + '' + foa.loc__c + '' + foa.zip_code__c, foa);
                }
                for(Fiber_Qualified_Address__c s: Trigger.new)
                {
                            if(FQAMAP.containsKey(s.street_address__c + '' + s.loc__c + '' + s.zip_code__c))
                            FQAMap.get(s.street_address__c + '' + s.loc__c + '' + s.zip_Code__c).fiber_address__c = s.id;
                            }
             }
}
}

 

The code saved without any errors, however when I tested it with a dummy record in the Sandbox, it did not update the Subscriber__c record with the Fiber_Address__c ID. My guess is it is something in the final two rows that is not correct?

bob_buzzardbob_buzzard

This is because you are setting the fiber_address__c field into the FQAmap entry, but you aren't saving the changes back to the database.

 

You'll need something like:

 

 

 if(gpon.size()>0)
 {
       List<Subscriber__c> toUpdate=new List<Subscriber__c>();
       Map<ID, String> FQAID = new Map<ID, String>();
       for(Subscriber__c FQAID2: gpon)
       {
                Map<String, Subscriber__c> FQAMap = new Map<String, Subscriber__c>();
                for(Subscriber__c foa: gpon)
                {
                            FQAMAP.put(foa.street_address__c + '' + foa.loc__c + '' + foa.zip_code__c, foa);
                }
                for(Fiber_Qualified_Address__c s: Trigger.new)
                {
                     if(FQAMAP.containsKey(s.street_address__c + '' + s.loc__c + '' + s.zip_code__c))
                     {
                       Subscriber__c cand = 
                          FQAMap.get(s.street_address__c + '' + s.loc__c + '' + s.zip_Code__c);
                       cand.fiber_address__c = s.id;
                       toUpdate.add(cand);
                }
       }

       update toUpdate;
}

 

 

This was selected as the best answer
Matthew HamptonMatthew Hampton

Fantasitc, worked like a charm. Thank you so much for your help1