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
bellardinabellardina 

trigger on standard object in order to update a field on custom object

There are 3 objects- Account(std object),oppurtunity(std object),Application(custom object).Oppurtunity and application are linked via master detail relationship.I I have created 2 record types for account. I need to write a trigger on account so that when account record type is set to a specific value and client id(text field on account) is not null then status(picklist field on application )is automatically updated to a specific value.

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

salesforce expert wrote:

my bad!

 

try this now..

 

if(!Trigger.isInsert) {

  for(Account ac: Trigger.new) {

    if(ac.Recortypeid == <some record type id> && ac.clientid__c != null) {
        Opportunity op = [select id from Opportunity where AccountId =: ac.Id];
       if(op != null) {
           Application__c ap = [select id, status__c from application__c where OppID__c =: op.Id])

           ap.status__c = <some status>;

           update ap;

       }

  }

 

if you still have issues post your code here!



FYI: this code places SOQL inside for loops and is not bulk safe. There are other issues as well: If no application exists, you will get an attempt to dereference a nul object. You need to know the exact recordtypeID instead of looking it up,

All Answers

salesforce expertsalesforce expert

try this code!

 

if(!Trigger.isInsert) {

  for(Account ac: Trigger.new) {

    if(ac.Recortypeid == <some record type id> && ac.clientid__c != null) {

       Application__c ap = [select id, status__c from application__c where account__c =: ac.Id])

           ap.status__c = <some status>;

           update ap;

       }

  }

}

 

you can add extra spices to this code...

 

let me know if this helps you!! i would be happy if this works for you!!

 

 

baskaran

bellardinabellardina

It is throwing an error:-' there is no column called account__c on entity application__c'. Account and application are not directly linked to each other.Account(std object) is related to oppurtunity(std object) by lookup relationship.Oppurtunity is linked to application(custom object) by master-detail relationship.Please help

salesforce expertsalesforce expert

my bad!

 

try this now..

 

if(!Trigger.isInsert) {

  for(Account ac: Trigger.new) {

    if(ac.Recortypeid == <some record type id> && ac.clientid__c != null) {
        Opportunity op = [select id from Opportunity where AccountId =: ac.Id];
       if(op != null) {
           Application__c ap = [select id, status__c from application__c where OppID__c =: op.Id])

           ap.status__c = <some status>;

           update ap;

       }

  }

 

if you still have issues post your code here!

Starz26Starz26

try this: Please note that this will update ALL application__c object for the given opportunity. If you only want to update a specific application for an Opportunity for a specific account, you will need to provide that criteria and add that to the loop as a filter when updating the Application__c object records....

 

//Build map of AccountIDs in trigger to list of relaited opportunity records
Map<Id, Opportunity[]) opps = New Map<id, Opportunity[]);
//Create list of Applications to be updated
Application__c[] tbuApps = New Application__c[]{};

//List of Account IDs in the trigger..You could just replace this with trigger.new.keySet() in the query below
Set<ID> acctIDs = New Set<id>();

//get Recordtype ID for the specific recordtype you are looking for
ID AccountRT = [SELECT Id FROM RecordType where Name = 'RECORDTYPENAME' and sObjectType = 'Account' and IsActive = true LIMIT 1].Id;

//populate Set of account IDs
acctIDs = trigger.newMap.keySet();

//build map of Account IDs to the respective list of Opportunities with the associated Application Records
For(Opportunity opp : [Select ID, AccountID, (Select Status__c From Applications__r **Make sure this API name is correct**) From opportunity Where AccountID IN :acctIDs){
    
    if(opps.containsKey(opp.AccountID))
        opps.get(opp.AccountID).add(opp)
    else
        opps.put(opp.AccountID, New Oportunity[]{opp})
}

//Process the trigger records
For (Account a : trigger.new){
   //If the account RT and clientID meet criteria
   If(a.RecordTypeID == AccountRT && clientID__c != null){
     //See if we have a corresponding Opportunity
     if(opps.containsKey(a.id){
         //process any Application records for the opportunity if they exist
         for(Application__c app : oppList.get(a.id).Applications__r){
           //this is where you put additional filter criteria if you want to select only certain Apps  
           app.Status__c = 'YOURSTATUSHERE';
           //add to the list to update
           tbuApps.add(app);
         }
      }
    }
}
//Check to see if we have updates and if so do them
if (tbuApps.size() > 0)
   update tbuApps;

 

Starz26Starz26

salesforce expert wrote:

my bad!

 

try this now..

 

if(!Trigger.isInsert) {

  for(Account ac: Trigger.new) {

    if(ac.Recortypeid == <some record type id> && ac.clientid__c != null) {
        Opportunity op = [select id from Opportunity where AccountId =: ac.Id];
       if(op != null) {
           Application__c ap = [select id, status__c from application__c where OppID__c =: op.Id])

           ap.status__c = <some status>;

           update ap;

       }

  }

 

if you still have issues post your code here!



FYI: this code places SOQL inside for loops and is not bulk safe. There are other issues as well: If no application exists, you will get an attempt to dereference a nul object. You need to know the exact recordtypeID instead of looking it up,

This was selected as the best answer
salesforce expertsalesforce expert

Thanks for pin pointing the issues in my code. i really appreciate!! can you tell how much batch size a trigger can handle if you say it is bulk safe?

 

 

- baskaran

Starz26Starz26

In order to be "Bulkified" the trigger must be able to handle ANY amount of records thrown at it. With that said, a trigger runs in maximum batches of 200 records per batch.

salesforce expertsalesforce expert

thanks a lot!

Timothy Gentet-O'Brien 20Timothy Gentet-O'Brien 20
All of the code snippets pasted above should not be implemented in a SF org. You should NEVER run a SOQL Query within a for loop.

You should collate the data outside of the for loop, using collection data types, then use those within the for loop so you are not possibly hitting governor limits.
List<Application__c> applications = [SELECT Id, Status__c, OppID__c
                                     FROM Application__c
                                     WHERE OppID__r.AccountId IN :Trigger.new.keySet()];

for(Application__c app : applications){
    app.Status__c = 'Updated Value';
}

update applications;