• Kevin Tullos
  • NEWBIE
  • 60 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 7
    Questions
  • 11
    Replies
I was thinking that you could have a trigger like this on the account, but it didn't work.  Please help me with it.



/*
    Created by: Kevin Tullos
    Last Update: 22 June 2014 by Kevin Tullos
    Questions?: kevin.tullos@fleetpride.com
*/
trigger deleteOld_Owner_tasks on Account (before update) {
   
        Set<Id> accountIds = new Set<Id>(); //set for holding the Ids of all Accounts that have been assigned to new Owners
        Map<Id, String> oldOwnerIds = new Map<Id, String>(); //map for holding the old account ownerId
        Map<Id, String> newOwnerIds = new Map<Id, String>(); //map for holding the new account ownerId
        Task[] TaskUpdates = new Task[0]; //Task sObject to hold OwnerId updates
       
        for (Account a : Trigger.new) { //for all records
            if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId) {
                oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId); //put the old OwnerId value in a map
                newOwnerIds.put(a.Id, a.OwnerId); //put the new OwnerId value in a map
                accountIds.add(a.Id); //add the Account Id to the set
            }
        }
       
        if (!accountIds.isEmpty()) { //if the accountIds Set is not empty
            for (Account act : [SELECT Id, (SELECT Id, Status, OwnerId FROM Tasks WHERE IsClosed = False) FROM Account WHERE Id in :accountIds]) { //SOQL to get Contacts and Opportunities for updated Accounts
                String newOwnerId = newOwnerIds.get(act.Id); //get the new OwnerId value for the account
                String oldOwnerId = oldOwnerIds.get(act.Id); //get the old OwnerId value for the account
               
                for (Task t : act.Tasks) { //for all tasks
                    if (t.OwnerId == oldOwnerId) { //if the task is assigned to the old account Owner
                    delete t;
                    }
                } 
          
            }
           
        }
  
}
I want to delete all the open tasks that are associated with an account when the account owner is changed. I would post the code that i have written, but none of it is useful.

it can either be batch apex or a trigger.

Thanks.

Kevin

I am trying to write a trigger that updates the account owner with the user who has the cooresponding salesman_ID field.  The account has OwnerID, and Salesman_Number__c, and I need to get the ID from the User Record that has the matching Salesman_Number__c.  I have tried a lot of things and I cannot get them to compile (error:  Error: Compile Error: Illegal assignment from Schema.SObjectField to Id at line 12 column 69)  .  Also I am unsure if the code will deliver the results that I need.  Please help.

Thanks.

Kevin



trigger Update_Account_Owner on Account (before insert, before update) {

    // get list of CURRENTLY ACTIVE Users
        List<User> UserList = [ SELECT ID, Salesman_Number__c
                        FROM User
                        WHERE IsActive = True
                        ORDER BY Salesman_Number__c ];

        // if a salesman number was found, assign the User.ID for the Account
        // if not, assign "00540000002LWA0AAO"
        for( Account Acct : Trigger.new ) {
            if( Acct.Salesman_Number__c == User.Salesman_Number__c) Acct.OwnerID = User.ID;
            else Acct.OwnerID = '00540000002LWA0AAO';
        }
    }

This Trigger that I have written isn't working correctly.  What i am trying to do is get a list of the users that includes ID and Salesman Number.  Then I trying to update the account with the new user ID.  Currently nothing is changing, once I click edit and I don't know why.

thanks.

kevin

trigger AccountOwnerUpdateTrigger on Account (before insert, before update) {

    // this map will contain a list of Accounts with the same salesman #
    Map<String, List<Account>> AccountOwnerMap = new Map<String, List<Account>> ();
    for( Account Acc : trigger.new ) {
        // create the list if it doesn't already exist
        List<Account> AcctList = AccountOwnerMap.get( Acc.Ownerid );
        if( AcctList == null ) {
            AcctList = new List<Account>();
        }
        AcctList.add( Acc );
        AccountOwnerMap.put( Acc.OwnerID, AcctList );
    }

    // get list of CURRENTLY ACTIVE Users using the field Salesman_Number__c
   
    List<User> UserList = [
                        SELECT ID, Salesman_Number__c
                        FROM User
                        WHERE ID IN :AccountOwnerMap.keySet() AND IsActive = True ];
              
                                          
    // go through the User List and get a list of Accounts that have the same salesman
    for( User User : UserList ) {
        List<Account> AcctList = AccountOwnerMap.get( User.ID );

        // assign the Account Owner for each Account in the list
        for( Account Acc : AcctList )
        Acc.Ownerid = User.ID;
        }
}
When we are updating the owner ID with out ERP system, we replicate the ownership change over to SFDC (it is called the salesman number) using informatica.  This - Salesman Number - field is on the user record and on the account record.  What I am trying to do is get a list of the account records where the Salesman number has been changed and update the owner of the account in salesforce with the owner that has the corresponding salesman number on the user record.  (does that make sense?)  Here is my code below, and I think that it is close, but I am getting the following complie error...

Error: Compile Error: Variable does not exist: Account.Owner at line 7 column 55


Here is the trigger I have written.  Please help.

thanks.

Kevin


trigger AccountOwnerUpdateTrigger on Account (before insert, before update) {

    // this map will contain a list of Accounts with the same salesman #
    Map<String, List<Account>> AccountOwnerMap = new Map<String, List<Account>> ();
    for( Account Acc : trigger.new ) {
        // create the list if it doesn't already exist
        List<Account> AcctList = AccountOwnerMap.get( Account.Owner );
        if( AcctList == null ) {
            AcctList = new List<Account>();
        }
        AcctList.add( Acc );
        AccountOwnerMap.put( Account.OwnerID, AcctList );
    }

    // get list of CURRENTLY ACTIVE Sales Targets using the formula field Owner_Salesman_Number__c
    // and order it so that the latest sales target comes last
    List<User> UserList = [
                        SELECT ID, Salesman_Number__c
                        FROM User
                        WHERE ID IN :AccountByOwnerMap.keySet()
                        AND IsActive = TRUE
                        ];

    // go through the Sales Target list and get a list of Sales Order that have the same salesman
    for( User User : UserList ) {
        List<Account> AcctList = AccountOwnerMap.get( User.ID );

        // assign the Sales Target for each Sales Order in the list
        // only if the Sales Target start date is before the Sales Order date
        // that is, if the Sales Target was active at the time of the order
        for( Account Acc : AcctList ) {
            Acc.User = Account.Ownerid;
        }
    }

}
I got this trigger from the help and I am unable to write a test class for it.  My best attempt did not suffice any of the tests.  Could someone please help me.

thank you.


//Reassigns Contact to Account Owner

trigger reassignContact on Contact (before insert, before update) {
   try {

        Set<Id> accountIds = new Set<Id>();
        Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();
  
        // all the accounts whose owner ids to look up
        for ( Contact c : Trigger.new ) {
            if(c.accountId <> null){
             accountIds.add( c.accountId );
            }
        }
      
        // look up each account owner id
        for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
            accountOwnerIdMap.put( acct.id, acct.ownerId );
        }
      
        // change contact owner to its account owner
        for ( Contact c : Trigger.new ) {
            if(c.AccountId <> null){
             c.ownerId = accountOwnerIdMap.get( c.accountId );
            }
        }
    } catch(Exception e) { //catch errors
        System.Debug('reassignContacts failure: '+e.getMessage()); //write error to the debug log
    }

}
My org is having an issue with users creating recurring tasks that are complete.  To resolve this now I am changing all the tasks that are closed in the future back to not started.  Since these are recurring tasks, it is a real pain to undo since I have to do it one at a time with the data loader.

If anyone has any ideas on a trigger, would you please post  them so that I can keep this from happening?

thanks.

kevin
I was thinking that you could have a trigger like this on the account, but it didn't work.  Please help me with it.



/*
    Created by: Kevin Tullos
    Last Update: 22 June 2014 by Kevin Tullos
    Questions?: kevin.tullos@fleetpride.com
*/
trigger deleteOld_Owner_tasks on Account (before update) {
   
        Set<Id> accountIds = new Set<Id>(); //set for holding the Ids of all Accounts that have been assigned to new Owners
        Map<Id, String> oldOwnerIds = new Map<Id, String>(); //map for holding the old account ownerId
        Map<Id, String> newOwnerIds = new Map<Id, String>(); //map for holding the new account ownerId
        Task[] TaskUpdates = new Task[0]; //Task sObject to hold OwnerId updates
       
        for (Account a : Trigger.new) { //for all records
            if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId) {
                oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId); //put the old OwnerId value in a map
                newOwnerIds.put(a.Id, a.OwnerId); //put the new OwnerId value in a map
                accountIds.add(a.Id); //add the Account Id to the set
            }
        }
       
        if (!accountIds.isEmpty()) { //if the accountIds Set is not empty
            for (Account act : [SELECT Id, (SELECT Id, Status, OwnerId FROM Tasks WHERE IsClosed = False) FROM Account WHERE Id in :accountIds]) { //SOQL to get Contacts and Opportunities for updated Accounts
                String newOwnerId = newOwnerIds.get(act.Id); //get the new OwnerId value for the account
                String oldOwnerId = oldOwnerIds.get(act.Id); //get the old OwnerId value for the account
               
                for (Task t : act.Tasks) { //for all tasks
                    if (t.OwnerId == oldOwnerId) { //if the task is assigned to the old account Owner
                    delete t;
                    }
                } 
          
            }
           
        }
  
}
I want to delete all the open tasks that are associated with an account when the account owner is changed. I would post the code that i have written, but none of it is useful.

it can either be batch apex or a trigger.

Thanks.

Kevin

I am trying to write a trigger that updates the account owner with the user who has the cooresponding salesman_ID field.  The account has OwnerID, and Salesman_Number__c, and I need to get the ID from the User Record that has the matching Salesman_Number__c.  I have tried a lot of things and I cannot get them to compile (error:  Error: Compile Error: Illegal assignment from Schema.SObjectField to Id at line 12 column 69)  .  Also I am unsure if the code will deliver the results that I need.  Please help.

Thanks.

Kevin



trigger Update_Account_Owner on Account (before insert, before update) {

    // get list of CURRENTLY ACTIVE Users
        List<User> UserList = [ SELECT ID, Salesman_Number__c
                        FROM User
                        WHERE IsActive = True
                        ORDER BY Salesman_Number__c ];

        // if a salesman number was found, assign the User.ID for the Account
        // if not, assign "00540000002LWA0AAO"
        for( Account Acct : Trigger.new ) {
            if( Acct.Salesman_Number__c == User.Salesman_Number__c) Acct.OwnerID = User.ID;
            else Acct.OwnerID = '00540000002LWA0AAO';
        }
    }

This Trigger that I have written isn't working correctly.  What i am trying to do is get a list of the users that includes ID and Salesman Number.  Then I trying to update the account with the new user ID.  Currently nothing is changing, once I click edit and I don't know why.

thanks.

kevin

trigger AccountOwnerUpdateTrigger on Account (before insert, before update) {

    // this map will contain a list of Accounts with the same salesman #
    Map<String, List<Account>> AccountOwnerMap = new Map<String, List<Account>> ();
    for( Account Acc : trigger.new ) {
        // create the list if it doesn't already exist
        List<Account> AcctList = AccountOwnerMap.get( Acc.Ownerid );
        if( AcctList == null ) {
            AcctList = new List<Account>();
        }
        AcctList.add( Acc );
        AccountOwnerMap.put( Acc.OwnerID, AcctList );
    }

    // get list of CURRENTLY ACTIVE Users using the field Salesman_Number__c
   
    List<User> UserList = [
                        SELECT ID, Salesman_Number__c
                        FROM User
                        WHERE ID IN :AccountOwnerMap.keySet() AND IsActive = True ];
              
                                          
    // go through the User List and get a list of Accounts that have the same salesman
    for( User User : UserList ) {
        List<Account> AcctList = AccountOwnerMap.get( User.ID );

        // assign the Account Owner for each Account in the list
        for( Account Acc : AcctList )
        Acc.Ownerid = User.ID;
        }
}
I got this trigger from the help and I am unable to write a test class for it.  My best attempt did not suffice any of the tests.  Could someone please help me.

thank you.


//Reassigns Contact to Account Owner

trigger reassignContact on Contact (before insert, before update) {
   try {

        Set<Id> accountIds = new Set<Id>();
        Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();
  
        // all the accounts whose owner ids to look up
        for ( Contact c : Trigger.new ) {
            if(c.accountId <> null){
             accountIds.add( c.accountId );
            }
        }
      
        // look up each account owner id
        for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
            accountOwnerIdMap.put( acct.id, acct.ownerId );
        }
      
        // change contact owner to its account owner
        for ( Contact c : Trigger.new ) {
            if(c.AccountId <> null){
             c.ownerId = accountOwnerIdMap.get( c.accountId );
            }
        }
    } catch(Exception e) { //catch errors
        System.Debug('reassignContacts failure: '+e.getMessage()); //write error to the debug log
    }

}
My org is having an issue with users creating recurring tasks that are complete.  To resolve this now I am changing all the tasks that are closed in the future back to not started.  Since these are recurring tasks, it is a real pain to undo since I have to do it one at a time with the data loader.

If anyone has any ideas on a trigger, would you please post  them so that I can keep this from happening?

thanks.

kevin

Hi, I created the following custom fields in Contact object to calculate the average survey socre:

- Sum of Scores (field name: Sum_of_Scores__c)

- # of surveys (field name: Surveys_filled_out__c)

- Ave Score: this is a formula: ROUND(Sum_of_Scores__c / Surveys_filled_out__c,0)

 

However, I'm getting'#Error!' in the Ave Score field when the value of the formula returns 0. I'd like it to show '0' instead.

 

Does anyone know how to work around that? Thanks.

 

 

Hi,
I have been suffering with lack of logic from last couple of days.
i want to write a trigger on account object custom field which will calculate revenue for current fiscal yearYTD based on oppotunity amount.
once the current fiscal yearYTD gets changed again calculations start from next fiscal yearYTD.(that mean whatever value stored for last current fiscal yearYTD need to be override with urrent fiscal yearYTD)
Can somebody help me in figure out the best approach to get current fiscalYTD.
I hope i'm clear with my question.

Thanks in Advance

Thanks&Regards
Lakshman