• DBManager
  • NEWBIE
  • 190 Points
  • Member since 2010

  • Chatter
    Feed
  • 7
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 15
    Questions
  • 42
    Replies

I want to clone a record, and change the master-detail parent of the cloned record before inserting it.

 

An example:

  • A and B are Item records. 
  • X and Y are Event/Issue records.
  • The Event/Issue object is a master-detail parent of the Item object.
  • X is a master-detail parent of A.
  1. Clone A, creating record B.
  2. From the date field of record X, find the Event/Issue in the next month, Y.
  3. Set the master-detail parent of record B to Y.
  4. Insert B

What I cannot seem to achieve is the query and process for step 2. I have proved steps 3 and 4 can work, by using the Id of a dummy record.

 

Here is my code so far:

trigger SeriesItems on Item__c (after insert) {

for (integer i = 0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].Item_Sold__c == 'CIPI Membership' && Trigger.new[i].Cloned__c == FALSE){
        for (integer n = 0; n < Trigger.new[i].Subscription_Term_Months__c; n++){
            Item__c copy = Trigger.new[i].clone(false);
            copy.Cloned__c = TRUE;
            copy.Event_Issue__c = 'a08W0000000OiVl'; insert copy; } } } }

 

Hope someone can help.

I have an approval process for a custom object, Purchase Order, that simply unlocks it after it has been approved. This object is a parent in a master-detail with another object, Purchase Items.

 

The approval process is very simple:

  • If the created date equals NULL, then enter step, else approve
  • Approve action: Unlock Record
  • Approve action: Update Purchase Order to 'Re-Opened' Stage

As an administrator, I can run this approval process without an issue.

 

Other users, however, get the following error message when they try:

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger PurchaseOrderTrigger caused an unexpected exception, contact your administrator: PurchaseOrderTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0W200000091UOyEAM; first error: ENTITY_IS_LOCKED, the entity is locked for editing: []: Class.PurchaseItemTriggerMethods.copyStageToStatusFromOrder: line 131, column 1".

 

Here is the trigger reference in the error:

trigger PurchaseOrderTrigger on Purchase_Orders__c (after update) 
{
    PurchaseItemTriggerMethods.copyStageToStatusFromOrder(Trigger.newMap);
}

 

And here is the relevant part of the class reference above:

    public static void copyStageToStatusFromOrder(Map<Id, Purchase_Orders__c> purchOrderMap)
    {
        Map<Id,Purchase_Item__c> purchItemMap = new Map<Id,Purchase_Item__c>([Select Status_Copy__c, Stage__c From Purchase_Item__c Where Purchase_Order__c IN :purchOrderMap.KeySet()]);
        Map<Id,Purchase_Item__c> purchItemMapEmpty = new Map<Id,Purchase_Item__c>(); 
        copyStageToStatusFromItem(purchItemMap.Values(), purchItemMapempty, True);
        Update purchItemMap.Values();
    }

 

What I can't understand is why, if the Purchase Order (and therefore the Purchase Items) are unlocked before the update happens, why the error above is generated?

 

Can anyone help?

 

Thanks in advance.

We have built a Purchase Order system into Salesforce. The Purchase Order object is a parent of the Purchase Item object, and when the PO is approved, a trigger fires which updates all the PIs with the value of the PO Stage field.

 

However, that trigger prevents the Purchase Order from being approved, as it generates the following error:

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger PurchaseOrderTrigger caused an unexpected exception, contact your administrator: PurchaseOrderTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0W200000091BnTEAU; first error: ENTITY_IS_LOCKED, the entity is locked for editing: []: Class.PurchaseItemTriggerMethods.copyStageToStatusFromOrder: line 131, column 1".

 

Here is the firing trigger:

trigger PurchaseOrderTrigger on Purchase_Orders__c (after update) 
{
    PurchaseItemTriggerMethods.copyStageToStatusFromOrder(Trigger.newMap);
}

 

And here are the relevant lines of the class:

    public static void copyStageToStatusFromOrder(Map<Id, Purchase_Orders__c> purchOrderMap)
    {
    
        Map<Id,Purchase_Item__c> purchItemMap = new Map<Id,Purchase_Item__c>([Select Status_Copy__c, Stage__c From Purchase_Item__c Where Purchase_Order__c IN :purchOrderMap.KeySet()]);
        Map<Id,Purchase_Item__c> purchItemMapEmpty = new Map<Id,Purchase_Item__c>(); 
        copyStageToStatusFromItem(purchItemMap.Values(), purchItemMapempty, True);
        Update purchItemMap.Values();
   
    }

 

From my research I have found that it might be something to do with using an after update and the Trigger.newMap value. However, I have not found the right changes that will fix the issue.

 

Can anyone help?

I am trying to use a trigger to create a series of cloned records in a custom object, Item.

 

The Item is a in a Master-Detail relationship with both Opportunity and Event/Issue (aka Issue_Year__c), and I want to change the Event/Issue field for each new, cloned Item. The idea is to split the revenue from one Item across monthly Event/Issue records (for online sales, for example).

 

However, having tried various cominations of before/after insert and .clone(false/true) function, I can't seem to get it to work.

 

Here is my code, as it stands:

 

trigger SeriesItems on Item__c (after insert) {

Set<Id> itemIds = new Set<Id>();

for (integer i = 0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].Item_Sold__c == 'CIPI Membership' && Trigger.new[i].Cloned__c == FALSE){
        itemIds.add(Trigger.new[i].Id);
    }
}

Item__c[] itemList =
    [SELECT Id, Subscription_Term_Months__c, Event_Issue__r.Product__c FROM Item__c WHERE Id IN :itemIds];

if (itemList.size() > 0){
    for (Item__c item :ItemList){
        for (integer n = 1; n < item.Subscription_Term_Months__c; n++){
            Item__c copy = item.clone();
            copy.Cloned__c = TRUE;
            Issue_Year__c ei = 
                [SELECT Id FROM Issue_Year__c WHERE Product__c = :item.Event_Issue__r.Product__c AND CALENDAR_MONTH(Date__c) = 7 LIMIT 1];
            copy.Event_Issue__c = ei.Id;
            insert copy;
        }
    }
}

}

 

Here is the current error message I get, when using a test class:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SeriesItems: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ItemTrigger: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.ItemTrigger: line 13, column 1: [] Trigger.SeriesItems: line 22, column 1:

 

And here is the relevant lines of the trigger to which this error refers:

trigger ItemTrigger on Item__c (before insert,after update,after insert) {

    System.debug('Item Trigger');
    Item__c[] item = Trigger.New;
    Item__c[] itemOld = Trigger.Old;
    Double lineNumber;
    String UniqueId='';
    list<ContactAttendance__c> caList = New list<ContactAttendance__c>(); 
        
    for(Integer i = 0; i< item.Size();i++){
        
        if(Trigger.IsInsert && Trigger.IsBefore){
            lineNumber = [select Max_Line_Number__c from opportunity where id = :item[i].Opportunity__c limit 1].Max_Line_Number__c; 

 

My company runs events and sells stands at those events, and I would like to implement a feature which allows the sales teams to see which stands have been sold and which haven't. However, given that I am still trying to learn Visualforce and Apex through trial and error, I am finding it difficult to implement the 'reserved or vacant' part of this solution.

 

So far I have a visualforce page where the background is set to an image of the venue floorplan. Each stand is overlayed with a <div> element, where the background image of the <div> is a red diagonal line with a transparent background (indicating that stand is reserved).

 

So the code looks something like this:

<apex:page renderAs="pdf" standardController="Issue_Year__c" extensions="FloorplanSet">

<head>
<style type="text/css"> 
@page {
size: A4 landscape;
}

body {
background-image:url("https://c.cs2.content.force.com/servlet/servlet.FileDownload?file=015R0000000AdVD");
background-repeat:no-repeat;
}

.med {
position:absolute;
float:left;
background-image:url("https://c.cs2.content.force.com/servlet/servlet.FileDownload?file=015R0000000Ae5R");
background-repeat:no-repeat;
height:20px;
width:20px;
}

</style>
</head>

<body>

<div style="position:relative;">
<div id="60" class="med" style="top:8.54cm; left:7.05cm;"></div>
</div

</body>

 

Obviously, I only want the red diagonal line to display when the stand number is reserved, so I need an Apex Class to get the list of reserved stands. I can then check if each stand number is in the list, and change the visibility of each <div> accordingly.

 

This class currently looks like this:

public class FloorplanSet {

private final Issue_Year__c evis;
private Set<String> stand;

public FloorplanSet(ApexPages.StandardController stdController) {
    this.evis = (Issue_Year__c)stdController.getRecord();
}

public Set<String> getStand() {
    for (Stand__c sta :[SELECT Name FROM Stand__c WHERE Event_Issue__c = :evis.Id AND Status__c = 'Reserved']){
        stand.add(sta.Name);
    }
    return stand;
}

}

 

What I am having difficulty doing is retrieving the list of stand numbers ({!stand}) and seeing if the stand number for each <div> (which is also the id for the div, i.e. '60') is present in that list.

 

For testing purposes, I have tried simply adding {!stand} into the Visualforce page, and though it saves, I get the following error when I try to load the page:

common.apex.runtime.impl.ExecutionException: Attempt to de-reference a null object 

 

Hope someone can help.

I'm trying to do something really basic, but I can't seem to get it right. 

 

Here is my existing code:

trigger UpdateGBPAmount on Item__c (after insert, after update) {

for (integer i=0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].RecordTypeId == '012R00000004ni3' || Trigger.new[i].RecordTypeId == '01220000000AGp1'){
        Exchange_Rate__c[] rate = [SELECT Id, Date__c, Rate__c FROM Exchange_Rate__c WHERE Name LIKE 'USD%'];
    }
}

}

To the above SOQL query, I want to add a condition to check that the Exchange Rate Date__c field is in the same month and year as the Item Deal_Date__c field. So something like:

 

AND month(Date__c) = month(Trigger.new[i].Deal_Date__c) AND year(Date__c) = year(Trigger.new[i].Deal_Date__c)

 But if try the above, or variations I have found in reasearching this problem, none work.

 

Can anyone help?

 

 

I have run out of lookups within an object (Item__c), and there is a validation rule I want to write. I am trying to do it in an Apex trigger, but it doesn't seem to work properly.

 

The Item__c object has two lookups - one to the Event_Issue__c object and one to the Stand__c object. In turn, the Stand__c object has a lookup to Event_Issue__c. So I want to check, without using a lookup filter, that  Item__r.Stand__r.Event_Issue__c is the same record as Item__r.Event__Issue__c.

 

However, the code I have written seems to throw up the error whether those values match or not.

 

This is the code I have currently:

trigger LinkStand on Item__c (after insert, after update) {

for (integer i=0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].Probability__c == 100 && Trigger.new[i].Item_Sold__c == 'Stand'){
        Stand__c[] stand = [SELECT Id, Event_Issue__c FROM Stand__c WHERE Name != 'TBC'];
        for (Stand__c sta :stand){
            if (sta.Event_Issue__c <> Trigger.new[i].Event_Issue__c){
                Trigger.new[i].Stand__c.adderror('The Stand you have chosen is at the wrong event. Please use the lookup icon to select the correct Stand');
            }
        }
    }
}

}

 

Hope someone can help.

 

 

 

I am trying to use a custom font (for info, Garamond) in a Visualforce page. Following some research, I have found that it might be possible to upload the font file as a static resource, and refer to it in the page.

 

However, I don't seem to be able to find much about the detail of how this works. So:

 

  • Firstly, is this possible?
  • If so, in what format do I need to upload the font file? TTF? Zipped TTF? Other Font file?
  • Lastly, how do I reference the font in the Visualforce page? In other words, what would I put in the blank:
body {
font-family: _________;
}

 

Thanks for any help.

 

I have encountered a really strange problem that, despite trawling teh internets and the dev forums, I can't seem to solve. It is also very confusing, and seemingly has no pattern to it.

 

I have setup a Visualforce page that renders to PDF, and can be opened and printed from the Opportunity page.

 

However, one user seems to be unable to open the page from Internet Explorer 8 on her machine. When she tries to open the page, a window opens with the correct url, but before it loads the page, it cancels itself.

 

Her profile and role are the same as mine (admin), yet when I log on to SF on her machine, I can open the page in IE without any problem. To add to this, if she logs on in Firefox on her machine, or in Chrome on a Windows 7 machine, she can access the page ok.

 

Stranger still, if she logs on to IE9 on the W7 machine, she cannot access the page - it just repetedly rediects her back to the SF login screen. But if I log on to the same browser/machine, after one extra login, I can access the document.

 

Note that, whilst we have the same profile and role, I have the admin functions ticked in my user profile, whilst she does not.

 

Also note that I have tried 

cache="true"

and

standardStylesheets="false"

in the page descriptions, but neither solve the problem.

 

Can we find a solution, or is a browser change the only option?

 

Thanks in advance.

I am still relatively new to Visualforce and am trying to do something quite simple, but failing.

 

I am creating a PDF visualforce page for our contracts and I want the Terms and Conditions shown on the contract to vary, according to which value a user chooses from a picklist.

 

This is what I have so far:

 

{!IF(opportunity.Contract__c!='HPE2',
'1. Lorem ipsum dolor sit amet......' & '2. Sed accumsan dapibus dolor....' & '3. Integer ac urna quam....',
'1. Lorem ipsum dolor sit amet......' & '2. Sed accumsan dapibus dolor....' & '3. Integer ac urna quam....'
)}

 

What I cannot seem to do is to format the T&C's so that there is a line break between clauses. I have tried using the following:

 

& br() &

 But they do not work. Nor do any combination of <br/> or \n

 

So where I should have:

 

1. Lorem ipsum dolor sit amet.....
2. Sed accumsan dapibus dolor.....
3. Integer ac urna quam.....

 I currently get:

1. Lorem ipsum dolor sit amet.....2. Sed accumsan dapibus dolor.....3. Integer ac urna quam.....

 There must be a simple way of doing this?

 

I have created a custom object, called Magazine_Subscription__c. The object includes Contact (lookup), Subscription_Date__c (date-time), Magazine__c (picklist) and Status__c (picklist) fields.

 

What I want for each Contact is the most recent record (by Subscription_Date__c) for each Magazine__c  marked as "Current" in the Status__c field, and for the rest to be marked as "Old".

 

But no matter my research into Group By, Order By and Aggregate functions, I can't seem to work out how to do it.

 

This is what I've got so far, I hope someone can help:

trigger UpdateOldSubs on Magazine_Subscription__c (after update, after insert) {

Set<Id> ConIds = new Set<Id>();

if (Trigger.isUpdate){
    for (integer i=0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].Contact__c != Trigger.old[i].Contact__c){
            ConIds.add(Trigger.old[i].Contact__c);
        }
    }
}

if (ConIds.size() > 0){

Contact[] ConList = 
[SELECT Id FROM Contact WHERE Id IN :ConIds];

Magazine_Subscription__c[] ConSubList = 
[SELECT Id, Contact__c, Subscription_Date__c,  Status__c, Magazine__c FROM Magazine_Subscription__c  WHERE Contact__c IN :ConIds];

for (Contact cons :ConList){
	for (Magazine_Subscription__c subs :ConSubList){
		if (subs.Contact__c == cons.Id){
		//*******New Code Here*******
		}
	}
}

}

}

 

I am fairly new to Visualforce, and am trying to get to grips with the more complex sides, such as Custom controllers and extensions, etc. 

 

I have been looking at this problem for a while, and have found a few possible solutions, though none have worked.

 

The problem:

I am trying to create a list of invoices that can be generated for any particular Account. The list setup is fine, an I am happy with it. What I lack is a final total of the 'Gross Unpaid' column.

 

Possible solutions:

Following a lot of research, I found the following page:

Sum value that is within apex:repeat

 

Which suggested there were three ways to do this:

1. You can have a trigger on case on inset, delete, update event you can modify the sum of Cx_Case_Hours__c  in this trigger.
2. Also you can use java script code to find the repeat column values and add them through  loop which will display in the html variable
3. Or can use a separate function in your controller for the above calculation.

 

I have now tried all of these, with no joy.

 

No.1

This didn't work because Trigger wasn't being fired correctly

No. 2

I got an example from this page:

How to Count and Sum the Items and display it outside the apex:repeat

But it didn't work for me (not sure why, it just didn't bring back any values).

No.3

I tried pages like this one:

Can't figure out how to display a simple SUM function on my VF Page

But I'm not confident enough with Extensions to implement it, and the errors I got were a mystery to me!

 

Can anyone advise a good solution to do this.

 

My code is below - just to confirm, I want to finish the page with a sum of the Sales_Invoice__r.Gross__c field where Sales_Invoice__r.Status__c = 'Unpaid'.

 

Hope someone can help. Here's my page:

<apex:page renderAs="pdf" standardController="Account">

<apex:stylesheet value="{!URLFOR($Resource.A4Page, 'A4Page.css')}"/>

<apex:image id="CampdenLogo" value="https://emea.salesforce.com/servlet/servlet.ImageServer?id=01520000000liQP&oid=00D2000000075hl"
width="650" height="200"/><br></br>

<table cellpadding="0" cellspacing="0" align="right">
<tr><td>
<strong>Campden Media</strong><br/>
1 St John's Lane<br/>
London<br/>
EC1M 4PN<br/>
</td></tr>
</table>
<br/><br/><br/><br/><br/>

<table cellpadding="0" cellspacing="0">
<tr><td>
<strong>{!account.Name}</strong><br/>
</td></tr>
<tr><td>
{!account.BillingStreet}<br/>
</td></tr>
<tr><td>
{!account.BillingCity}<br/>
</td></tr>
<tr style="display: {!IF(account.BillingState == NULL, 'none', 'block')};"><td>
{!account.BillingState}<br/>
</td></tr>
<tr><td>
{!account.BillingPostalCode}<br/>
</td></tr>
<tr><td>
{!account.BillingCountry}<br/><br/>
</td></tr>
<tr><td>
<strong>Date:</strong> <apex:outputText value="{0,date, dd/MM/yyyy}"><apex:param value="{!NOW()}"/></apex:outputText><br/><br/>
</td></tr>
</table>
<br/>

<table cellpadding="0" cellspacing="0" align="center" style="font-weight:bold; text-align:center;">
<tr>
<td style="height:30px; width:120px;  border-left:1px solid black; border-bottom:1px solid black; border-top:1px solid black;">
Invoice Date
</td>
<td style="height:30px; width:100px;  border-left:1px solid black; border-bottom:1px solid black; border-top:1px solid black;">
Invoice No
</td>
<td style="height:30px; width:120px;  border-left:1px solid black; border-bottom:1px solid black; border-top:1px solid black;">
Gross
</td>
<td style="height:30px; width:120px;  border-left:1px solid black; border-bottom:1px solid black; border-top:1px solid black;">
Due Date
</td>
<td style="height:30px; width:100px;  border-left:1px solid black; border-bottom:1px solid black; border-top:1px solid black;">
Days Aged
</td>
<td style="height:30px; width:100px;  border:1px solid black;">
Status
</td>
</tr>
</table>

<apex:repeat var="cx" value="{!account.Sales_Invoices2__r}">
<table cellpadding="0" cellspacing="0" align="center" style="text-align:center;">
<tr style="display: {!IF(cx.Status__c == "Unpaid", 'block', 'none')};">

<td style="height:30px; width:120px;  border-left:1px solid black; border-bottom:1px solid black;">
<apex:outputText value="{0,date,dd/MM/yyyy}"><apex:param value="{!cx.Invoice_Date__c}"/></apex:outputText>
</td>

<td style="height:30px; width:100px;  border-left:1px solid black; border-bottom:1px solid black;">
{!cx.Name}
</td>

<td style="height:30px; width:120px;  border-left:1px solid black; border-bottom:1px solid black;">
<apex:outputText value="{0,number, £#,##0.00}"> <apex:param value="{!cx.Gross__c}"/></apex:outputText>
</td>

<td style="height:30px; width:120px;  border-left:1px solid black; border-bottom:1px solid black;">
{!DAY(cx.Due_Date_New__c)}/{!MONTH(cx.Due_Date_New__c)}/{!YEAR(cx.Due_Date_New__c)}
</td>

<td style="height:30px; width:100px;  border-left:1px solid black; border-bottom:1px solid black;">
{!cx.Days_Aged__c}
</td>

<td style="height:30px; width:100px;  border-left:1px solid black; border-bottom:1px solid black; border-right:1px solid black;">
{!cx.Status__c}
</td>

</tr>
</table>
</apex:repeat>
<br/><br/>

<p>
<strong>Total Gross Amount:</strong> <apex:outputText value="{0,number, £#,##0.00}"> <apex:param value="{!account.Gross_Unpaid__c}"/></apex:outputText>
</p>

</apex:page>

 Thanks in advance!

I am quite stuck after trying every trick I can find in the book, having done plenty of research into the classic "Too many SOQL queries" error.

 

I have, for example, made sure that there is no SELECT statement or UPDATE within a FOR loop (which seem to be the most common mistakes).

 

And yet still the error occurs!

 

Here is the error:

System.LimitException: Too many SOQL queries: 101

Class.RecordUpdate.updateInvoices: line 54, column 9 Trigger.DepartmentTrigger1: line 66, column 5

 Here is the Trigger that is causing the problem:

trigger DepartmentTrigger1 on Account (after update) {

  Set<Id> accountIds = new Set<Id>();


for (Integer i = 0; i < Trigger.new.size(); i++){
    if (
    //Changing btw two Parent Companies
        (Trigger.new[i].Parent_Company__c != Trigger.old[i].Parent_Company__c && 
        Trigger.new[i].Department__c == TRUE && Trigger.old[i].Department__c == TRUE)||
    //Changing to a Department
        (Trigger.new[i].Department__c == true && Trigger.old[i].Department__c == false)||
    // Changing from a Department
        (Trigger.new[i].Department__c == FALSE && Trigger.old[i].Department__c == TRUE)
        ) {
        accountIds.add(Trigger.new[i].Id);
    }
}
  
    //Update Contacts
    RecordUpdate.updateContacts(accountIds);
    
    //Update Deals
    RecordUpdate.updateDeals(accountIds);    

    //Update Sales Invoices
    RecordUpdate.updateInvoices(accountIds);
    
}

 And here is the Class that is referenced above:

public with sharing class RecordUpdate {

public static void updateContacts(Set<Id> contactset){
    Contact[] relatedContacts =
        [SELECT f.id, f.Accountid, f.Account.Department__c, f.Account.Parent_Company__c, f.Parent_Company__c
        FROM Contact f
        WHERE f.Accountid IN :contactset];

    for ( Contact childc : relatedContacts ) {
        if (childc.Account.Department__c == TRUE) {
            childc.Parent_Company__c = childc.Account.Parent_Company__c;
        }
        else
            childc.Parent_Company__c = childc.AccountId;
    }
    update relatedContacts;
}


public static void updateDeals(Set<Id> dealset){
    List<Opportunity> relatedDeals =
        [SELECT g.id, g.Accountid, g.Account.RecordTypeId, g.Account.Department__c, g.Account.Parent_Company__c, 
        g.Parent_Company__c, g.Agency_Client__r.id , g.Agency_Client__r.Department__c, g.Agency_Client__r.Parent_Company__c
        FROM Opportunity g
        WHERE g.Accountid IN :dealset OR g.Agency_Client__r.id IN :dealset];
    
    for ( Opportunity childo : relatedDeals ) {
        if (childo.Account.Department__c == TRUE) {
            childo.Parent_Company__c = childo.Account.Parent_Company__c;
        }
        else if (childo.Account.RecordTypeId == '01220000000Dqq6'){
            if (childo.Agency_Client__r.Department__c == TRUE){
                childo.Parent_Company__c = childo.Agency_Client__r.Parent_Company__c;
            }
            else
                childo.Parent_Company__c = childo.Agency_Client__c;
        }
        else
            childo.Parent_Company__c = childo.AccountId;
    }
    update relatedDeals;
}


public static void updateInvoices(Set<Id> invoiceset){
    Sales_Invoices__c[] relatedInvoices =
        [SELECT s.Id, s.Company__c, s.Company__r.Parent_Company__c, s.Company__r.Department__c, s.Parent_Company__c, s.Agency_Client__r.Id
        FROM Sales_Invoices__c s
        WHERE s.Company__c IN :invoiceset OR s.Agency_Client__c IN :invoiceset
        FOR UPDATE];
    
    for ( Sales_Invoices__c childs : relatedInvoices ) {
        if (childs.Company__r.Department__c == TRUE) {
            childs.Parent_Company__c = childs.Company__r.Parent_Company__c;
        }
        else
            childs.Parent_Company__c = childs.Company__c;
    }
    update relatedInvoices;
}

}

 

 There are a few other triggers that are involved, but they are tiny in comparison to the above example.

 

Thanks for any help you can offer.

I think I might be a little bit blind here, because I cannot see why a validation rule should be throwing up an exception.

 

The only thing that I can fathom that would create the error would be if the associated test class was setting an illegal date - i.e. one that is not on the first of the month.

 

However, the only Start Dates set are:

Start_Date__c=date.newinstance(2011, 1, 1)

Start_Date__c=date.newinstance(2011, 7, 1)

which are both on the first of the month!



 

So, first, the error:

Error:Apex trigger ItemandIPISubscriptionLinks caused an unexpected exception, contact your administrator: ItemandIPISubscriptionLinks: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a092000000KcY3sAAF; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Start Date must be the first of the month.: [Start_Date__c]: Trigger.ItemandIPISubscriptionLinks: line 223, column 1

 Now the validation rule:

AND (
NOT ( ISBLANK ( Start_Date__c )),
DAY ( Start_Date__c ) > 1
)

 Now the Trigger:

trigger ItemandIPISubscriptionLinks on Item__c (after update, after insert) {

Set<Id> itemIds = new Set<Id>();
Set<Id> eventIds = new Set<Id>();
Set<Id> itemCountIds = new Set<Id>();


//Put the list value ids in the set
if (Trigger.isUpdate){
    for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].RecordTypeId == '01220000000AGp1' &&
            ((Trigger.new[i].Probability__c == 100 && Trigger.new[i].Cancelled__c == FALSE 
            && 
            ((Trigger.old[i].Probability__c <> 100 || Trigger.old[i].Cancelled__c <> FALSE)
            || 
            (Trigger.new[i].Start_Date__c <> Trigger.old[i].Start_Date__c ||
            Trigger.new[i].Term__c <> Trigger.old[i].Term__c ||
            Trigger.new[i].Full_Amount2__c <> Trigger.old[i].Full_Amount2__c)))
            
            ||
            
            (Trigger.new[i].Probability__c <> 100 || Trigger.new[i].Cancelled__c <> FALSE) 
            && 
            Trigger.old[i].Probability__c == 100 && Trigger.old[i].Cancelled__c == FALSE)
           )
        {
            itemIds.add(Trigger.new[i].id);
            eventIds.add(Trigger.new[i].Event_Issue__c);
        }
    }
}
if (Trigger.isInsert){
    for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].RecordTypeId == '01220000000AGp1' && Trigger.new[i].Probability__c == 100 && Trigger.new[i].Cancelled__c == FALSE) {
            itemIds.add(Trigger.new[i].id);
            eventIds.add(Trigger.new[i].Event_Issue__c);
        }
    }
}

IPI_Subscription__c[] sub =
[SELECT Id, Event_Issue__c, Month__c FROM IPI_Subscription__c WHERE Event_Issue__c IN :eventIds];

Item__c[] ite =
[SELECT Id, Event_Issue__c, Probability__c, Cancelled__c, Start_Date__c, End_Date__c, Term__c, Amount_Formula__c, Monthly_Amount__c, Jan_Amount__c, Feb_Amount__c, Mar_Amount__c,
Apr_Amount__c, May_Amount__c, Jun_Amount__c, Jul_Amount__c, Aug_Amount__c, Sep_Amount__c, Oct_Amount__c, 
Nov_Amount__c, Dec_Amount__c FROM Item__c WHERE Event_Issue__c IN :eventIds];

for (IPI_Subscription__c subs :sub){
    Decimal amou = 0;
    Integer n = 0;

for (Item__c items :ite){
    
    if (items.Probability__c == 100 && items.Cancelled__c == FALSE){
    
    //January 
        if (subs.Month__c == 'Jan'){
            if (items.Jan_Amount__c>0){
                items.Jan_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                n += 1;
                subs.Amount_Sold2__c = amou;
            }
            else
                items.Jan_Subscription__c = NULL;
        }

    //February
        else if (subs.Month__c == 'Feb'){
            if (items.Feb_Amount__c>0){
                items.Feb_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Feb_Subscription__c = NULL;
        }
       
    //March
        else if (subs.Month__c == 'Mar'){
            if (items.Mar_Amount__c>0){
                items.Mar_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Mar_Subscription__c = NULL;
        }
    
    //April
        else if (subs.Month__c == 'Apr'){
            if (items.Apr_Amount__c>0){
                items.Apr_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Apr_Subscription__c = NULL;
        }
    
    //May
        else if (subs.Month__c == 'May'){
            if (items.May_Amount__c>0){
                items.May_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.May_Subscription__c = NULL;
        }
    
    //Jun
        else if (subs.Month__c == 'Jun'){
            if (items.Jun_Amount__c>0){
                items.Jun_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Jun_Subscription__c = NULL;
        }
    
    //July
        else if (subs.Month__c == 'Jul'){
            if (items.Jul_Amount__c>0){
                items.Jul_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Jul_Subscription__c = NULL;
        }
    
    //August
        else if (subs.Month__c == 'Aug'){
            if (items.Aug_Amount__c>0){
                items.Aug_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Aug_Subscription__c = NULL;
        }
    
    //September
        else if (subs.Month__c == 'Sep'){
            if (items.Sep_Amount__c>0){
                items.Sep_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Sep_Subscription__c = NULL;
        }
    
    //October
        else if (subs.Month__c == 'Oct'){
            if (items.Oct_Amount__c>0){
                items.Oct_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Oct_Subscription__c = NULL;
        }
    
    //November
        else if (subs.Month__c == 'Nov'){
            if (items.Nov_Amount__c>0){
                items.Nov_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Nov_Subscription__c = NULL;
        }
    
    //December
        else if (subs.Month__c == 'Dec'){
            if (items.Dec_Amount__c>0){
                items.Dec_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Dec_Subscription__c = NULL;
        }
    }
    
    else {
    items.Jan_Subscription__c = NULL;
    items.Feb_Subscription__c = NULL;
    items.Mar_Subscription__c = NULL;
    items.Apr_Subscription__c = NULL;
    items.May_Subscription__c = NULL;
    items.Jun_Subscription__c = NULL;
    items.Jul_Subscription__c = NULL;
    items.Aug_Subscription__c = NULL;
    items.Sep_Subscription__c = NULL;
    items.Oct_Subscription__c = NULL;
    items.Nov_Subscription__c = NULL;
    items.Dec_Subscription__c = NULL;
    }
}
    
    subs.Number_Sold__c = n;
    
}

update sub;
update ite;





}

 

And finally the Test class:

@isTest
private class TestIPITriggers {

static TestMethod void TestTotalItemAmount(){

Account company = new Account(Name='test company', BillingStreet='123 red', BillingCity='London', BillingPostalCode='123fgh', BillingCountry='United Kingdom');
insert company;

Contact contact = new Contact(LastName = 'test', AccountId = company.Id, Email = 'test@test.com', Phone='1234');
insert contact;

Opportunity deal = new Opportunity(Name='autofill', AccountId = company.Id, RecordTypeId ='01220000000AGqi', Contact_Delegate__c=contact.Id, CloseDate=date.today(), 
                    StageName='Closed - Awaiting Approval', Invoice_Address__c='Company', Address_Invoice_to_contact_above__c='Yes', 
                    Voucher_Copy_Address__c='Company', Address_Voucher_Copy_to_contact_above__c='Yes');
insert deal;

Issue_Year__c event = new Issue_Year__c(Name='LC11a', Date__c=date.newinstance(2011, 1, 1), Description__c='Leaders Council 2011', SUN_T3_Code__c = 'LC2011');
insert event;

IPI_Subscription__c subscription1 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Jan', Name='LCJan11a');
insert subscription1;

IPI_Subscription__c subscription2 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Feb', Name='LCFeb11a');
insert subscription2;

IPI_Subscription__c subscription3 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Mar', Name='LCMar11a');
insert subscription3;

IPI_Subscription__c subscription4 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Apr', Name='LCApr11a');
insert subscription4;

IPI_Subscription__c subscription5 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='May', Name='LCMay11a');
insert subscription5;

IPI_Subscription__c subscription6 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Jun', Name='LCJun11a');
insert subscription6;

IPI_Subscription__c subscription7 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Jul', Name='LCJul11a');
insert subscription7;

IPI_Subscription__c subscription8 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Aug', Name='LCAug11a');
insert subscription8;

IPI_Subscription__c subscription9 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Sep', Name='LCSep11a');
insert subscription9;

IPI_Subscription__c subscription10 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Oct', Name='LCOct11a');
insert subscription10;

IPI_Subscription__c subscription11 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Nov', Name='LCNov11a');
insert subscription11;

IPI_Subscription__c subscription12 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Dec', Name='LCDec11a');
insert subscription12;

Item__c item1 = new Item__c(Name='.', Opportunity__c = deal.Id, Full_Amount2__c=6000, RecordTypeId='01220000000AGp1', Probability__c=100, 
                Event_Issue__c = event.Id, Start_Date__c=date.newinstance(2011, 1, 1), Term__c=6, Payment_Terms__c='28 days');
insert item1;

Item__c[] items = [SELECT Id, Amount__c, Amount_Formula__c, Jan_Subscription__r.Name, Jan_Amount__c, Event_Issue__r.Name FROM Item__c WHERE Event_Issue__r.Name = 'LC11a'];

for (Item__c it :items){
system.assertEquals(it.Jan_Amount__c, 1000);
system.assertEquals(it.Jan_Subscription__r.Name, 'LCJan11a');
}

item1.Start_Date__c=date.newinstance(2011, 7, 1);
update item1;

item1.Probability__c = 50;
update item1;

delete item1;


}

}

 

 

Hope you can help!

 

 

<apex:page renderAs="pdf" standardController="Issue_Year__c">

    <table border="0" width="50%" style="page-break-after:always; float:left;">
<apex:repeat var="items" first="0" rows="7" value="{!Issue_Year__c.Items__r}">
<tr>
<td>{!items.Opportunity__r.Contact_Delegate__r.FirstName} {!items.Opportunity__r.Contact_Delegate__r.LastName}
<br></br>{!items.Opportunity__r.Contact_Delegate__r.Account.Name}</td>
</tr>
</apex:repeat> 
    </table>


    <table border="0"  width="50%" style="page-break-after:always; float:right;">
<apex:repeat var="items" first="7" rows="7" value="{!Issue_Year__c.Items__r}">
<tr>
<td>{!items.Opportunity__r.Contact_Delegate__r.FirstName} {!items.Opportunity__r.Contact_Delegate__r.LastName}
<br></br>{!items.Opportunity__r.Contact_Delegate__r.Account.Name}</td>
</tr>
</apex:repeat> 
     </table>
         
    
</apex:page>

 

Above is my code so far for creating a label template from a Contact list. The style info is not included above, however, labels need to be sized properly, aligned properly, in seven rows per page (i.e. followed by a page break) and two columns. This is because they need to print on to label sticker templates.

 

The difficulty is getting the records to display correctly AND iterate fully over a list that will change in length.

 

So far, I can get the first page to display correctly. But then I get stuck going into the next page - the list stops after 14 results because of the repeat limits.

 

I could perhaps use some kind of loop, so something along the lines of:

 

n=(n + 7)

 

<apex:repeat var="items" first="n" rows="7" value="{!Issue_Year__c.Items__r}">

 

But I have not found much help on the boards or the Guide.

 

Hope someone can help.

 

Thanks.

 

I want to clone a record, and change the master-detail parent of the cloned record before inserting it.

 

An example:

  • A and B are Item records. 
  • X and Y are Event/Issue records.
  • The Event/Issue object is a master-detail parent of the Item object.
  • X is a master-detail parent of A.
  1. Clone A, creating record B.
  2. From the date field of record X, find the Event/Issue in the next month, Y.
  3. Set the master-detail parent of record B to Y.
  4. Insert B

What I cannot seem to achieve is the query and process for step 2. I have proved steps 3 and 4 can work, by using the Id of a dummy record.

 

Here is my code so far:

trigger SeriesItems on Item__c (after insert) {

for (integer i = 0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].Item_Sold__c == 'CIPI Membership' && Trigger.new[i].Cloned__c == FALSE){
        for (integer n = 0; n < Trigger.new[i].Subscription_Term_Months__c; n++){
            Item__c copy = Trigger.new[i].clone(false);
            copy.Cloned__c = TRUE;
            copy.Event_Issue__c = 'a08W0000000OiVl'; insert copy; } } } }

 

Hope someone can help.

I have an approval process for a custom object, Purchase Order, that simply unlocks it after it has been approved. This object is a parent in a master-detail with another object, Purchase Items.

 

The approval process is very simple:

  • If the created date equals NULL, then enter step, else approve
  • Approve action: Unlock Record
  • Approve action: Update Purchase Order to 'Re-Opened' Stage

As an administrator, I can run this approval process without an issue.

 

Other users, however, get the following error message when they try:

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger PurchaseOrderTrigger caused an unexpected exception, contact your administrator: PurchaseOrderTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0W200000091UOyEAM; first error: ENTITY_IS_LOCKED, the entity is locked for editing: []: Class.PurchaseItemTriggerMethods.copyStageToStatusFromOrder: line 131, column 1".

 

Here is the trigger reference in the error:

trigger PurchaseOrderTrigger on Purchase_Orders__c (after update) 
{
    PurchaseItemTriggerMethods.copyStageToStatusFromOrder(Trigger.newMap);
}

 

And here is the relevant part of the class reference above:

    public static void copyStageToStatusFromOrder(Map<Id, Purchase_Orders__c> purchOrderMap)
    {
        Map<Id,Purchase_Item__c> purchItemMap = new Map<Id,Purchase_Item__c>([Select Status_Copy__c, Stage__c From Purchase_Item__c Where Purchase_Order__c IN :purchOrderMap.KeySet()]);
        Map<Id,Purchase_Item__c> purchItemMapEmpty = new Map<Id,Purchase_Item__c>(); 
        copyStageToStatusFromItem(purchItemMap.Values(), purchItemMapempty, True);
        Update purchItemMap.Values();
    }

 

What I can't understand is why, if the Purchase Order (and therefore the Purchase Items) are unlocked before the update happens, why the error above is generated?

 

Can anyone help?

 

Thanks in advance.

We have built a Purchase Order system into Salesforce. The Purchase Order object is a parent of the Purchase Item object, and when the PO is approved, a trigger fires which updates all the PIs with the value of the PO Stage field.

 

However, that trigger prevents the Purchase Order from being approved, as it generates the following error:

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger PurchaseOrderTrigger caused an unexpected exception, contact your administrator: PurchaseOrderTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0W200000091BnTEAU; first error: ENTITY_IS_LOCKED, the entity is locked for editing: []: Class.PurchaseItemTriggerMethods.copyStageToStatusFromOrder: line 131, column 1".

 

Here is the firing trigger:

trigger PurchaseOrderTrigger on Purchase_Orders__c (after update) 
{
    PurchaseItemTriggerMethods.copyStageToStatusFromOrder(Trigger.newMap);
}

 

And here are the relevant lines of the class:

    public static void copyStageToStatusFromOrder(Map<Id, Purchase_Orders__c> purchOrderMap)
    {
    
        Map<Id,Purchase_Item__c> purchItemMap = new Map<Id,Purchase_Item__c>([Select Status_Copy__c, Stage__c From Purchase_Item__c Where Purchase_Order__c IN :purchOrderMap.KeySet()]);
        Map<Id,Purchase_Item__c> purchItemMapEmpty = new Map<Id,Purchase_Item__c>(); 
        copyStageToStatusFromItem(purchItemMap.Values(), purchItemMapempty, True);
        Update purchItemMap.Values();
   
    }

 

From my research I have found that it might be something to do with using an after update and the Trigger.newMap value. However, I have not found the right changes that will fix the issue.

 

Can anyone help?

I am trying to use a trigger to create a series of cloned records in a custom object, Item.

 

The Item is a in a Master-Detail relationship with both Opportunity and Event/Issue (aka Issue_Year__c), and I want to change the Event/Issue field for each new, cloned Item. The idea is to split the revenue from one Item across monthly Event/Issue records (for online sales, for example).

 

However, having tried various cominations of before/after insert and .clone(false/true) function, I can't seem to get it to work.

 

Here is my code, as it stands:

 

trigger SeriesItems on Item__c (after insert) {

Set<Id> itemIds = new Set<Id>();

for (integer i = 0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].Item_Sold__c == 'CIPI Membership' && Trigger.new[i].Cloned__c == FALSE){
        itemIds.add(Trigger.new[i].Id);
    }
}

Item__c[] itemList =
    [SELECT Id, Subscription_Term_Months__c, Event_Issue__r.Product__c FROM Item__c WHERE Id IN :itemIds];

if (itemList.size() > 0){
    for (Item__c item :ItemList){
        for (integer n = 1; n < item.Subscription_Term_Months__c; n++){
            Item__c copy = item.clone();
            copy.Cloned__c = TRUE;
            Issue_Year__c ei = 
                [SELECT Id FROM Issue_Year__c WHERE Product__c = :item.Event_Issue__r.Product__c AND CALENDAR_MONTH(Date__c) = 7 LIMIT 1];
            copy.Event_Issue__c = ei.Id;
            insert copy;
        }
    }
}

}

 

Here is the current error message I get, when using a test class:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SeriesItems: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ItemTrigger: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.ItemTrigger: line 13, column 1: [] Trigger.SeriesItems: line 22, column 1:

 

And here is the relevant lines of the trigger to which this error refers:

trigger ItemTrigger on Item__c (before insert,after update,after insert) {

    System.debug('Item Trigger');
    Item__c[] item = Trigger.New;
    Item__c[] itemOld = Trigger.Old;
    Double lineNumber;
    String UniqueId='';
    list<ContactAttendance__c> caList = New list<ContactAttendance__c>(); 
        
    for(Integer i = 0; i< item.Size();i++){
        
        if(Trigger.IsInsert && Trigger.IsBefore){
            lineNumber = [select Max_Line_Number__c from opportunity where id = :item[i].Opportunity__c limit 1].Max_Line_Number__c; 

 

My company runs events and sells stands at those events, and I would like to implement a feature which allows the sales teams to see which stands have been sold and which haven't. However, given that I am still trying to learn Visualforce and Apex through trial and error, I am finding it difficult to implement the 'reserved or vacant' part of this solution.

 

So far I have a visualforce page where the background is set to an image of the venue floorplan. Each stand is overlayed with a <div> element, where the background image of the <div> is a red diagonal line with a transparent background (indicating that stand is reserved).

 

So the code looks something like this:

<apex:page renderAs="pdf" standardController="Issue_Year__c" extensions="FloorplanSet">

<head>
<style type="text/css"> 
@page {
size: A4 landscape;
}

body {
background-image:url("https://c.cs2.content.force.com/servlet/servlet.FileDownload?file=015R0000000AdVD");
background-repeat:no-repeat;
}

.med {
position:absolute;
float:left;
background-image:url("https://c.cs2.content.force.com/servlet/servlet.FileDownload?file=015R0000000Ae5R");
background-repeat:no-repeat;
height:20px;
width:20px;
}

</style>
</head>

<body>

<div style="position:relative;">
<div id="60" class="med" style="top:8.54cm; left:7.05cm;"></div>
</div

</body>

 

Obviously, I only want the red diagonal line to display when the stand number is reserved, so I need an Apex Class to get the list of reserved stands. I can then check if each stand number is in the list, and change the visibility of each <div> accordingly.

 

This class currently looks like this:

public class FloorplanSet {

private final Issue_Year__c evis;
private Set<String> stand;

public FloorplanSet(ApexPages.StandardController stdController) {
    this.evis = (Issue_Year__c)stdController.getRecord();
}

public Set<String> getStand() {
    for (Stand__c sta :[SELECT Name FROM Stand__c WHERE Event_Issue__c = :evis.Id AND Status__c = 'Reserved']){
        stand.add(sta.Name);
    }
    return stand;
}

}

 

What I am having difficulty doing is retrieving the list of stand numbers ({!stand}) and seeing if the stand number for each <div> (which is also the id for the div, i.e. '60') is present in that list.

 

For testing purposes, I have tried simply adding {!stand} into the Visualforce page, and though it saves, I get the following error when I try to load the page:

common.apex.runtime.impl.ExecutionException: Attempt to de-reference a null object 

 

Hope someone can help.

I'm trying to do something really basic, but I can't seem to get it right. 

 

Here is my existing code:

trigger UpdateGBPAmount on Item__c (after insert, after update) {

for (integer i=0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].RecordTypeId == '012R00000004ni3' || Trigger.new[i].RecordTypeId == '01220000000AGp1'){
        Exchange_Rate__c[] rate = [SELECT Id, Date__c, Rate__c FROM Exchange_Rate__c WHERE Name LIKE 'USD%'];
    }
}

}

To the above SOQL query, I want to add a condition to check that the Exchange Rate Date__c field is in the same month and year as the Item Deal_Date__c field. So something like:

 

AND month(Date__c) = month(Trigger.new[i].Deal_Date__c) AND year(Date__c) = year(Trigger.new[i].Deal_Date__c)

 But if try the above, or variations I have found in reasearching this problem, none work.

 

Can anyone help?

 

 

I have encountered a really strange problem that, despite trawling teh internets and the dev forums, I can't seem to solve. It is also very confusing, and seemingly has no pattern to it.

 

I have setup a Visualforce page that renders to PDF, and can be opened and printed from the Opportunity page.

 

However, one user seems to be unable to open the page from Internet Explorer 8 on her machine. When she tries to open the page, a window opens with the correct url, but before it loads the page, it cancels itself.

 

Her profile and role are the same as mine (admin), yet when I log on to SF on her machine, I can open the page in IE without any problem. To add to this, if she logs on in Firefox on her machine, or in Chrome on a Windows 7 machine, she can access the page ok.

 

Stranger still, if she logs on to IE9 on the W7 machine, she cannot access the page - it just repetedly rediects her back to the SF login screen. But if I log on to the same browser/machine, after one extra login, I can access the document.

 

Note that, whilst we have the same profile and role, I have the admin functions ticked in my user profile, whilst she does not.

 

Also note that I have tried 

cache="true"

and

standardStylesheets="false"

in the page descriptions, but neither solve the problem.

 

Can we find a solution, or is a browser change the only option?

 

Thanks in advance.

I think I might be a little bit blind here, because I cannot see why a validation rule should be throwing up an exception.

 

The only thing that I can fathom that would create the error would be if the associated test class was setting an illegal date - i.e. one that is not on the first of the month.

 

However, the only Start Dates set are:

Start_Date__c=date.newinstance(2011, 1, 1)

Start_Date__c=date.newinstance(2011, 7, 1)

which are both on the first of the month!



 

So, first, the error:

Error:Apex trigger ItemandIPISubscriptionLinks caused an unexpected exception, contact your administrator: ItemandIPISubscriptionLinks: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a092000000KcY3sAAF; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Start Date must be the first of the month.: [Start_Date__c]: Trigger.ItemandIPISubscriptionLinks: line 223, column 1

 Now the validation rule:

AND (
NOT ( ISBLANK ( Start_Date__c )),
DAY ( Start_Date__c ) > 1
)

 Now the Trigger:

trigger ItemandIPISubscriptionLinks on Item__c (after update, after insert) {

Set<Id> itemIds = new Set<Id>();
Set<Id> eventIds = new Set<Id>();
Set<Id> itemCountIds = new Set<Id>();


//Put the list value ids in the set
if (Trigger.isUpdate){
    for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].RecordTypeId == '01220000000AGp1' &&
            ((Trigger.new[i].Probability__c == 100 && Trigger.new[i].Cancelled__c == FALSE 
            && 
            ((Trigger.old[i].Probability__c <> 100 || Trigger.old[i].Cancelled__c <> FALSE)
            || 
            (Trigger.new[i].Start_Date__c <> Trigger.old[i].Start_Date__c ||
            Trigger.new[i].Term__c <> Trigger.old[i].Term__c ||
            Trigger.new[i].Full_Amount2__c <> Trigger.old[i].Full_Amount2__c)))
            
            ||
            
            (Trigger.new[i].Probability__c <> 100 || Trigger.new[i].Cancelled__c <> FALSE) 
            && 
            Trigger.old[i].Probability__c == 100 && Trigger.old[i].Cancelled__c == FALSE)
           )
        {
            itemIds.add(Trigger.new[i].id);
            eventIds.add(Trigger.new[i].Event_Issue__c);
        }
    }
}
if (Trigger.isInsert){
    for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].RecordTypeId == '01220000000AGp1' && Trigger.new[i].Probability__c == 100 && Trigger.new[i].Cancelled__c == FALSE) {
            itemIds.add(Trigger.new[i].id);
            eventIds.add(Trigger.new[i].Event_Issue__c);
        }
    }
}

IPI_Subscription__c[] sub =
[SELECT Id, Event_Issue__c, Month__c FROM IPI_Subscription__c WHERE Event_Issue__c IN :eventIds];

Item__c[] ite =
[SELECT Id, Event_Issue__c, Probability__c, Cancelled__c, Start_Date__c, End_Date__c, Term__c, Amount_Formula__c, Monthly_Amount__c, Jan_Amount__c, Feb_Amount__c, Mar_Amount__c,
Apr_Amount__c, May_Amount__c, Jun_Amount__c, Jul_Amount__c, Aug_Amount__c, Sep_Amount__c, Oct_Amount__c, 
Nov_Amount__c, Dec_Amount__c FROM Item__c WHERE Event_Issue__c IN :eventIds];

for (IPI_Subscription__c subs :sub){
    Decimal amou = 0;
    Integer n = 0;

for (Item__c items :ite){
    
    if (items.Probability__c == 100 && items.Cancelled__c == FALSE){
    
    //January 
        if (subs.Month__c == 'Jan'){
            if (items.Jan_Amount__c>0){
                items.Jan_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                n += 1;
                subs.Amount_Sold2__c = amou;
            }
            else
                items.Jan_Subscription__c = NULL;
        }

    //February
        else if (subs.Month__c == 'Feb'){
            if (items.Feb_Amount__c>0){
                items.Feb_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Feb_Subscription__c = NULL;
        }
       
    //March
        else if (subs.Month__c == 'Mar'){
            if (items.Mar_Amount__c>0){
                items.Mar_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Mar_Subscription__c = NULL;
        }
    
    //April
        else if (subs.Month__c == 'Apr'){
            if (items.Apr_Amount__c>0){
                items.Apr_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Apr_Subscription__c = NULL;
        }
    
    //May
        else if (subs.Month__c == 'May'){
            if (items.May_Amount__c>0){
                items.May_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.May_Subscription__c = NULL;
        }
    
    //Jun
        else if (subs.Month__c == 'Jun'){
            if (items.Jun_Amount__c>0){
                items.Jun_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Jun_Subscription__c = NULL;
        }
    
    //July
        else if (subs.Month__c == 'Jul'){
            if (items.Jul_Amount__c>0){
                items.Jul_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Jul_Subscription__c = NULL;
        }
    
    //August
        else if (subs.Month__c == 'Aug'){
            if (items.Aug_Amount__c>0){
                items.Aug_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Aug_Subscription__c = NULL;
        }
    
    //September
        else if (subs.Month__c == 'Sep'){
            if (items.Sep_Amount__c>0){
                items.Sep_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Sep_Subscription__c = NULL;
        }
    
    //October
        else if (subs.Month__c == 'Oct'){
            if (items.Oct_Amount__c>0){
                items.Oct_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Oct_Subscription__c = NULL;
        }
    
    //November
        else if (subs.Month__c == 'Nov'){
            if (items.Nov_Amount__c>0){
                items.Nov_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Nov_Subscription__c = NULL;
        }
    
    //December
        else if (subs.Month__c == 'Dec'){
            if (items.Dec_Amount__c>0){
                items.Dec_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Dec_Subscription__c = NULL;
        }
    }
    
    else {
    items.Jan_Subscription__c = NULL;
    items.Feb_Subscription__c = NULL;
    items.Mar_Subscription__c = NULL;
    items.Apr_Subscription__c = NULL;
    items.May_Subscription__c = NULL;
    items.Jun_Subscription__c = NULL;
    items.Jul_Subscription__c = NULL;
    items.Aug_Subscription__c = NULL;
    items.Sep_Subscription__c = NULL;
    items.Oct_Subscription__c = NULL;
    items.Nov_Subscription__c = NULL;
    items.Dec_Subscription__c = NULL;
    }
}
    
    subs.Number_Sold__c = n;
    
}

update sub;
update ite;





}

 

And finally the Test class:

@isTest
private class TestIPITriggers {

static TestMethod void TestTotalItemAmount(){

Account company = new Account(Name='test company', BillingStreet='123 red', BillingCity='London', BillingPostalCode='123fgh', BillingCountry='United Kingdom');
insert company;

Contact contact = new Contact(LastName = 'test', AccountId = company.Id, Email = 'test@test.com', Phone='1234');
insert contact;

Opportunity deal = new Opportunity(Name='autofill', AccountId = company.Id, RecordTypeId ='01220000000AGqi', Contact_Delegate__c=contact.Id, CloseDate=date.today(), 
                    StageName='Closed - Awaiting Approval', Invoice_Address__c='Company', Address_Invoice_to_contact_above__c='Yes', 
                    Voucher_Copy_Address__c='Company', Address_Voucher_Copy_to_contact_above__c='Yes');
insert deal;

Issue_Year__c event = new Issue_Year__c(Name='LC11a', Date__c=date.newinstance(2011, 1, 1), Description__c='Leaders Council 2011', SUN_T3_Code__c = 'LC2011');
insert event;

IPI_Subscription__c subscription1 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Jan', Name='LCJan11a');
insert subscription1;

IPI_Subscription__c subscription2 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Feb', Name='LCFeb11a');
insert subscription2;

IPI_Subscription__c subscription3 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Mar', Name='LCMar11a');
insert subscription3;

IPI_Subscription__c subscription4 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Apr', Name='LCApr11a');
insert subscription4;

IPI_Subscription__c subscription5 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='May', Name='LCMay11a');
insert subscription5;

IPI_Subscription__c subscription6 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Jun', Name='LCJun11a');
insert subscription6;

IPI_Subscription__c subscription7 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Jul', Name='LCJul11a');
insert subscription7;

IPI_Subscription__c subscription8 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Aug', Name='LCAug11a');
insert subscription8;

IPI_Subscription__c subscription9 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Sep', Name='LCSep11a');
insert subscription9;

IPI_Subscription__c subscription10 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Oct', Name='LCOct11a');
insert subscription10;

IPI_Subscription__c subscription11 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Nov', Name='LCNov11a');
insert subscription11;

IPI_Subscription__c subscription12 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Dec', Name='LCDec11a');
insert subscription12;

Item__c item1 = new Item__c(Name='.', Opportunity__c = deal.Id, Full_Amount2__c=6000, RecordTypeId='01220000000AGp1', Probability__c=100, 
                Event_Issue__c = event.Id, Start_Date__c=date.newinstance(2011, 1, 1), Term__c=6, Payment_Terms__c='28 days');
insert item1;

Item__c[] items = [SELECT Id, Amount__c, Amount_Formula__c, Jan_Subscription__r.Name, Jan_Amount__c, Event_Issue__r.Name FROM Item__c WHERE Event_Issue__r.Name = 'LC11a'];

for (Item__c it :items){
system.assertEquals(it.Jan_Amount__c, 1000);
system.assertEquals(it.Jan_Subscription__r.Name, 'LCJan11a');
}

item1.Start_Date__c=date.newinstance(2011, 7, 1);
update item1;

item1.Probability__c = 50;
update item1;

delete item1;


}

}

 

 

Hope you can help!

 

Hi,

 

    I am displaying some content in the bottom -left of Pdf Page.Here, i want to display the content in two seperate lines in the bottom-left corner of every pdf  page. like

 

   Equity Marketing

   Updated on date 08/01/20011.

 

Now the below code is displaying  inthe same line.Equity Marketing Updated on date 08/01/20011.

 

<head>
<style type="text/css">
@page {
    margin : 70pt .5in .5in .5in;
    @top-center {
        content :element(header);
        font-size : 10 px;
        color : #808080;
        width : 50 px;
     }
     div.header {
    position : running(header) ;
             }  

     
    @bottom-right
         {
            content: "Page: " counter(page) " of " counter(pages);
           
            color : #808080;
         }
       
    @bottom-left {
        content :"Equity Marketing " + <br> + "Updated on date 08/01/20011."; 
                
        font-size : 10 px;
        color : #808080;
    }
   
}
</style>
</head>

 

Please help.