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
FabianGFabianG 

Cross Object Field Update Trigger - Custom to Standard Object

Dear all,

I'm trying to develop an apex trigger that populates a checkbox-field on a standard object if a date-field on a custom object is populated/not empty.
Due to a missing/variable relationship this can't be done using a workflow. Both objects have a unique company-id in common which coult be used to relate them to each other.
I know more or less that I have to build a list in the trigger to retrieve the id's and map them but I can' figure out how to to write a working trigger.

Custom object: SProfile__c
Custom object date-field: ActivationDate__c
Standard object: Lead
Standard object checkbox: Activated__c
Matching ID: CompanyID__c

I hope the information helps. I very much appreciate your help!

Cheers
Fabian

 
Best Answer chosen by FabianG
Sid_CloudSid_Cloud
Try and edit the below code. Should work:
trigger updateActivatedFieldLead on SProfile__c(before insert, before update) {
    List<Lead> leadList = new List<Lead>();
    List<Id> companyId = new List<Id>();
    
    for(SProfile__c sp : Trigger.new) {
        if(sp.ActivationDate__c!=null) {
        	companyId.add(sp.Company__c);    
        }
    }
    
leadList = [SELECT Id, Activated__c FROM Lead WHERE Company__c IN : companyId];
    for(Lead l : leadList) {
        l.Activated__c = true;
    }
    update leadList;
}

You can of course add/modify the conditions as per your needs!

All Answers

Sid_CloudSid_Cloud
Try and edit the below code. Should work:
trigger updateActivatedFieldLead on SProfile__c(before insert, before update) {
    List<Lead> leadList = new List<Lead>();
    List<Id> companyId = new List<Id>();
    
    for(SProfile__c sp : Trigger.new) {
        if(sp.ActivationDate__c!=null) {
        	companyId.add(sp.Company__c);    
        }
    }
    
leadList = [SELECT Id, Activated__c FROM Lead WHERE Company__c IN : companyId];
    for(Lead l : leadList) {
        l.Activated__c = true;
    }
    update leadList;
}

You can of course add/modify the conditions as per your needs!
This was selected as the best answer
FabianGFabianG
Thank you @Sid_Cloud
The code looks like it should do what I want to do.
Unfortunately, the CompanyID Company__c is a string / text-field in our system because we retrieve it from an external system.
Probably for this reason, my testclass gibes me an Error on the ID

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateActivatedFieldLead: execution of BeforeInsert

caused by: System.StringException: Invalid id: 1234567

External entry point: []



My Test-Class looks as follows (I had to rename some of the fields relating to our German-speaking system):
 
@istest
public class UpdateAcivatedTestclass {
    static TestMethod void InsertLeadandSP(){
        Lead NewLead = new Lead();
        
        NewLead.Company = 'TestCompany';
        NewLead.LastName = 'Tester';
        NewLead.Salutation = 'Herr';
        NewLead.LeadSource = 'Inbound';
        NewLead.LeadSourceDetail__c = 'Google';
        NewLead.LeadPotential__c = 'A = Hoch';
        NewLead.Ansprache__c = 'Herr';
        NewLead.UnternehmensID__c = '1234567' ;
                
        Suchprofil__c SP = new Suchprofil__c();
        
        SP.Aktivierungsdatum__c = System.today();
        SP.UnternehmensID__c = '1234567' ;
        
        insert NewLead;
        insert SP;
        
    }

}

Any Ideas what I did wrong?
Sid_CloudSid_Cloud
Your test class looks fine to me. I am not sure what went wrong. Just a thought - Could you try creating a new Company record in this test class and use that ID as a reference instead of hard coding the value like you have mentioned here. I guess that might do the trick!! Maybe if you have any other test class where you have created a new company record - you can copy the code from there and use it here. Let me know if that works for you!
FabianGFabianG
Your hint was not bad. I simply used an old testclass, which did not  get errors. But: When I simply tested the Trigger in my sandbox by filling in a date in the ActivationDate-field, I got another error:

updateActivatedFieldLead: execution of BeforeUpdate caused by: System.StringException: Invalid id: 62131537: External entry point

So there seems to be any struggle with our internal ID, which derives from an external system and therefore seems to be not accecped by salesforce/trigger. Perhaps I have to try a different ID or mothod to link them up.
We have those searching-profiles attached to a lead in the beginning, both created by an external system. Unfortunately they can't be linked up by a fixed relationship because the searching-profile might furtheron also be attached/related to an account or an opportunity. Just to explain where everything comes from.

I really appreciate your efforts!

Thanks
Fabian
FabianGFabianG
Got a solution. My ID's weren't acutally ID's, but strings. So I simply had to change this:
 
//WAS:
List<Id> companyId = new List<Id>();

//HAS TO BE:
List<String> companyId = new List<String>();

 
Sid_CloudSid_Cloud
Thanks for the update. Good to know that you solved it! :)