+ Start a Discussion
JensenleeJensenlee 

Trigger to Change Status

Dear all,

 

I am very new and sforce apex coding with very little knowledge about programming - but I am very keen to learn!

 

Below is a very simple scenario which I am trying is achieve:

 

My requirements is that everytime a new record is inserted or updated to "Context" custom object, I wish to have the status of all records (new and existing) records in "Context" to change to Inactive

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
trigger ChangeContextStatus on Context__c (after insert, after update) 

{

List <Context__c> oCT = [select Status__c, from Context__c 
WHERE End_Markets__c = 'Singapore Domestic'];

    for (Context__c ct: oCT)
    {
                   ct.Status__c = True;
                
    }
    update oCT;
}

If anyone could help, thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Rahul S.ax961Rahul S.ax961

Hi Jen,

 

I modified the code, hope it helps.

 

trigger ChangeContextStatus on Context__c (after insert, after update)
{
    List <Context__c> oCT = [select Status__c, from Context__c WHERE End_Markets__r.name = 'Singapore Domestic' AND Id in: Trigger.new]; // here, since its an M-D relation. u have to filter query by name of end market. and also check whether id is in trigger.new
    if(!oCT.isEmpty())
    {
        for (Context__c ct: oCT)
        {
               ct.Status__c = True;     // you told status should be inactive, but updating with true will make it active.
            
        }
        update oCT;
        }
    }
}

 

Thanks & regards,

Rahul Sharma

All Answers

JensenleeJensenlee

Apologise, I have corrected some syntax errors, but it is still not working :smileysad:

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

trigger ChangeContextStatus on Context__c (after insert, after update) 
{

List <Context__c> oCT = [select Status__c from Context__c 
WHERE End_Markets__c = 'Singapore Domestic'];

    for (Context__c ct: oCT)
    {
                ct.Status__c = True;
                
    }
    update oCT;
}
Ritesh AswaneyRitesh Aswaney

trigger ChangeContextStatus on Context__c (after insert, after update) 
{

List <Context__c> oCT = [select Status__c from Context__c 
WHERE End_Markets__c = :'Singapore Domestic'];            //Added colon before bind variable

    for (Context__c ct: oCT)
    {
                ct.Status__c = True; // should be True in single quotes, unless Status__c is boolean
                
    }
    update oCT;
}

JensenleeJensenlee

Dear Ritesh,

 

Thanks for your reply. Unfortunately the trigger is still not fiiring.

 

There are no errors but the context status continues to be false  when I add a new Context Record where End Market is "Singapore Domestic" (master child lookup)

 

status__ci s boolean

 

Thanks.

Ritesh AswaneyRitesh Aswaney

The trigger should fire, even if it doesnt do what you intend for it to, as that could be a problem with the logic.

 

I'd recommend adding some System.debug messages and then checking the Monitoring > Debug Logs to see what's happening.

Rahul S.ax961Rahul S.ax961

Hi Jen,

 

I modified the code, hope it helps.

 

trigger ChangeContextStatus on Context__c (after insert, after update)
{
    List <Context__c> oCT = [select Status__c, from Context__c WHERE End_Markets__r.name = 'Singapore Domestic' AND Id in: Trigger.new]; // here, since its an M-D relation. u have to filter query by name of end market. and also check whether id is in trigger.new
    if(!oCT.isEmpty())
    {
        for (Context__c ct: oCT)
        {
               ct.Status__c = True;     // you told status should be inactive, but updating with true will make it active.
            
        }
        update oCT;
        }
    }
}

 

Thanks & regards,

Rahul Sharma

This was selected as the best answer
JensenleeJensenlee
Dear Rahul, Thank you for your reply to both my post.
Rahul S.ax961Rahul S.ax961

Nice to hear it Helped.... :smileyhappy: