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
mac adminmac admin 

Trigger to updtae Status

Hi all,
I want to update the status field when an record is created in the 3rd object. Let say I have 3 objects A B and C. When a record is created in object C before inserting the record I want to check the email ID in the remaning objects A and B if the mail ID is matching with remaning two object i want show the flag symbool in those two objects and update the picklist in the object C. Can anyone help me over here.

Thanks in advance,
Regards,
mac.
RD@SFRD@SF
Hi Mac,

Whats the relation here between the objects.
I think writing a after insert trigger would suffice, but the object C should have a lookup up to the objects A and B to refer them and check for the email and update the flag in those records

Hope it helps
RD
mac adminmac admin
Thanks for the replay RD@SF
In my small change i want to update the status in the remaning two objects not in the object C. Can you help me over here.
RD@SFRD@SF
Hmm Ok,

For after insert trigger to pickup the other two records from objects A and B, atleast a lookup is needed in object C. i.e. two lookups one pointing to object A record and other pointing to object B record. so that the update on the records and checking the emails from A and B can be done and the object c record can be updated.

Do you have any such lookups?



 
Vijaya AmarnathVijaya Amarnath
Hi Mac,

check the below code..

trigger checkForAandB on objectC(After Insert)
{
    // declare list of other 2 objects to update
    List<objectA> existing_objA = new List<objectA>();
    List<objectB> existing_objB = new List<objectB>();
    
    Set<String> emails = new Set<String>(); // to keep all emails
    
    for(objectC oC : Trigger.New)
    {
        // check for email and add in set variable
        if(!String.IsBlank(oC.Email__c))
        {
            emails.add(oC.Email__c);
        }
    }
    
    //check if emails are there
    if(!emails.IsEmpty())
    {
        //query all obA records with Same email and map the status
        for(objectA oA : [SLECT Id,Email__c,Status__c FROM objectA WHERE Email__c IN: emails])
        {
            oA.Status__c = 'Flag';
            existing_objA.add(oA);
        }
        
        //query all obB records with Same email and map the status
        for(objectB oB : [SLECT Id,Email__c,Status__c FROM objectB WHERE Email__c IN: emails])
        {
            oB.Status__c = 'Flag';
            existing_objB.add(oB);
        }
        
        //Update those records
        Update existing_objA;
        Update existing_objB;
    }
}
mac adminmac admin
Hi Vijaya Amarnath, Thanks for the reply.
It's not updtaing the Status fields in the both object can you help me over here.

Thanks in advance.

Regards,
mac.
Vijaya AmarnathVijaya Amarnath
Hi Mac,

Please share the code..

Regards,
Vijaya Amarnath.
mac adminmac admin
Vijaya, Below is the code which I have created with reference to your code.
trigger verifyINQ on UnderGradute__c(After Insert)
{
    // declare list of other 2 objects to update
    List<Inquiry_Form_Leads__c> existing_objA = new List<Inquiry_Form_Leads__c>();
    List<Lead> existing_objB = new List<Lead>();
    
    Set<String> emails = new Set<String>(); // to keep all emails
    
    for(UnderGradute__c oC : Trigger.New)
    {
        // check for email and add in set variable
        if(!String.IsBlank(oC.Email__c))
        {
            emails.add(oC.Email__c);
        }
    }
    
    //check if emails are there
    if(!emails.IsEmpty())
    {
        //query all obA records with Same email and map the status
        for(Inquiry_Form_Leads__c oA : [SLECT Id,Email__c,Status__c FROM Inquiry_Form_Leads__c WHERE Email__c IN: emails])
        {
            oA.Converted_Status__c = 'Matched';
            existing_objA.add(oA);
        }
        
        //query all obB records with Same email and map the status
        for(Lead oB : [SLECT Id,Email,Status__c FROM Lead WHERE Email IN: emails])
        {
            oB.Status__c = 'Matched';
            existing_objB.add(oB);
        }
        
        //Update those records
        Update existing_objA;
        Update existing_objB;
    }
}

Can you help me over here.

Thanks in advance.

Regards,
mac.
Vijaya AmarnathVijaya Amarnath
Hi Mac,


Let me get you requirement. Suppose in Under Graduate record if you are entering a email ex: ug@gmail.com, then with the same email if foound any Lead/Inquiry Form Leads.. We need to update those 2 records.. Am I correct on your requirement?..
mac adminmac admin
Yes when the same email is found in the other two objects then a picklist in the two objects should be update.

I have created the code as you mentioned but the fields are not updtaing. Can you help me over here.


Thanks in advance.

Regards,
mac.
mac adminmac admin
Hi Vijay,

Can you hlep me with query to update the fields.