• Michael Lindsay 4
  • NEWBIE
  • 20 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 4
    Replies
Hi,

I have this trigger that I would like to convert to an apex class and I would like to add some code to the class.  The trigger changes contacts to the account owner id when the account owner is changed.  What I would like to add is something that also changes the contact owner when a contact is created or added to the account.

Here is the trigger I have.

trigger AlignContactownertoAccountOwner on Account (after insert,after update) {
      Set<Id> accountIds = new Set<Id>();
      Map<Id, String> oldOwnerIds = new Map<Id, String>();
      Map<Id, String> newOwnerIds = new Map<Id, String>();
      List<Contact> contactUpdates = new List<Contact>();
      for (Account a : Trigger.new)
      {
         if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId)
         {
            oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId);
            newOwnerIds.put(a.Id, a.OwnerId);
            accountIds.add(a.Id);

         }

      }

        if (!accountIds.isEmpty()) {

         for (Account acc : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account WHERE Id in :accountIds])

            {

            String newOwnerId = newOwnerIds.get(acc.Id);
            String oldOwnerId = oldOwnerIds.get(acc.Id);

            for (Contact c : acc.Contacts)

            {

               if (c.OwnerId == oldOwnerId)
               {

               Contact updatedContact = new Contact(Id = c.Id, OwnerId = newOwnerId);

               contactUpdates.add(updatedContact);

               }

            }

             

            }

       }
            update contactUpdates;

}

Any help would be great.

Thanks,

Michael 
All,

I am trying to create a field set for a custom object and running into a few problem....lack of coding skills being one!

Here is what I have so far.

Controller:
public with sharing class CustomerImplementation {
    public String getCustomerImplementation() {
        return null;
    }
}

Visual Force page:

<apex:page standardController="CustomerImplementation__c">
    <apex:repeat value="{!$ObjectType.CustomerImplementation__c.FieldSets.Milestone1}" var="f">
        <apex:outputText value="{!CustomerImplementation__c[f]}" /><br/>
    </apex:repeat>
</apex:page>

When I load the page I don't get any of the fields that page it empty.  I believe my problem is the controller.

Thanks for the help!

Michael
All,

I am trying to create my first VF page and running into an error.

So far I just have the default VF page created but trying to add a field from my custom object and getting an error.  I want to add fields from the custom object called Customer Implementation.

This is what I have

<apex:page>
  <!-- Begin Default Content REMOVE THIS -->
  <h1>Congratulations</h1>
  {!CustomerImplementation__c.AccountName__c}
  This is your new Page
  <!-- End Default Content REMOVE THIS -->
</apex:page>

This is the error: Error: Unknown property 'CustomerImplementation__c.AccountName__c' referenced in CIGMilestone1

 
All,

We are trying to track the cumulative time an opportunity spends in each stage including the current stage.  I understand that you can get to most of this data using the opportunity stage history report but it is lacking in the current stage data and the math is not always correct!  I found a posting at the following URL that looks close to what I what but even after creating it just as listed I am getting compile errors.

Here is the URL:  http://salesforce.stackexchange.com/questions/76619/calculate-duration-spent-in-custom-field-status

Here are the things I did.
  1. Add field to Opportunity Object called "Last Status Change" (Date Time)
  2. Add formula field (name it "Time in current Status") to Opportunity object with the following logic: IF(ISBLANK(Last_Status_Change__c), NOW() - CreatedDate, NOW()- Last_Status_Change__c)
  3. Create a new Custom Object called "Opportunity Status Change"
  4. Add the following fields to this new object: Opportunity (Lookup to Opportunity) Status (Text) Time in Status (Number)

Trigger
 trigger trgOppTrackStatusTime on Opportunity (after update)  {
        Map<string, OpportunityStatusChange__c > metricByKey = new Map<string, OpportunityStatusChange__c >();

        for(OpportunityStatusChange__c m : [SELECT id, Status__c, Opportunity__c, Time_In_Status__c FROM OpportunityStatusChange__c WHERE Opportunity__c IN :trigger.newMap.keyset()])
        {
            metricByKey.put(string.valueOf(m.Opportunity__c)+string.valueOf(m.Status__c),m);
        }

        List<Opportunity_Status_Change__c> metrics = new List<Opportunity_Status_Change__c>();
        string key = '';
        Decimal hours;
        for(Opportunity o : trigger.new)
        {
            String oldStage = trigger.oldMap.get(o.Id).Status__c;
            String newStage = o.Operations_Status__c;
            Boolean stageChanged = oldStage != newStage;

            if(stageChanged)
            {
                hours = o.Time_in_current_Status__c;

                Opportunity_Status_Change__c temp;
                key = string.valueOf(o.Id)+string.valueOf(trigger.oldMap.get(o.Id).Status__c);
                if(metricByKey.containsKey(key))
                {
                    temp = metricByKey.get(key);
                    temp.Time_In_Status__c += hours;
                }
                else
                {
                    temp = new Opportunity_Status_Change__c(
Name=trigger.oldMap.get(o.Id).Status__c,
                                                Status__c = trigger.oldMap.get(o.Id).Status__c,
                                                Opportunity__c = o.Id,
                                                Time_In_Status__c = hours
                                             );
                }

                metrics.add(temp);
            }
        }

        if(metrics.size() > 0)
            upsert metrics; 


Any help would be great!

Thanks,

Michael
Hi,

I have this trigger that I would like to convert to an apex class and I would like to add some code to the class.  The trigger changes contacts to the account owner id when the account owner is changed.  What I would like to add is something that also changes the contact owner when a contact is created or added to the account.

Here is the trigger I have.

trigger AlignContactownertoAccountOwner on Account (after insert,after update) {
      Set<Id> accountIds = new Set<Id>();
      Map<Id, String> oldOwnerIds = new Map<Id, String>();
      Map<Id, String> newOwnerIds = new Map<Id, String>();
      List<Contact> contactUpdates = new List<Contact>();
      for (Account a : Trigger.new)
      {
         if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId)
         {
            oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId);
            newOwnerIds.put(a.Id, a.OwnerId);
            accountIds.add(a.Id);

         }

      }

        if (!accountIds.isEmpty()) {

         for (Account acc : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account WHERE Id in :accountIds])

            {

            String newOwnerId = newOwnerIds.get(acc.Id);
            String oldOwnerId = oldOwnerIds.get(acc.Id);

            for (Contact c : acc.Contacts)

            {

               if (c.OwnerId == oldOwnerId)
               {

               Contact updatedContact = new Contact(Id = c.Id, OwnerId = newOwnerId);

               contactUpdates.add(updatedContact);

               }

            }

             

            }

       }
            update contactUpdates;

}

Any help would be great.

Thanks,

Michael 
All,

I am trying to create a field set for a custom object and running into a few problem....lack of coding skills being one!

Here is what I have so far.

Controller:
public with sharing class CustomerImplementation {
    public String getCustomerImplementation() {
        return null;
    }
}

Visual Force page:

<apex:page standardController="CustomerImplementation__c">
    <apex:repeat value="{!$ObjectType.CustomerImplementation__c.FieldSets.Milestone1}" var="f">
        <apex:outputText value="{!CustomerImplementation__c[f]}" /><br/>
    </apex:repeat>
</apex:page>

When I load the page I don't get any of the fields that page it empty.  I believe my problem is the controller.

Thanks for the help!

Michael
All,

We are trying to track the cumulative time an opportunity spends in each stage including the current stage.  I understand that you can get to most of this data using the opportunity stage history report but it is lacking in the current stage data and the math is not always correct!  I found a posting at the following URL that looks close to what I what but even after creating it just as listed I am getting compile errors.

Here is the URL:  http://salesforce.stackexchange.com/questions/76619/calculate-duration-spent-in-custom-field-status

Here are the things I did.
  1. Add field to Opportunity Object called "Last Status Change" (Date Time)
  2. Add formula field (name it "Time in current Status") to Opportunity object with the following logic: IF(ISBLANK(Last_Status_Change__c), NOW() - CreatedDate, NOW()- Last_Status_Change__c)
  3. Create a new Custom Object called "Opportunity Status Change"
  4. Add the following fields to this new object: Opportunity (Lookup to Opportunity) Status (Text) Time in Status (Number)

Trigger
 trigger trgOppTrackStatusTime on Opportunity (after update)  {
        Map<string, OpportunityStatusChange__c > metricByKey = new Map<string, OpportunityStatusChange__c >();

        for(OpportunityStatusChange__c m : [SELECT id, Status__c, Opportunity__c, Time_In_Status__c FROM OpportunityStatusChange__c WHERE Opportunity__c IN :trigger.newMap.keyset()])
        {
            metricByKey.put(string.valueOf(m.Opportunity__c)+string.valueOf(m.Status__c),m);
        }

        List<Opportunity_Status_Change__c> metrics = new List<Opportunity_Status_Change__c>();
        string key = '';
        Decimal hours;
        for(Opportunity o : trigger.new)
        {
            String oldStage = trigger.oldMap.get(o.Id).Status__c;
            String newStage = o.Operations_Status__c;
            Boolean stageChanged = oldStage != newStage;

            if(stageChanged)
            {
                hours = o.Time_in_current_Status__c;

                Opportunity_Status_Change__c temp;
                key = string.valueOf(o.Id)+string.valueOf(trigger.oldMap.get(o.Id).Status__c);
                if(metricByKey.containsKey(key))
                {
                    temp = metricByKey.get(key);
                    temp.Time_In_Status__c += hours;
                }
                else
                {
                    temp = new Opportunity_Status_Change__c(
Name=trigger.oldMap.get(o.Id).Status__c,
                                                Status__c = trigger.oldMap.get(o.Id).Status__c,
                                                Opportunity__c = o.Id,
                                                Time_In_Status__c = hours
                                             );
                }

                metrics.add(temp);
            }
        }

        if(metrics.size() > 0)
            upsert metrics; 


Any help would be great!

Thanks,

Michael