• CBradley1985
  • NEWBIE
  • 10 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 5
    Replies
Hi All,
I am having a bit of trouble that I'm hoping you can help me with...I have a Class that clones our opportunities and one of the things I'd like to add to it is have it set the Account Owner as the Opportunity Owner during cloning.
Attached is my Class that I currently have...any help would be appreciated!
Thanks!
Chris
global with sharing class CloneOpportunity 
{
    public static final String PRICEBOOK_NAME = 'CTM Price Book';
    
    WebService static String clone(String oppId)
    {
        final String ACTIVITY_HISTORY_SUBJECT = 'Products Missing from ' + PRICEBOOK_NAME;
        final String COMMONS = 'The following products are not associated with the ' + PRICEBOOK_NAME;
        
        // Clone opportunity
        String pricebook2Id = [select Id from Pricebook2 where Name = :PRICEBOOK_NAME limit 1].Id;
        RecordType recordType = [select Id from RecordType where SobjectType = 'Opportunity' and Name = 'Renewal Opportunity' limit 1];
        String oppQuery = getOpportunityQuery(oppId);
        Opportunity sourceOpp = (Opportunity)Database.query(oppQuery);
        Opportunity newOpportunity = sourceOpp.clone(false, true, false, false);
        newOpportunity.RecordTypeId = recordType.Id;
        newOpportunity.Pricebook2Id = pricebook2Id;
        newOpportunity.Contract_Number__c = '';
        newOpportunity.Record_Type__c = 'Quote';
        newOpportunity.StageName = 'Needs Analysis';
        newOpportunity.Thank_You_Card_Email__c = '';
        newOpportunity.Thank_You_Card_Contact__c = null;
        newOpportunity.Purchase_Order__c = '0' ;
        newOpportunity.GreatPlainsCustomerID__c ='';
        newOpportunity.Reason_for_Lost_Opportunity__c = null;
        newOpportunity.Smooth_Billing__c = FALSE;
        newOpportunity.Smooth_Revenue__c = FALSE;
        newOpportunity.Thank_You_Card_Template__c = null;
        newOpportunity.Type = 'Renewal';
        newOpportunity.OwnerId = 'Account.OwnerId';
        if(newOpportunity.Renewal_Cycle_Follow_Up_Date__c != null)
        {
            newOpportunity.Renewal_Cycle_Follow_Up_Date__c = newOpportunity.Renewal_Cycle_Follow_Up_Date__c.addYears(1);
        }
        if(newOpportunity.Contract_Start_Date__c != null)
        {
            newOpportunity.Contract_Start_Date__c = newOpportunity.Contract_Start_Date__c.addYears(1);
        }
        if(newOpportunity.Contract_End_Date__c != null)
        {
            newOpportunity.Contract_End_Date__c = newOpportunity.Contract_End_Date__c.addYears(1);
        }
        if(newOpportunity.Distribution_Dates_Start_Date__c != null)
        {
            newOpportunity.Distribution_Dates_Start_Date__c = newOpportunity.Distribution_Dates_Start_Date__c.addYears(1);
        }
        if(newOpportunity.Distribution_Dates_End_Date__c != null)
        {
            newOpportunity.Distribution_Dates_End_Date__c = newOpportunity.Distribution_Dates_End_Date__c.addYears(1);
        }
        if(newOpportunity.Bill_Dates_Start_Date__c != null)
        {
            newOpportunity.Bill_Dates_Start_Date__c = newOpportunity.Bill_Dates_Start_Date__c.addYears(1);
        }
        if(newOpportunity.Bill_Dates_End_Date__c != null)
        {
            newOpportunity.Bill_Dates_End_Date__c = newOpportunity.Bill_Dates_End_Date__c.addYears(1);
        }
        if(newOpportunity.CloseDate != null)
        {
            newOpportunity.CloseDate = newOpportunity.CloseDate.addYears(1);
        }
        insert newOpportunity;
        
        // Clone opportunity products
        Set<String> productIds = getProductIdsFromLineItem(oppId);
        Map<String, String> ctmProductIds = new Map<String, String>();
        for(PricebookEntry entry : [select Id, Product2Id from PricebookEntry where Pricebook2Id = :pricebook2Id and Product2Id in :productIds and CurrencyIsoCode = :newOpportunity.CurrencyIsoCode])
        {
            ctmProductIds.put(entry.Product2Id, entry.Id);
        }
        
        String lineItemQuery = getOpportunityLineItemQuery(oppId);
        OpportunityLineItem[] sourceLineItems = (List<OpportunityLineItem>)Database.query(lineItemQuery);
        List<OpportunityLineItem> newLineItems = new List<OpportunityLineItem>();
        List<String> lineItemsInActivity = new List<String>();
        String productNames = '';
        
        for(OpportunityLineItem item : sourceLineItems)
        {
            if(ctmProductIds.keySet().contains(item.PricebookEntry.Product2Id))
            {
                OpportunityLineItem newLineItem = item.clone(false, true, false, false);
                newLineItem.OpportunityId = newOpportunity.Id;
                newLineItem.PricebookEntryId = ctmProductIds.get(item.PricebookEntry.Product2Id);
                newLineItem.Quantity = (1);
                if(newLineItem.Billing_Start_Date__c != null)
                {
                    newLineItem.Billing_Start_Date__c = newLineItem.Billing_Start_Date__c.addYears(1);
                }
                if(newLineItem.Billing_End_Date__c != null)
                {
                    newLineItem.Billing_End_Date__c = newLineItem.Billing_End_Date__c.addYears(1);
                }
                if(newLineItem.Contract_Start_Date__c != null)
                {
                    newLineItem.Contract_Start_Date__c = newLineItem.Contract_Start_Date__c.addYears(1);
                }
                if(newLineItem.Contract_End_Date__c != null)
                {
                    newLineItem.Contract_End_Date__c = newLineItem.Contract_End_Date__c.addYears(1);
                }
                newLineItems.add(newLineItem);
            }
            else
            {
                String productName = ' • ' + item.PricebookEntry.Product2.Name + '\r\n';
                lineItemsInActivity.add(productName);
                productNames += productName;
            }
        }
        if(newLineItems.size() > 0)
        {
            insert newLineItems;
        }
        
        if(lineItemsInActivity.size() > 0)
        {
            Task task = new Task();
            task.WhatId = newOpportunity.Id;
            task.Subject = ACTIVITY_HISTORY_SUBJECT;
            task.ActivityDate = Date.today().addDays(-1);
            task.Status = 'Completed';
            task.Type= 'SF Message';
            task.Description = COMMONS + ':\r\n';
            for(String lineItemName : lineItemsInActivity)
            {
                task.Description += lineItemName;
            }
            insert task;
        }
        return productNames + '^o^' + newOpportunity.Id;
    }
    
    /**
     * Get the opportunity query.
     */
    private static String getOpportunityQuery(String oppId)
    {
        String result = 'select ';
        for(String fieldApiName : Schema.SObjectType.Opportunity.fields.getMap().keySet())
        {
            result += fieldApiName + ',';
        }
        result = result.substring(0, result.length() - 1) + ' from Opportunity where Id = \'' + oppId + '\'';
        return result;
    }
    
    /**
     * Get the opportunity product query.
     */
    private static String getOpportunityLineItemQuery(String oppId)
    {
        String result = 'select ';
        for(String fieldApiName : Schema.SObjectType.OpportunityLineItem.fields.getMap().keySet())
        {
            result += fieldApiName + ',';
        }
        result = result.replace('totalprice,', '');
        result = result + 'PricebookEntry.Product2Id, PricebookEntry.Product2.Name from OpportunityLineItem where OpportunityId = \'' + oppId + '\'';
        return result;
    }
    /**
     * Get the product IDs in opportunity.
     */
    private static Set<String> getProductIdsFromLineItem(String oppId)
    {
        Set<String> result = new Set<String>();
        for(OpportunityLineItem item : [select PricebookEntry.Product2Id from OpportunityLineItem where OpportunityId = :oppId])
        {
            result.add(item.PricebookEntry.Product2Id);
        }
        return result;
    }


 

Hello,

 

I am extremely new to APEX so please don't laugh at me...

 

I have a piece of code that we paid for and the API Version is 23.0 and I need to copy this code and create a new class so that I can use a different button that we have to run the seperate code after I sweek a couple things. (Mainly just values on the new opportunity.)

 

And with version 28 or higher I get these errors. I have tried to find a place to get the Force.com IDE v.27 or earlier and haven't had much luck.

 

1. If you could point me to a place that I can get this earlier version so I can use eclipse to impliment into the sandbox with the IDE plug-in that would be fantastic.

 

2. Or if you want to take a peek at the piece of code and tell me what needs to be changed to make it work I'd love to learn. (Trust me, I'm not one to just say give me the answer...I want to learn it!)

 

This seems to be the piece that's getting me into trouble. Again, I appreciate ANY help.

 

Thanks!


Chris

 

    @isTest

    public static void testCloneOpportunity()

    {

        OpportunityLineItem line = [select OpportunityId from OpportunityLineItem limit 1];

        CloneOpportunity.clone(line.OpportunityId);

    }

Hi All,

 

I'm having a bit of trouble with my vf page and wondering if anyone has experienced the same. My page worked yesterday just find and today when I wanted to make a slight modification to it in my production database I can't even do it. 

 

Normally I would just open it up and make my change but now when I try it just says "Processing...." at the top. I don't even have the save button anymore.

 

Is anyone else having the same issue? I have Enterprise Edition.

 

Any feedback would be great.

 

Chris

Hello,

 

I am very very new to Apex...And need some help so any help would be greatly appreciated!

 

I am trying to figure out how to write a trigger to do the job of a WFR. I would use a WFR but it won't update a field on a different object. 

 

What I'm trying to accomplish is when Inventory Reports__c on the Account record is equal to "None" it will update field Reports__c on my custom object "Inventory" to "Deactivate". 

 

Also, a point in the direction to help me on my learning path would be a great help!

 

Thank you,

 

Chris

Hi All,
I am having a bit of trouble that I'm hoping you can help me with...I have a Class that clones our opportunities and one of the things I'd like to add to it is have it set the Account Owner as the Opportunity Owner during cloning.
Attached is my Class that I currently have...any help would be appreciated!
Thanks!
Chris
global with sharing class CloneOpportunity 
{
    public static final String PRICEBOOK_NAME = 'CTM Price Book';
    
    WebService static String clone(String oppId)
    {
        final String ACTIVITY_HISTORY_SUBJECT = 'Products Missing from ' + PRICEBOOK_NAME;
        final String COMMONS = 'The following products are not associated with the ' + PRICEBOOK_NAME;
        
        // Clone opportunity
        String pricebook2Id = [select Id from Pricebook2 where Name = :PRICEBOOK_NAME limit 1].Id;
        RecordType recordType = [select Id from RecordType where SobjectType = 'Opportunity' and Name = 'Renewal Opportunity' limit 1];
        String oppQuery = getOpportunityQuery(oppId);
        Opportunity sourceOpp = (Opportunity)Database.query(oppQuery);
        Opportunity newOpportunity = sourceOpp.clone(false, true, false, false);
        newOpportunity.RecordTypeId = recordType.Id;
        newOpportunity.Pricebook2Id = pricebook2Id;
        newOpportunity.Contract_Number__c = '';
        newOpportunity.Record_Type__c = 'Quote';
        newOpportunity.StageName = 'Needs Analysis';
        newOpportunity.Thank_You_Card_Email__c = '';
        newOpportunity.Thank_You_Card_Contact__c = null;
        newOpportunity.Purchase_Order__c = '0' ;
        newOpportunity.GreatPlainsCustomerID__c ='';
        newOpportunity.Reason_for_Lost_Opportunity__c = null;
        newOpportunity.Smooth_Billing__c = FALSE;
        newOpportunity.Smooth_Revenue__c = FALSE;
        newOpportunity.Thank_You_Card_Template__c = null;
        newOpportunity.Type = 'Renewal';
        newOpportunity.OwnerId = 'Account.OwnerId';
        if(newOpportunity.Renewal_Cycle_Follow_Up_Date__c != null)
        {
            newOpportunity.Renewal_Cycle_Follow_Up_Date__c = newOpportunity.Renewal_Cycle_Follow_Up_Date__c.addYears(1);
        }
        if(newOpportunity.Contract_Start_Date__c != null)
        {
            newOpportunity.Contract_Start_Date__c = newOpportunity.Contract_Start_Date__c.addYears(1);
        }
        if(newOpportunity.Contract_End_Date__c != null)
        {
            newOpportunity.Contract_End_Date__c = newOpportunity.Contract_End_Date__c.addYears(1);
        }
        if(newOpportunity.Distribution_Dates_Start_Date__c != null)
        {
            newOpportunity.Distribution_Dates_Start_Date__c = newOpportunity.Distribution_Dates_Start_Date__c.addYears(1);
        }
        if(newOpportunity.Distribution_Dates_End_Date__c != null)
        {
            newOpportunity.Distribution_Dates_End_Date__c = newOpportunity.Distribution_Dates_End_Date__c.addYears(1);
        }
        if(newOpportunity.Bill_Dates_Start_Date__c != null)
        {
            newOpportunity.Bill_Dates_Start_Date__c = newOpportunity.Bill_Dates_Start_Date__c.addYears(1);
        }
        if(newOpportunity.Bill_Dates_End_Date__c != null)
        {
            newOpportunity.Bill_Dates_End_Date__c = newOpportunity.Bill_Dates_End_Date__c.addYears(1);
        }
        if(newOpportunity.CloseDate != null)
        {
            newOpportunity.CloseDate = newOpportunity.CloseDate.addYears(1);
        }
        insert newOpportunity;
        
        // Clone opportunity products
        Set<String> productIds = getProductIdsFromLineItem(oppId);
        Map<String, String> ctmProductIds = new Map<String, String>();
        for(PricebookEntry entry : [select Id, Product2Id from PricebookEntry where Pricebook2Id = :pricebook2Id and Product2Id in :productIds and CurrencyIsoCode = :newOpportunity.CurrencyIsoCode])
        {
            ctmProductIds.put(entry.Product2Id, entry.Id);
        }
        
        String lineItemQuery = getOpportunityLineItemQuery(oppId);
        OpportunityLineItem[] sourceLineItems = (List<OpportunityLineItem>)Database.query(lineItemQuery);
        List<OpportunityLineItem> newLineItems = new List<OpportunityLineItem>();
        List<String> lineItemsInActivity = new List<String>();
        String productNames = '';
        
        for(OpportunityLineItem item : sourceLineItems)
        {
            if(ctmProductIds.keySet().contains(item.PricebookEntry.Product2Id))
            {
                OpportunityLineItem newLineItem = item.clone(false, true, false, false);
                newLineItem.OpportunityId = newOpportunity.Id;
                newLineItem.PricebookEntryId = ctmProductIds.get(item.PricebookEntry.Product2Id);
                newLineItem.Quantity = (1);
                if(newLineItem.Billing_Start_Date__c != null)
                {
                    newLineItem.Billing_Start_Date__c = newLineItem.Billing_Start_Date__c.addYears(1);
                }
                if(newLineItem.Billing_End_Date__c != null)
                {
                    newLineItem.Billing_End_Date__c = newLineItem.Billing_End_Date__c.addYears(1);
                }
                if(newLineItem.Contract_Start_Date__c != null)
                {
                    newLineItem.Contract_Start_Date__c = newLineItem.Contract_Start_Date__c.addYears(1);
                }
                if(newLineItem.Contract_End_Date__c != null)
                {
                    newLineItem.Contract_End_Date__c = newLineItem.Contract_End_Date__c.addYears(1);
                }
                newLineItems.add(newLineItem);
            }
            else
            {
                String productName = ' • ' + item.PricebookEntry.Product2.Name + '\r\n';
                lineItemsInActivity.add(productName);
                productNames += productName;
            }
        }
        if(newLineItems.size() > 0)
        {
            insert newLineItems;
        }
        
        if(lineItemsInActivity.size() > 0)
        {
            Task task = new Task();
            task.WhatId = newOpportunity.Id;
            task.Subject = ACTIVITY_HISTORY_SUBJECT;
            task.ActivityDate = Date.today().addDays(-1);
            task.Status = 'Completed';
            task.Type= 'SF Message';
            task.Description = COMMONS + ':\r\n';
            for(String lineItemName : lineItemsInActivity)
            {
                task.Description += lineItemName;
            }
            insert task;
        }
        return productNames + '^o^' + newOpportunity.Id;
    }
    
    /**
     * Get the opportunity query.
     */
    private static String getOpportunityQuery(String oppId)
    {
        String result = 'select ';
        for(String fieldApiName : Schema.SObjectType.Opportunity.fields.getMap().keySet())
        {
            result += fieldApiName + ',';
        }
        result = result.substring(0, result.length() - 1) + ' from Opportunity where Id = \'' + oppId + '\'';
        return result;
    }
    
    /**
     * Get the opportunity product query.
     */
    private static String getOpportunityLineItemQuery(String oppId)
    {
        String result = 'select ';
        for(String fieldApiName : Schema.SObjectType.OpportunityLineItem.fields.getMap().keySet())
        {
            result += fieldApiName + ',';
        }
        result = result.replace('totalprice,', '');
        result = result + 'PricebookEntry.Product2Id, PricebookEntry.Product2.Name from OpportunityLineItem where OpportunityId = \'' + oppId + '\'';
        return result;
    }
    /**
     * Get the product IDs in opportunity.
     */
    private static Set<String> getProductIdsFromLineItem(String oppId)
    {
        Set<String> result = new Set<String>();
        for(OpportunityLineItem item : [select PricebookEntry.Product2Id from OpportunityLineItem where OpportunityId = :oppId])
        {
            result.add(item.PricebookEntry.Product2Id);
        }
        return result;
    }


 

Hello,

 

I am extremely new to APEX so please don't laugh at me...

 

I have a piece of code that we paid for and the API Version is 23.0 and I need to copy this code and create a new class so that I can use a different button that we have to run the seperate code after I sweek a couple things. (Mainly just values on the new opportunity.)

 

And with version 28 or higher I get these errors. I have tried to find a place to get the Force.com IDE v.27 or earlier and haven't had much luck.

 

1. If you could point me to a place that I can get this earlier version so I can use eclipse to impliment into the sandbox with the IDE plug-in that would be fantastic.

 

2. Or if you want to take a peek at the piece of code and tell me what needs to be changed to make it work I'd love to learn. (Trust me, I'm not one to just say give me the answer...I want to learn it!)

 

This seems to be the piece that's getting me into trouble. Again, I appreciate ANY help.

 

Thanks!


Chris

 

    @isTest

    public static void testCloneOpportunity()

    {

        OpportunityLineItem line = [select OpportunityId from OpportunityLineItem limit 1];

        CloneOpportunity.clone(line.OpportunityId);

    }

Hi All,

 

I'm having a bit of trouble with my vf page and wondering if anyone has experienced the same. My page worked yesterday just find and today when I wanted to make a slight modification to it in my production database I can't even do it. 

 

Normally I would just open it up and make my change but now when I try it just says "Processing...." at the top. I don't even have the save button anymore.

 

Is anyone else having the same issue? I have Enterprise Edition.

 

Any feedback would be great.

 

Chris