• Abhi Malik
  • NEWBIE
  • 99 Points
  • Member since 2016
  • Salesforce Developer
  • Techmatrix Consulting


  • Chatter
    Feed
  • 1
    Best Answers
  • 7
    Likes Received
  • 1
    Likes Given
  • 12
    Questions
  • 13
    Replies
Trigger:
trigger QuantityMOQValidate on OpportunityLineItem (after insert, after update) {
if(checkRecursive.runOnce()){
    Set<Id> pbeIds = new Set<Id>();
    for (OpportunityLineItem oli : Trigger.new){
        pbeIds.add(oli.pricebookentryid);
        system.debug('adding to pbeIds ------>' + oli.pricebookentry.id);
    }
    Map<Id, double> entries = new Map<Id, Decimal>();
    for(PricebookEntry pbe : [select id, MOQ__c from PricebookEntry where id IN : pbeIds]){
        entries.put(pbe.id, pbe.MOQ__c);
        system.debug('putting in a map id------>' + pbe.id);
        system.debug('putting in a map value------>' + pbe.MOQ__c);
    }
    list<OpportunityLineItem> upOLI = new list<OpportunityLineItem>();
    for(OpportunityLineItem oli: Trigger.new){
        if((oli.Quantity < entries.get(oli.pricebookEntryId)) && entries.get(oli.pricebookEntryId) != Null ){
            system.debug('Quantity less than MOQ');            
            oli.addError('Quantity less than MOQ ['+ Integer.valueof(entries.get(oli.pricebookEntryId))+']');
        }        
        system.debug('>>>>>>>>Quantity----->' + oli.Quantity+'>>MOQ val----->'+ entries.get(oli.pricebookEntryId));        
    }   
}
}

Testclass:

@istest
public class TestQuantityMOQValidate {
 static testMethod void myUnitTest() {       

//Data Prep
//Create Account, Opportunity, Product, etc.
        Account acct1 = new Account(name='test Account One1');
        insert acct1;
//Create Opportunity on Account
        Opportunity Oppty1 = new Opportunity(name='test Oppty One1');
        Oppty1.StageName = 'Selected';
        Oppty1.CloseDate = Date.today() + 30;
        insert Oppty1;         

// Create Products 
         Product2 testprod1 = new Product2 (name='test product one1');
         testprod1.productcode = 'test pd code1one';
        
                insert testprod1;
         Product2 testprod2 = new Product2 (name='test product two2');
         testprod2.productcode = 'test pd code2two';
         
         
         insert testprod2;
// Get Pricebook
         Pricebook2 testpb = [select id from Pricebook2 where IsStandard = true];
         testpb.IsActive=true;
         update testpb;   
// Add to pricebook
        PriceBookEntry pbe1 = new PricebookEntry();
        pbe1.UnitPrice = 100;
        pbe1.Product2Id = testprod1.Id;
        pbe1.Pricebook2Id = testpb.id;
        pbe1.IsActive = true;
        pbe1.useStandardPrice = false;
        insert pbe1;
        PriceBookEntry pbe2 = new PricebookEntry();
        pbe2.UnitPrice = 200;
        pbe2.Product2Id = testprod2.Id; 
        pbe2.Pricebook2Id = testpb.id;
        pbe2.IsActive = true;
        pbe2.useStandardPrice = false;
        insert pbe2;

Test.startTest();

Oppty1.StageName='Selected';
update Oppty1;
System.assertNotEquals('Prospecting', Oppty1.StageName); 

Test.stopTest();
}
}
Duration and Enddate both are not null at same time , and if both are provied a value then the value of both is equal then only record will be saved. otherwise through an error.
the null part is done like this( ISBLANK( Duration__c )&& ISBLANK( End_Date__c ) ) but how to check the value of duration and enddate.
 
Hi everyone
am trying to write a batch class for my project 
when i will create a pledge for donation then based on the duration our transactions are created by trigger but those transactions are not more then 12 but, if my duration is 24 months then .it will create 12 transaction by trigger after that when my list size of transactions is less then 12 every month then it will create a new transaction for that 12 th month and repeat this process for the complete duration .
code:

global class BatchClassPledgeTransaction implements Database.batchable<sObject> {
        String query;
     list<Transaction__c > transList=new list<Transaction__c >();
     // Start Method
    global Database.querylocator start(Database.BatchableContext BC){
        Query = 'Select id,name,Duration__c,Start_Date__c,End_Date__c from Pledge__c';   
        return Database.getQueryLocator(query);
    }
     // Execute Logic
    global void execute(Database.BatchableContext BC, List<Pledge__c> Scope){
       Id PledgeRecTypeCard = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('Card').getRecordTypeId();
       Id PledgeRecTypeGIRO = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('GIRO').getRecordTypeId();
       Id PledgeRecTypeOnline = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('Online').getRecordTypeId();
       Id PledgeRecTypeRecurringCash = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('Recurring Cash').getRecordTypeId();
       Id PledgeRecTypeRecurringCheque  = Schema.SObjectType.Pledge__c.getRecordTypeInfosByName().get('Recurring Cheque ').getRecordTypeId();
       Id TransactionRecTypeCard = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('Card').getRecordTypeId();
       Id TransactionRecTypeGIRO = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('GIRO').getRecordTypeId();
       Id TransactionRecTypeOnline = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('Online').getRecordTypeId();
       Id TransactionRecTypeRecurringCash = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('Recurring Cash').getRecordTypeId();
       Id TransactionRecTypeRecurringCheque  = Schema.SObjectType.Transaction__c .getRecordTypeInfosByName().get('Recurring Cheque ').getRecordTypeId();
       
       for(Pledge__c plg:Scope){
            Integer i=0;
            Integer loopiterater=0;
            Integer j=integer.valueof(plg.Duration__c)-12;
            Integer monthDiff = plg.Start_Date__c.monthsBetween(plg.End_Date__c);
            loopiterater=monthDiff;
        
       if(integer.valueof(plg.Duration__c)>12||loopiterater>12){ 
              for(i=0;i<=j;i++){
                Transaction__c tran= new Transaction__c ();
                       tran.Transaction_Date__c=plg.Start_Date__c.addMonths(i);
                       tran.Pledge__c=plg.id;
                       tran.Amount_Reconciled__c=plg.Amount__c;
                       tran.Donor__c=plg.Account__c;
                       tran.Status__c=plg.Status__c;
                      
                           if(plg.recordtypeid==PledgeRecTypeCard){
                              tran.recordtypeid=TransactionRecTypeCard;
                           } 
                            
                           if(plg.recordtypeid==PledgeRecTypeGIRO){
                              tran.recordtypeid=TransactionRecTypeGIRO;
                           } 
                           
                           if(plg.recordtypeid==PledgeRecTypeOnline){
                              tran.recordtypeid=TransactionRecTypeOnline;
                           } 
                           
                           if(plg.recordtypeid==PledgeRecTypeRecurringCash){
                              tran.recordtypeid=TransactionRecTypeRecurringCash;
                           } 
                           
                           if(plg.recordtypeid==PledgeRecTypeRecurringCheque){
                              tran.recordtypeid=TransactionRecTypeRecurringCheque;
                           } 
                       translist.add(tran);
            }
        
         }
        
       }
    }
     global void finish(Database.BatchableContext BC){
    }
}
<apex:page id="Page" showHeader="false" controller="MyCustomVFPage_CTR" action="{!InitPage}" cache="false">
    <apex:form >
        Thank you for your response.
    </apex:form>
</apex:page>
^ Visualforce page. When someone clicks a link in an alert email I sent them it directs them here, and appends a value in the url based on what link in the alert they click: accepted or rejected. 
 
public class MyCustomVFPage_CTR {
    public String ObjectId {get;set;}
    public String ResponseCode {get;set;}
    public MyCustomVFPage_CTR () {
        ObjectId = ApexPages.currentPage().getParameters().get('ObjectId');
        ResponseCode = ApexPages.currentPage().getParameters().get('ResponseCode');
    }
    public PageReference InitPage() {
        List<Lead> CustomerIssues = [SELECT Id, Status FROM Lead WHERE Id=:ObjectId LIMIT 1];
        if(!CustomerIssues.IsEmpty()){
            CustomerIssues[0].Status = ResponseCode;
            UPDATE CustomerIssues;
        }
        Return null;
    }
}
^ class takes the value that's passed from the url, and updates the related record.

I don't know how to get code coverage for that class. Do I need to write something to test going to a visualforce page?





 
is there any online aptitude test for salesforce?
am completly new to salesforce plese help.
Am trying to use javascript in a very simple VF page but it is not working can anybody please help me to find out the error.
<----------------PAGE---------------->
<apex:page controller="Example2">

 <script type="text/javascript">
 
    function validate()
    {
        if(document.getElementById('{!$Component.frm.pb.pbs.pbsi1.aa}').value == '')
        {
            alert("FIRST NAME is mandatory");
        }
        if(document.getElementById('{!$Component.frm.pb.pbs.bb}').value == '')
        {
            alert("LAST NAME is mandatory");
        }
         else
        {
            callmove();
            alert("NAME has been inserted CONCATINATED");
        }
    }
        
  </script>  
  <apex:form id="frm"> 
  <apex:actionFunction action="{!move}" name="move" reRender="pb"/>  
   <apex:pageBlock id="pb">   
    <apex:pageBlockSection id="pbs">
               <apex:pageBlockSectionItem ><apex:outputLabel value="FIRST NAME"/></apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem id="pbsi1"><apex:inputText value="{!aa}" id="aa"/></apex:pageBlockSectionItem>
              <apex:pageBlockSectionItem ><apex:outputLabel value="LAST NAME"/></apex:pageBlockSectionItem> 
            <apex:pageBlockSectionItem id="pbsi2"><apex:inputText value="{!bb}" id="bb"/></apex:pageBlockSectionItem>
       <apex:pageBlockSectionItem ><apex:outputLabel value="FULL NAME"/></apex:pageBlockSectionItem> 
            <apex:pageBlockSectionItem id="pbsi3"><apex:inputText value="{!cc}" id="cc"/></apex:pageBlockSectionItem>        
    </apex:pageBlockSection>    
      <apex:pageBlockButtons location="bottom" >      
    <apex:commandButton value="CONCAT" action="{!move}" onclick="move();"/>
        </apex:pageBlockButtons>  
   </apex:pageBlock>
     </apex:form>
 </apex:page>

<----------------CLASS---------------->
public class Example2 {

    public PageReference move() {
    cc=aa+bb;
        return null;
    }

public string aa{get;set;}
public string bb{get;set;}
public string cc{get;set;}
 
public pagereference getMove() {
cc=aa+bb;
return null;
        
    }

}
I am trying to Schedule an class at the interval of 5 minutes but am not getting any idea how to perform it, i have seen some examples but they are not working properly please help if any body can.
I have a trigger that has to delete an Account record associated with a Contact record when that particular contact  record gets deleted, However I get the following error when the DML  statement delete get's executed.

"15:14:59:227 FATAL_ERROR System.DmlException: Delete failed. First exception on row 0 with id 0011100000R5Gf0AAF; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0031100000QId7U) is currently in trigger deleteAccountRecord, therefore it cannot recursively delete itself: []"

here is my code: 

trigger deleteAccountRecord on Contact (before delete) {


    RecordType donorRecType = [SELECT ID FROM RecordType WHERE sObjectType = 'Contact' AND DeveloperName = 'Donor_Contact'];
    if(Trigger.isDelete){
        List<ID> contactIDList = new List<ID>();
        for(Contact contactToDelete: Trigger.old) {
            if (contactToDelete.RecordTypeId != donorRecType.Id ) {
                  
                    contactIDList.add(contactToDelete.id);
            }
        }
      
       
       List<Account> DelAccountRecordList = [select id from Account where Id IN(SELECT accountId FROM Contact WHERE Id IN :contactIDList)];

        if(DelAccountRecordList.size() >0 && RecursiveTriggerHelper.isFirstRun){
           RecursiveTriggerHelper.isFirstRun = false;
           delete DelAccountRecordList ;
         
        }
     }
  }

//Helps to stop the trigger from firing twice. (at least It should but it's not working)
public class RecursiveTriggerHelper {

     public static boolean isFirstRun = true;
  
}

I have tried using "After Insert" but DelAccountRecordList returns null with no records. Is there a way to deal with my error without changing "Before Update". I am totally out of ideas on how to solve this issue.

Thanks in advance for your help.