• Bard09
  • NEWBIE
  • 50 Points
  • Member since 2008

  • Chatter
    Feed
  • 2
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 12
    Questions
  • 27
    Replies

Hey all,

 

First time using Batch Apex to do a large bulk update.  I'm hoping to be able to run this monthly as a kind of cron job to keep a static financial field synced up each month.

 

global class MonthlyOpportunityAAV implements Database.Batchable <sObject>, Database.Stateful {

	public List <Opportunity> updateOpportunityList;

	global database.querylocator start(Database.BatchableContext BC) {
    
    	updateOpportunityList = new List <Opportunity> ();
    	
    	return Database.getQueryLocator('SELECT Id, Annualized_Value__c, Monthly_AV__c, Monthly_AV_Match__c FROM Opportunity WHERE Monthly_AV_Match__c=\'False\'');
    	
    }

    global void execute(Database.BatchableContext BC, List <sObject> scope) {

		for (sObject s : scope) {
    		
    		Opportunity o = (Opportunity)s;
    		
    		if (o.Annualized_Value__c != o.Monthly_AV__c) {
    		
	    		o.Monthly_AV__c = o.Annualized_Value__c;
	    		updateOpportunityList.add(o);
	    		
    		}
    		
    	}
    	
    	if (!updateOpportunityList.isEmpty()) {
    		
    		try {
    			update updateOpportunityList;
	    	}
	    	catch (DmlException e) {
	    		for (Integer i = 0; i < e.getNumDml(); i++) {
			    	System.debug('DMLERROR for ' + e.getDmlId(i) + ': ' + e.getDmlMessage(i));
			    }
	    	}
    		
    	}
    	

    }

}

 

I've kicked off this Batch Apex a few times, and it appears to work for a few hundred Opportunities, but then it gets hung up on a bunch of Opps that fail due to validation rules.  Here's an example of one of those rules:

 

IF(ISCHANGED(Monthly_AV__c),

FALSE,

BlahBlahBlah

)

 

As you can see, I've tried to exempt the rule from being executed if it detects an update to the Monthly AV field.  Unfortunately, this doesn't seem to be working in my code.  When I've reviewed some of the errors I get during the Batch Apex update, here's what I see.  (this is an example)

 

10:47:32.818 (186818515000)|EXCEPTION_THROWN|[33]|System.DmlException: Update failed. First exception on row 5 with id 0065000000FLPqjAAH; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, A Payment Type ID is required for this Payment Type.: [Payment_Type_Id__c]
10:47:32.989 (186989044000)|SYSTEM_METHOD_ENTRY|[36]|Exception.getNumDml()
10:47:32.989 (186989074000)|SYSTEM_METHOD_EXIT|[36]|Exception.getNumDml()
10:47:32.989 (186989103000)|SYSTEM_METHOD_ENTRY|[37]|Exception.getDmlId(Integer)
10:47:32.989 (186989130000)|SYSTEM_METHOD_EXIT|[37]|Exception.getDmlId(Integer)
10:47:32.989 (186989172000)|SYSTEM_METHOD_ENTRY|[37]|Exception.getDmlMessage(Integer)
10:47:32.989 (186989189000)|SYSTEM_METHOD_EXIT|[37]|Exception.getDmlMessage(Integer)
10:47:32.989 (186989211000)|SYSTEM_METHOD_ENTRY|[37]|System.debug(ANY)
10:47:32.989 (186989221000)|USER_DEBUG|[37]|DEBUG|DMLERROR for 0065000000FLPqjAAH: A Payment Type ID is required for this Payment Type.
10:47:32.989 (186989228000)|SYSTEM_METHOD_EXIT|[37]|System.debug(ANY)
10:47:32.989 (186989238000)|SYSTEM_METHOD_ENTRY|[36]|Exception.getNumDml()
10:47:32.989 (186989248000)|SYSTEM_METHOD_EXIT|[36]|Exception.getNumDml()
10:47:32.989 (186989259000)|SYSTEM_METHOD_ENTRY|[37]|Exception.getDmlId(Integer)
10:47:32.989 (186989270000)|SYSTEM_METHOD_EXIT|[37]|Exception.getDmlId(Integer)
10:47:32.989 (186989281000)|SYSTEM_METHOD_ENTRY|[37]|Exception.getDmlMessage(Integer)
10:47:32.989 (186989291000)|SYSTEM_METHOD_EXIT|[37]|Exception.getDmlMessage(Integer)
10:47:32.989 (186989301000)|SYSTEM_METHOD_ENTRY|[37]|System.debug(ANY)
10:47:32.989 (186989306000)|USER_DEBUG|[37]|DEBUG|DMLERROR for 0065000000FLQ0oAAH: A Payment Type ID is required for this Payment Type.

 

Can anyone help me out with what's happening here?  Am I missing something obvious?  Order of execution issue?  Exception-handling issue?

 

Kind of at a loss :\

Hey elite devs!

 

I've used wrapper classes before, but I have to say I'm absolutely stumped why I'm having problems with a wrapper class I've implemented recently for a Visualforce page.  I'd really appreciate another set of eyes on this issue.

 

The class displays a list of OpportunityContactRoles attached to an Opportunity Id in the URL, and displays them with checkboxes next to them for user-selection.

 

Stripped down class:

 

public class FulfillmentEmailController {
	
    public List <utilityContact> opportunityContactRoleList {get; set;}
    
    //Returning a list of Contact Roles related to this Opp
    public List <utilityContact> getRelevantOpportunityContactRoles() { 
        
        if (opportunityContactRoleList == null){
        	
           opportunityContactRoleList = new List <utilityContact> ();
        
           for (OpportunityContactRole eachOpportunityContactRole : [SELECT Id, ContactId, Role, Contact.Name, Contact.Email FROM OpportunityContactRole WHERE OpportunityId = :ApexPages.currentPage().getParameters().get('oppId')]){
                	
           	opportunityContactRoleList.add(new utilityContact(eachOpportunityContactRole));
                
            }
            
        }
        
        return opportunityContactRoleList;
        
    }
	
    public class utilityContact {
        
        public OpportunityContactRole oppCon {get; set;}
        public Boolean selected {get; set;}
        
        public utilityContact(OpportunityContactRole thisOpportunityContactRole) {
            oppCon = thisOpportunityContactRole;
            selected = false;
        }
        
    }

}

 

Visualforce example:

 

<apex:page controller="FulfillmentEmailController" tabStyle="Opportunity">
    <apex:form >
    	<apex:pageBlock title="Select Recipients">
            <p>Please check the contacts you would like to include.</p>
            <br />
            <apex:pageBlockTable value="{!RelevantOpportunityContactRoles}" var="c">
	            <apex:column>
	            	<apex:inputCheckbox value="{!c.selected}" />
	            </apex:column>
	            <apex:column value="{!c.oppCon.Contact.Name}"/>
	            <apex:column value="{!c.oppCon.Contact.Email}"/>
	            <apex:column value="{!c.oppCon.Role}"/>
        	</apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

I should make it clear that I've implemented code EXACTLY like this in another file-- just using different field names and such.  It works without a hitch.

 

However, when I try to run this, I get the following error:

 

     Invalid field selected for SObject Contact

     (or if I comment out the "selected" field....) Invalid field oppCon for SObject Contact

 

I've done some debugging on this, and it's clear the utility class is populating correctly.  If I change the Visualforce columns to simply {!c}, here's an example of what is displayed:

 

utilityContact:[oppCon=OpportunityContactRole:{Role=District Site Coordinator, ContactId=0035000000W0gGcAAJ, Id=00K50000009YJtFEAW}, selected=false]

 

This shows me that the wrapper class is working fine, and that it's correctly populating the fields I'm trying to query.

 

So why am I getting this error?!  Why "Contact" and not OpportunityContactRole?  Why does a different Visualforce page where I use this exact same logic (also querying OpportuntiyContactRoles) work, but this page doesn't?

 

Help me, elite devs!

I've been asked by my organization to send a notification e-mail when Opportunity Contact Roles are deleted on Closed Opportunities.

 

However, I'm 100% stymied trying to find a solution for this problem.

 

Here's what I've tried:

 

1. Apex code.  Unfortunately, OpportunityContactRole is not allowed for triggers!  So that's out.

2. Creating a Roll Up Summary field on Opportunities to roll up the total # of Contact Roles.  Unfortunately, it's not possible to roll up Contact Roles to Opportunites!

3. Creating a workflow rule.  Two problems here: first, Contact Roles aren't able to be included in Workflow rules.  Two, there is no known way to detect "isDeleted" for Workflow purposes (https://sites.secure.force.com/success/ideaView?c=09a30000000D9xtAAC&id=087300000007DvLAAU&mc=0)

 

Does anyone have any ideas-- no matter how fanciful or obtuse, that might help me implement this e-mail notification?

 

Trying to find a solution to this is driving me CRAZY!

 

Does anyone have any tips or examples for how to write test code for components with custom controllers?  I've written a few myself, but am unable to find any documentation for how to instantiate a component for test purposes.

 

If anyone could point me to an example (or, better yet, post some test code you've written yourself!) that would be amazing.

 

I'm sure this could help others as well.  Google has been absolutely useless on the topic.

In the Winter' 11 update, an Attachments section was added for Activities/Events, which was a longstanding pain point for our organization.

 

However, we use Visualforce to customize our Events page, and need to manually call each Related List.

 

However "NotesAndAttachments" doesn't work for the RelatedList name, and neither does "Attachments".

 

Does anyone know what the RelatedList name is for the Event-specific Attachments section in Visualforce?

Hey all,

 

Is there a way to display previous change data for fields on a Visualforce e-mail template, similar to Apex's trigger.old?

 

Eg "The Opportunity name has changed.  Old value: XYZ.  New Value: ABC".

 

I know I can 100% do this by coding the e-mail template in Apex-- but I really would prefer to write these sorts of e-mails with Visualforce if at all possible.

 

If anyone knows anything, it would be MUCH appreciated.

 

Thanks!

 

Good day All, 

 

May i know how can i get the salesforce site URL ? say if we are in sandbox , it show : https://cs2.salesforce.com

 

if production , it show me https://na7.salesforce.com ?

 

I'm using unlimited version.

 

Thank you !

Good day, 

 

I try using following code and i get null at all ? can someone please let me know why do i miss out to get such info ?

 

 

System.debug('domain string : ' +Site.getDomain());

System.debug('getCustomWebAddress string : ' +Site.getCustomWebAddress());

System.debug('getCurrentSiteUrl String :' +Site.getCurrentSiteUrl());

System.debug('Sub domain String :' +Site.Subdomain);

System.debug('TopLevelDomain String :' +Site.TopLevelDomain);

System.debug('getOriginalUrl String :' +Site.getOriginalUrl());

 

between, i would like to get the organization instance name but is there a method to retrieve it ?

 

ie: www.mycompany.com.jp ....what i want is to get back the 'mycompany' ? how i can do that ?

 

Thank you ! 

 


Message Edited by Nakata on 03-09-2010 05:48 AM
I'm building a pricing tool and from this tool users can add products. I really don't want to reinvent the functionality of adding products that already exists. I am trying to create a button that sends the user to the out of the box add products functionality and then when completed it should return the user to the visual force page.

I am failing big time. The code below is what I have and it appears to be populating the retURL parameter correctly but this doesn't work at all. If you hit cancel in the add products tool or save it always returns you to the opp, not the visualforce page which is what I want.

Does anyone know of other URL parameters I need to add to make this work? Is it even possible?

Page:
<apex:page controller="addProductsRedirect">
<apex:outputField value="{!opp.Id}"/>-
<apex:outputField value="{!opp.Name}"/>

<apex:form >
<apex:commandLink value="Add Product" action="{!addProduct}">
<apex:param name="addProductsURL" value="{!URLFOR($Action.OpportunityLineItem.AddProduct)}" assignTo="{!addProductsURL}"/>
</apex:commandLink>
</apex:form>
{!URLFOR($Action.OpportunityLineItem.AddProduct)}
</apex:page>



Controller:
public class addProductsRedirect {

Opportunity opp;
public String addProductsURL {get; set;}

public Opportunity getOpp(){
if(opp == null){
opp = [select Id, Name from Opportunity limit 1];
}
return opp;
}

public PageReference addProduct(){
PageReference p = new PageReference(addProductsURL );
p.getParameters().put('addTo',opp.Id);
p.setRedirect(true);
return p;
}
}

Much thanks,

Jason

Message Edited by TehNrd on 12-15-2009 12:25 PM

Hi...I have written one Trigger on Territory delete ....But I am not able to cover that trigger code in my Test method . When I try to delete territory through my Apex Test class it gives me an " error : DML not allowed on Territory " .... So please let me know if there is any way to cover that code in my Test method.

Is it possible, using one of the action components like actionFunction or actionSupport, to rerender just one piece of a table? (in this case a pageBlockTable)

 

My table is listing Leads.  (Leads contained in a wrapper obj defined in the controller, to be exact.)  One of the columns contains a select control (so we've got a separate select control for each row).  When onchange fires for the select, I'd like to rerender the contents of another column in the same row.  Or it would be OK to rerender the entire other column.  But I don't want to rerender the whole table, or the whole row.

 

I don't seem to be able to do this.  If I put the column id in the rerender attribute, nothing seems to happen.  Same if I use the id of the outputPanel that's contained inside that column (which would be repeated for each row, so not sure how to refer to the outputPanel contained in a particular row.)

 

Is this possible?  Or is it only possible to refresh the table as a whole?

 

Thanks much!